|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\Filesystem\Pattern\Chunk; |
|
6
|
|
|
|
|
7
|
|
|
class Pattern |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
private $source; |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Chunk[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private $chunks; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $length; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $pattern |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($pattern) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->source = $pattern; |
|
28
|
|
|
$segments = self::split($pattern); |
|
29
|
|
|
$this->chunks = array_map([__CLASS__, 'createChunk'], $segments); |
|
30
|
|
|
$this->length = sizeof($segments); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $path |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function matches($path) |
|
38
|
|
|
{ |
|
39
|
|
|
$segments = self::split($path); |
|
40
|
|
|
if (sizeof($segments) !== sizeof($this->chunks)) { |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
for ($i = 0; $i < $this->length; $i++) { |
|
44
|
|
|
if (!$this->chunks[$i]->matches($segments[$i])) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $path |
|
53
|
|
|
* @return string[] |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getParameters($path) |
|
56
|
|
|
{ |
|
57
|
|
|
$segments = self::split($path); |
|
58
|
|
|
if (sizeof($segments) !== $this->length) { |
|
59
|
|
|
return []; |
|
60
|
|
|
} |
|
61
|
|
|
$parameters = []; |
|
62
|
|
|
for ($i = 0; $i < $this->length; $i++) { |
|
63
|
|
|
$parameters = array_merge( |
|
64
|
|
|
$parameters, |
|
65
|
|
|
$this->chunks[$i]->getParameters($segments[$i]) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
return $parameters; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getFixedPart() |
|
72
|
|
|
{ |
|
73
|
|
|
$candidates = $this->chunks; |
|
74
|
|
|
$accumulator = []; |
|
75
|
|
|
while (sizeof($candidates) > 0) { |
|
76
|
|
|
/** @var Chunk $chunk */ |
|
77
|
|
|
$chunk = array_shift($candidates); |
|
78
|
|
|
if ($chunk->getType() !== Chunk::TYPE_EXACT_MATCH) { |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
$accumulator[] = $chunk->getExpression(); |
|
82
|
|
|
} |
|
83
|
|
|
return implode('/', $accumulator); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function isFixed() |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ($this->chunks as $chunk) { |
|
89
|
|
|
if ($chunk->getType() === Chunk::TYPE_EXPRESSION) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function resolve(array $parameters) |
|
97
|
|
|
{ |
|
98
|
|
|
$resolved = array_map(function (Chunk $chunk) use ($parameters) { |
|
99
|
|
|
return (string) $chunk->resolve($parameters); |
|
100
|
|
|
}, $this->chunks); |
|
101
|
|
|
return new Pattern(implode('/', $resolved)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritDoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __toString() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->source; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getSource() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->source; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private static function split($path) |
|
121
|
|
|
{ |
|
122
|
|
|
return array_filter(explode('/', trim($path, '/')), function ($item) { |
|
123
|
|
|
return $item !== ''; |
|
124
|
|
|
}); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
129
|
|
|
*/ |
|
130
|
|
|
private static function createChunk($segment) |
|
131
|
|
|
{ |
|
132
|
|
|
return new Chunk($segment); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|