1 | <?php declare(strict_types=1); |
||
29 | class ParameterSpecBuilder implements ParameterSpecBuilderInterface |
||
30 | { |
||
31 | protected $regexp = '/ |
||
32 | ^ |
||
33 | (?: |
||
34 | (?<type>(\w+\b(?:\(.*\))?)(?:\s*\|\s*(?-1))*) |
||
35 | \s* |
||
36 | ) |
||
37 | (?: |
||
38 | (?<rest>\.{3}) |
||
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) |
|
69 | |||
70 | /** |
||
71 | * @param string $definition |
||
72 | * |
||
73 | * @return ParameterSpecInterface |
||
74 | * @throws ParameterSpecBuilderException |
||
75 | */ |
||
76 | 39 | public function build(string $definition): ParameterSpecInterface |
|
102 | |||
103 | 2 | protected function buildVariadicParameterSpec(array $matches): VariadicParameterSpec |
|
111 | |||
112 | 31 | protected function buildOptionalParameterSpec(array $matches): OptionalParameterSpec |
|
118 | |||
119 | 4 | protected function buildMandatoryParameterSpec(array $matches): MandatoryParameterSpec |
|
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 | case 'null': |
||
|
|||
136 | 3 | return null; |
|
137 | case 'true': |
||
138 | 3 | return true; |
|
139 | 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) |
|
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.