|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing\Handlers; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* An extendable HTTP Verb-based route handler to provide a RESTful API for a resource. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
final class ResourceHandler |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var string|object */ |
|
28
|
|
|
private $classResource; |
|
29
|
|
|
|
|
30
|
|
|
private string $actionResource; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param class-string|object $class of class string or class object |
|
|
|
|
|
|
34
|
|
|
* @param string $action The method name eg: action -> getAction |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($class, string $action = 'action') |
|
37
|
|
|
{ |
|
38
|
|
|
$this->classResource = $class; |
|
39
|
|
|
$this->actionResource = \ucfirst($action); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Append a missing namespace to resource class. |
|
44
|
|
|
* |
|
45
|
|
|
* @internal |
|
46
|
|
|
*/ |
|
47
|
|
|
public function namespace(string $namespace): self |
|
48
|
|
|
{ |
|
49
|
|
|
$resource = $this->classResource; |
|
50
|
|
|
|
|
51
|
|
|
if (\is_string($resource) && '\\' === $resource[0]) { |
|
52
|
|
|
$this->classResource = $namespace . $resource; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array<int,object|string> |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __invoke(string $requestMethod): array |
|
62
|
|
|
{ |
|
63
|
|
|
return [$this->classResource, \strtolower($requestMethod) . $this->actionResource]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|