Route::getMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component\Http;
6
7
use Nymfonya\Component\Http\Interfaces\RouteInterface;
8
9
class Route implements RouteInterface
10
{
11
12
    /**
13
     * allowed request method
14
     *
15
     * @var string
16
     */
17
    protected $method;
18
19
    /**
20
     * uri validation regexp
21
     *
22
     * @var string
23
     */
24
    protected $expr;
25
26
    /**
27
     * slug collection
28
     *
29
     * @var array
30
     */
31
    protected $slugs;
32
33
    /**
34
     * instanciate
35
     *
36
     * @param string $routeItem
37
     */
38 6
    public function __construct(string $routeItem)
39
    {
40 6
        $this->method = 'GET';
41 6
        $this->expr = '/^(*.)$/';
42 6
        $this->slugs = [];
43 6
        if (strpos($routeItem, ';') !== false) {
44
            list(
45 1
                $this->method,
46 1
                $this->expr,
47
                $slugs
48 1
            ) = explode(';', $routeItem);
49 1
            $this->slugs = $this->parsedSlugs($slugs);
50
        } else {
51 6
            $this->expr = $routeItem;
52
        }
53
    }
54
55
    /**
56
     * return regexp pattern
57
     *
58
     * @return string
59
     */
60 2
    public function getExpr(): string
61
    {
62 2
        return $this->expr;
63
    }
64
65
    /**
66
     * return required request method
67
     *
68
     * @return string
69
     */
70 2
    public function getMethod(): string
71
    {
72 2
        return $this->method;
73
    }
74
75
    /**
76
     * return slugs
77
     *
78
     * @return array
79
     */
80 1
    public function getSlugs(): array
81
    {
82 1
        return $this->slugs;
83
    }
84
85
    /**
86
     * return parsed slugs as string collection
87
     *
88
     * @param string $rawSlug
89
     * @return String[]
90
     */
91 1
    protected function parsedSlugs(string $rawSlug): array
92
    {
93 1
        if (is_null($rawSlug) || empty($rawSlug)) {
94 1
            return [];
95
        }
96 1
        return explode(',', $rawSlug);
97
    }
98
}
99