Test Failed
Push — master ( 79397a...a27c44 )
by Divine Niiquaye
15:30
created

ResourceHandler::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object.
Loading history...
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
     * @internal
44
     *
45
     * @param array<string,mixed> $properties The resource data properties
46
     *
47
     * @return static
48
     */
49
    public static function __set_state(array $properties)
50
    {
51
        return new static($properties['classResource'], $properties['actionResource']);
52
    }
53
54
    /**
55
     * Append a missing namespace to resource class.
56
     *
57
     * @internal
58
     */
59
    public function namespace(string $namespace): self
60
    {
61
        $resource = $this->classResource;
62
63
        if (\is_string($resource) && '\\' === $resource[0]) {
64
            $this->classResource = $namespace . $resource;
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return array<int,object|string>
72
     */
73
    public function __invoke(string $requestMethod): array
74
    {
75
        return [$this->classResource, \strtolower($requestMethod) . $this->actionResource];
76
    }
77
}
78