1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 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
|
|
|
/** @var string */ |
31
|
|
|
private $actionResource; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param class-string|object $class of class string or class object |
|
|
|
|
35
|
|
|
* @param string $action The method name eg: action -> getAction |
36
|
|
|
*/ |
37
|
|
|
public function __construct($class, string $action = 'action') |
38
|
|
|
{ |
39
|
|
|
$this->classResource = $class; |
40
|
|
|
$this->actionResource = \ucfirst($action); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Append a missing namespace to resource class. |
45
|
|
|
* |
46
|
|
|
* @internal |
47
|
|
|
*/ |
48
|
|
|
public function namespace(string $namespace): self |
49
|
|
|
{ |
50
|
|
|
$resource = $this->classResource; |
51
|
|
|
|
52
|
|
|
if (\is_string($resource) && '\\' === $resource[0]) { |
53
|
|
|
$this->classResource = $namespace . $resource; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array<int,object|string> |
61
|
|
|
*/ |
62
|
|
|
public function __invoke(string $requestMethod): array |
63
|
|
|
{ |
64
|
|
|
return [$this->classResource, \strtolower($requestMethod) . $this->actionResource]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|