Passed
Push — master ( 54ca4c...4c57ef )
by Pierre
02:19
created

Route::parsedSlugs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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