1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Specs\Builder; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorDefinitionBuilderException; |
20
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorDefinitionBuilderInterface; |
21
|
|
|
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ParameterSpecBuilderException; |
22
|
|
|
use Pinepain\JsSandbox\Specs\Parameters\MandatoryParameterSpec; |
23
|
|
|
use Pinepain\JsSandbox\Specs\Parameters\OptionalParameterSpec; |
24
|
|
|
use Pinepain\JsSandbox\Specs\Parameters\ParameterSpecInterface; |
25
|
|
|
use Pinepain\JsSandbox\Specs\Parameters\VariadicParameterSpec; |
26
|
|
|
use function strlen; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class ParameterSpecBuilder implements ParameterSpecBuilderInterface |
30
|
|
|
{ |
31
|
|
|
protected $regexp = '/ |
32
|
|
|
^ |
33
|
|
|
(?: |
34
|
|
|
(?<rest>\.{3}) |
35
|
|
|
\s* |
36
|
|
|
)? |
37
|
|
|
(?: |
38
|
|
|
(?<type>(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*) |
39
|
|
|
\s* |
40
|
|
|
) |
41
|
|
|
(?<name>[_a-z]\w*) |
42
|
|
|
(?: |
43
|
|
|
\s* = \s* |
44
|
|
|
(?<default> |
45
|
|
|
(?:[+-]?[0-9]+\.?[0-9]*) # numbers (no exponential notation) |
46
|
|
|
| |
47
|
|
|
(?:\\\'[^\\\']*\\\') # single-quoted string |
48
|
|
|
| |
49
|
|
|
(?:\"[^\"]*\") # double-quoted string |
50
|
|
|
| |
51
|
|
|
(?:\[\s*\]) # empty array |
52
|
|
|
| |
53
|
|
|
(?:\{\s*\}) # empty object |
54
|
|
|
| |
55
|
|
|
true | false | null |
56
|
|
|
) |
57
|
|
|
)? |
58
|
|
|
$ |
59
|
|
|
/xi'; |
60
|
|
|
/** |
61
|
|
|
* @var ExtractorDefinitionBuilderInterface |
62
|
|
|
*/ |
63
|
|
|
private $builder; |
64
|
|
|
|
65
|
39 |
|
public function __construct(ExtractorDefinitionBuilderInterface $builder) |
66
|
|
|
{ |
67
|
39 |
|
$this->builder = $builder; |
68
|
39 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $definition |
72
|
|
|
* |
73
|
|
|
* @return ParameterSpecInterface |
74
|
|
|
* @throws ParameterSpecBuilderException |
75
|
|
|
*/ |
76
|
39 |
|
public function build(string $definition): ParameterSpecInterface |
77
|
|
|
{ |
78
|
39 |
|
$definition = trim($definition); |
79
|
|
|
|
80
|
39 |
|
if (!$definition) { |
81
|
1 |
|
throw new ParameterSpecBuilderException('Definition must be non-empty string'); |
82
|
|
|
} |
83
|
|
|
|
84
|
38 |
|
if (preg_match($this->regexp, $definition, $matches)) { |
85
|
|
|
try { |
86
|
37 |
|
if ($matches['rest'] ?? false) { |
87
|
2 |
|
return $this->buildVariadicParameterSpec($matches); |
88
|
|
|
} |
89
|
|
|
|
90
|
35 |
|
if ($matches['default'] ?? false) { |
91
|
31 |
|
return $this->buildOptionalParameterSpec($matches); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return $this->buildMandatoryParameterSpec($matches); |
95
|
2 |
|
} catch (ExtractorDefinitionBuilderException $e) { |
96
|
1 |
|
throw new ParameterSpecBuilderException("Unable to parse definition because of extractor failure: " . $e->getMessage()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
throw new ParameterSpecBuilderException("Unable to parse definition: '{$definition}'"); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
protected function buildVariadicParameterSpec(array $matches): VariadicParameterSpec |
104
|
|
|
{ |
105
|
2 |
|
if (isset($matches['default'])) { |
106
|
1 |
|
throw new ParameterSpecBuilderException('Variadic parameter should have no default value'); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return new VariadicParameterSpec($matches['name'], $this->builder->build($matches['type'])); |
110
|
|
|
} |
111
|
|
|
|
112
|
31 |
|
protected function buildOptionalParameterSpec(array $matches): OptionalParameterSpec |
113
|
|
|
{ |
114
|
31 |
|
$default = $this->buildDefaultValue($matches['default']); |
115
|
|
|
|
116
|
31 |
|
return new OptionalParameterSpec($matches['name'], $this->builder->build($matches['type']), $default); |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
protected function buildMandatoryParameterSpec(array $matches): MandatoryParameterSpec |
120
|
|
|
{ |
121
|
4 |
|
return new MandatoryParameterSpec($matches['name'], $this->builder->build($matches['type'])); |
122
|
|
|
} |
123
|
|
|
|
124
|
31 |
|
protected function buildDefaultValue(string $definition) |
125
|
|
|
{ |
126
|
31 |
|
if (is_numeric($definition)) { |
127
|
4 |
|
if (false !== strpos($definition, '.')) { |
128
|
2 |
|
return (float)$definition; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
return (int)$definition; |
132
|
|
|
} |
133
|
|
|
|
134
|
27 |
|
switch (strtolower($definition)) { |
135
|
27 |
|
case 'null': |
|
|
|
|
136
|
3 |
|
return null; |
137
|
24 |
|
case 'true': |
138
|
3 |
|
return true; |
139
|
21 |
|
case 'false': |
140
|
3 |
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// after this point all expected definition values MUST be at least 2 chars length |
144
|
|
|
|
145
|
18 |
|
if (strlen($definition) < 2) { |
146
|
|
|
// UNEXPECTED |
147
|
|
|
// Less likely we will ever get here because it should fail at a parsing step, but just in case |
148
|
|
|
throw new ParameterSpecBuilderException("Unknown default value format '{$definition}'"); |
149
|
|
|
} |
150
|
|
|
|
151
|
18 |
|
if ($this->wrappedWith($definition, '[', ']')) { |
152
|
2 |
|
return []; |
153
|
|
|
} |
154
|
|
|
|
155
|
16 |
|
if ($this->wrappedWith($definition, '{', '}')) { |
156
|
2 |
|
return []; |
157
|
|
|
} |
158
|
|
|
|
159
|
14 |
|
foreach (['"', "'"] as $quote) { |
160
|
14 |
|
if ($this->wrappedWith($definition, $quote, $quote)) { |
161
|
14 |
|
return trim($definition, $quote); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// Less likely we will ever get here because it should fail at a parsing step, but just in case |
166
|
|
|
throw new ParameterSpecBuilderException("Unknown default value format '{$definition}'"); |
167
|
|
|
} |
168
|
|
|
|
169
|
18 |
|
private function wrappedWith(string $definition, string $starts, $ends) |
170
|
|
|
{ |
171
|
18 |
|
assert(strlen($definition) >= 2); |
172
|
|
|
|
173
|
18 |
|
return $starts == $definition[0] && $ends == $definition[-1]; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.