1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SvImages\Parser; |
4
|
|
|
|
5
|
|
|
use SvImages\Exception\RuntimeException; |
6
|
|
|
use SvImages\Exception\TransformerNotFoundException; |
7
|
|
|
use SvImages\Exception\UnexpectedValueException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Vytautas Stankus <[email protected]> |
11
|
|
|
* @license MIT |
12
|
|
|
*/ |
13
|
|
|
class UriParser |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Result |
17
|
|
|
*/ |
18
|
|
|
protected $result; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $options; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $options |
27
|
|
|
*/ |
28
|
3 |
|
public function __construct(array $options) |
29
|
|
|
{ |
30
|
3 |
|
$this->options = $options; |
31
|
3 |
|
$this->result = new Result(); |
32
|
3 |
|
$this->result->setRouteOptions($options); |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $path |
37
|
|
|
* |
38
|
|
|
* @return Result|null |
39
|
|
|
*/ |
40
|
3 |
|
public function parseUri($path) |
41
|
|
|
{ |
42
|
3 |
|
$container = $this->options['container']; |
43
|
3 |
|
if (!preg_match( |
44
|
3 |
|
'/^\/'.preg_quote($container, '/').'(?:\/(?<transformers>[a-zA-Z0-9_\-,\/]+))\/f_key(?<file_path>\/.+)$/', |
45
|
3 |
|
$path, |
46
|
|
|
$matches |
47
|
3 |
|
)) { |
48
|
1 |
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
$result = $this->result; |
52
|
2 |
|
$result->setUriPath($path); |
53
|
2 |
|
$result->setContainer($container); |
54
|
2 |
|
$result->setFilePath($matches['file_path']); |
55
|
2 |
|
$result->setTransformers($this->parseTransformers($matches['transformers'])); |
56
|
|
|
|
57
|
1 |
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $transformersFromUri |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* |
65
|
|
|
* @throws UnexpectedValueException |
66
|
|
|
* @throws TransformerNotFoundException |
67
|
|
|
* @throws RuntimeException |
68
|
|
|
*/ |
69
|
2 |
|
protected function parseTransformers($transformersFromUri) |
70
|
|
|
{ |
71
|
2 |
|
$transformers_list = explode('/', $transformersFromUri); |
72
|
2 |
|
$transformers = []; |
73
|
|
|
|
74
|
2 |
|
foreach ($transformers_list as $transformer) { |
75
|
2 |
|
if (!preg_match( |
76
|
2 |
|
'/^(?<name>[a-zA-Z0-9_\-]+)(?:,(?<options>[a-zA-Z0-9_\-,]+))?$/', |
77
|
2 |
|
$transformer, |
78
|
|
|
$matches |
79
|
2 |
|
)) { |
80
|
|
|
throw new UnexpectedValueException("Can't parse transformer options"); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$index = array_search($matches['name'], array_column($this->options['transformers'], 'name')); |
84
|
2 |
|
if (false === $index) { |
85
|
1 |
|
throw new TransformerNotFoundException(sprintf( |
86
|
1 |
|
"Transformer '%s' does not exist in '%s' container.", |
87
|
1 |
|
$matches['name'], |
88
|
1 |
|
$this->options['container'] |
89
|
1 |
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$item = $this->options['transformers'][$index]; |
93
|
|
|
|
94
|
1 |
|
if (!array_key_exists('type', $item)) { |
95
|
|
|
throw new RuntimeException("Transformer type is not set"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$transformer = [ |
99
|
1 |
|
'type' => $item['type'], |
100
|
1 |
|
'options' => null, |
101
|
1 |
|
]; |
102
|
|
|
|
103
|
1 |
|
if (array_key_exists('defaults', $item)) { |
104
|
1 |
|
$transformer['defaults'] = $item['defaults']; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
if (array_key_exists('allow_options_override', $item)) { |
108
|
|
|
$allow_override_options = $item['allow_options_override']; |
109
|
|
|
} else { |
110
|
1 |
|
$allow_override_options = $this->options['allow_options_override']; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
if (true === $allow_override_options && array_key_exists('options', $matches)) { |
114
|
1 |
|
$transformer['options'] = $matches['options']; |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
$transformers[] = $transformer; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
return $transformers; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|