@@ -185,7 +185,8 @@ |
||
185 | 185 | $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
186 | 186 | if (is_numeric($value) && $hasNumeric) { |
187 | 187 | return $value; |
188 | - } elseif (is_array($value)) { |
|
188 | + } |
|
189 | + elseif (is_array($value)) { |
|
189 | 190 | return count($value); |
190 | 191 | } |
191 | 192 | return function_exists('mb_strlen') |
@@ -12,302 +12,302 @@ |
||
12 | 12 | */ |
13 | 13 | class Validator |
14 | 14 | { |
15 | - use ValidationRules; |
|
15 | + use ValidationRules; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - public $errors = []; |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + public $errors = []; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The data under validation. |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $data = []; |
|
22 | + /** |
|
23 | + * The data under validation. |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $data = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * The failed validation rules. |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $failedRules = []; |
|
28 | + /** |
|
29 | + * The failed validation rules. |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $failedRules = []; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The rules to be applied to the data. |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $rules = []; |
|
34 | + /** |
|
35 | + * The rules to be applied to the data. |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $rules = []; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The size related validation rules. |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected $sizeRules = [ |
|
45 | - 'Between', 'Max', 'Min', |
|
46 | - ]; |
|
40 | + /** |
|
41 | + * The size related validation rules. |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected $sizeRules = [ |
|
45 | + 'Between', 'Max', 'Min', |
|
46 | + ]; |
|
47 | 47 | |
48 | - /** |
|
49 | - * The validation rules that imply the field is required. |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $implicitRules = [ |
|
53 | - 'Required', |
|
54 | - ]; |
|
48 | + /** |
|
49 | + * The validation rules that imply the field is required. |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $implicitRules = [ |
|
53 | + 'Required', |
|
54 | + ]; |
|
55 | 55 | |
56 | - /** |
|
57 | - * The numeric related validation rules. |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $numericRules = [ |
|
61 | - 'Number', |
|
62 | - ]; |
|
56 | + /** |
|
57 | + * The numeric related validation rules. |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $numericRules = [ |
|
61 | + 'Number', |
|
62 | + ]; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Run the validator's rules against its data. |
|
66 | - * @param mixed $data |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function validate($data, array $rules = []) |
|
70 | - { |
|
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
77 | - break; |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - return $this->errors; |
|
82 | - } |
|
64 | + /** |
|
65 | + * Run the validator's rules against its data. |
|
66 | + * @param mixed $data |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function validate($data, array $rules = []) |
|
70 | + { |
|
71 | + $this->normalizeData($data); |
|
72 | + $this->setRules($rules); |
|
73 | + foreach ($this->rules as $attribute => $rules) { |
|
74 | + foreach ($rules as $rule) { |
|
75 | + $this->validateAttribute($attribute, $rule); |
|
76 | + if ($this->shouldStopValidating($attribute)) { |
|
77 | + break; |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + return $this->errors; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Validate a given attribute against a rule. |
|
86 | - * @param string $attribute |
|
87 | - * @param string $rule |
|
88 | - * @return void |
|
89 | - * @throws BadMethodCallException |
|
90 | - */ |
|
91 | - public function validateAttribute($attribute, $rule) |
|
92 | - { |
|
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
95 | - return; |
|
96 | - } |
|
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | - } |
|
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
103 | - } |
|
104 | - } |
|
84 | + /** |
|
85 | + * Validate a given attribute against a rule. |
|
86 | + * @param string $attribute |
|
87 | + * @param string $rule |
|
88 | + * @return void |
|
89 | + * @throws BadMethodCallException |
|
90 | + */ |
|
91 | + public function validateAttribute($attribute, $rule) |
|
92 | + { |
|
93 | + list($rule, $parameters) = $this->parseRule($rule); |
|
94 | + if ('' == $rule) { |
|
95 | + return; |
|
96 | + } |
|
97 | + $value = $this->getValue($attribute); |
|
98 | + if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | + } |
|
101 | + if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | + $this->addFailure($attribute, $rule, $parameters); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add an error message to the validator's collection of errors. |
|
108 | - * @param string $attribute |
|
109 | - * @param string $rule |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function addError($attribute, $rule, array $parameters) |
|
113 | - { |
|
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | - $this->errors[$attribute][] = $message; |
|
116 | - } |
|
106 | + /** |
|
107 | + * Add an error message to the validator's collection of errors. |
|
108 | + * @param string $attribute |
|
109 | + * @param string $rule |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function addError($attribute, $rule, array $parameters) |
|
113 | + { |
|
114 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | + $this->errors[$attribute][] = $message; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Add a failed rule and error message to the collection. |
|
120 | - * @param string $attribute |
|
121 | - * @param string $rule |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
125 | - { |
|
126 | - $this->addError($attribute, $rule, $parameters); |
|
127 | - $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | - } |
|
118 | + /** |
|
119 | + * Add a failed rule and error message to the collection. |
|
120 | + * @param string $attribute |
|
121 | + * @param string $rule |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + protected function addFailure($attribute, $rule, array $parameters) |
|
125 | + { |
|
126 | + $this->addError($attribute, $rule, $parameters); |
|
127 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Get the data type of the given attribute. |
|
132 | - * @param string $attribute |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function getAttributeType($attribute) |
|
136 | - { |
|
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
138 | - ? 'length' |
|
139 | - : ''; |
|
140 | - } |
|
130 | + /** |
|
131 | + * Get the data type of the given attribute. |
|
132 | + * @param string $attribute |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function getAttributeType($attribute) |
|
136 | + { |
|
137 | + return !$this->hasRule($attribute, $this->numericRules) |
|
138 | + ? 'length' |
|
139 | + : ''; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get the validation message for an attribute and rule. |
|
144 | - * @param string $attribute |
|
145 | - * @param string $rule |
|
146 | - * @return string|null |
|
147 | - */ |
|
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
149 | - { |
|
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | - } |
|
153 | - $lowerRule = Str::snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
155 | - } |
|
142 | + /** |
|
143 | + * Get the validation message for an attribute and rule. |
|
144 | + * @param string $attribute |
|
145 | + * @param string $rule |
|
146 | + * @return string|null |
|
147 | + */ |
|
148 | + protected function getMessage($attribute, $rule, array $parameters) |
|
149 | + { |
|
150 | + if (in_array($rule, $this->sizeRules)) { |
|
151 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | + } |
|
153 | + $lowerRule = Str::snakeCase($rule); |
|
154 | + return $this->translator($lowerRule, $parameters); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Get a rule and its parameters for a given attribute. |
|
159 | - * @param string $attribute |
|
160 | - * @param string|array $rules |
|
161 | - * @return array|null |
|
162 | - */ |
|
163 | - protected function getRule($attribute, $rules) |
|
164 | - { |
|
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
166 | - return; |
|
167 | - } |
|
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
172 | - return [$rule, $parameters]; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
157 | + /** |
|
158 | + * Get a rule and its parameters for a given attribute. |
|
159 | + * @param string $attribute |
|
160 | + * @param string|array $rules |
|
161 | + * @return array|null |
|
162 | + */ |
|
163 | + protected function getRule($attribute, $rules) |
|
164 | + { |
|
165 | + if (!array_key_exists($attribute, $this->rules)) { |
|
166 | + return; |
|
167 | + } |
|
168 | + $rules = (array) $rules; |
|
169 | + foreach ($this->rules[$attribute] as $rule) { |
|
170 | + list($rule, $parameters) = $this->parseRule($rule); |
|
171 | + if (in_array($rule, $rules)) { |
|
172 | + return [$rule, $parameters]; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the size of an attribute. |
|
179 | - * @param string $attribute |
|
180 | - * @param mixed $value |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - protected function getSize($attribute, $value) |
|
184 | - { |
|
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
187 | - return $value; |
|
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
190 | - } |
|
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
194 | - } |
|
177 | + /** |
|
178 | + * Get the size of an attribute. |
|
179 | + * @param string $attribute |
|
180 | + * @param mixed $value |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + protected function getSize($attribute, $value) |
|
184 | + { |
|
185 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | + if (is_numeric($value) && $hasNumeric) { |
|
187 | + return $value; |
|
188 | + } elseif (is_array($value)) { |
|
189 | + return count($value); |
|
190 | + } |
|
191 | + return function_exists('mb_strlen') |
|
192 | + ? mb_strlen($value) |
|
193 | + : strlen($value); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Get the proper error message for an attribute and size rule. |
|
198 | - * @param string $attribute |
|
199 | - * @param string $rule |
|
200 | - * @return string|null |
|
201 | - */ |
|
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | - { |
|
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = Str::snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
207 | - } |
|
196 | + /** |
|
197 | + * Get the proper error message for an attribute and size rule. |
|
198 | + * @param string $attribute |
|
199 | + * @param string $rule |
|
200 | + * @return string|null |
|
201 | + */ |
|
202 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | + { |
|
204 | + $type = $this->getAttributeType($attribute); |
|
205 | + $lowerRule = Str::snakeCase($rule.$type); |
|
206 | + return $this->translator($lowerRule, $parameters); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get the value of a given attribute. |
|
211 | - * @param string $attribute |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - protected function getValue($attribute) |
|
215 | - { |
|
216 | - if (isset($this->data[$attribute])) { |
|
217 | - return $this->data[$attribute]; |
|
218 | - } |
|
219 | - } |
|
209 | + /** |
|
210 | + * Get the value of a given attribute. |
|
211 | + * @param string $attribute |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + protected function getValue($attribute) |
|
215 | + { |
|
216 | + if (isset($this->data[$attribute])) { |
|
217 | + return $this->data[$attribute]; |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Determine if the given attribute has a rule in the given set. |
|
223 | - * @param string $attribute |
|
224 | - * @param string|array $rules |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - protected function hasRule($attribute, $rules) |
|
228 | - { |
|
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
230 | - } |
|
221 | + /** |
|
222 | + * Determine if the given attribute has a rule in the given set. |
|
223 | + * @param string $attribute |
|
224 | + * @param string|array $rules |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + protected function hasRule($attribute, $rules) |
|
228 | + { |
|
229 | + return !is_null($this->getRule($attribute, $rules)); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Normalize the provided data to an array. |
|
234 | - * @param mixed $data |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function normalizeData($data) |
|
238 | - { |
|
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
241 | - : $data; |
|
242 | - } |
|
232 | + /** |
|
233 | + * Normalize the provided data to an array. |
|
234 | + * @param mixed $data |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function normalizeData($data) |
|
238 | + { |
|
239 | + $this->data = is_object($data) |
|
240 | + ? get_object_vars($data) |
|
241 | + : $data; |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Parse a parameter list. |
|
246 | - * @param string $rule |
|
247 | - * @param string $parameter |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function parseParameters($rule, $parameter) |
|
251 | - { |
|
252 | - return 'regex' == strtolower($rule) |
|
253 | - ? [$parameter] |
|
254 | - : str_getcsv($parameter); |
|
255 | - } |
|
244 | + /** |
|
245 | + * Parse a parameter list. |
|
246 | + * @param string $rule |
|
247 | + * @param string $parameter |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function parseParameters($rule, $parameter) |
|
251 | + { |
|
252 | + return 'regex' == strtolower($rule) |
|
253 | + ? [$parameter] |
|
254 | + : str_getcsv($parameter); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Extract the rule name and parameters from a rule. |
|
259 | - * @param string $rule |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - protected function parseRule($rule) |
|
263 | - { |
|
264 | - $parameters = []; |
|
265 | - if (Str::contains($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
268 | - } |
|
269 | - $rule = Str::camelCase($rule); |
|
270 | - return [$rule, $parameters]; |
|
271 | - } |
|
257 | + /** |
|
258 | + * Extract the rule name and parameters from a rule. |
|
259 | + * @param string $rule |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + protected function parseRule($rule) |
|
263 | + { |
|
264 | + $parameters = []; |
|
265 | + if (Str::contains($rule, ':')) { |
|
266 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | + $parameters = $this->parseParameters($rule, $parameter); |
|
268 | + } |
|
269 | + $rule = Str::camelCase($rule); |
|
270 | + return [$rule, $parameters]; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Set the validation rules. |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - protected function setRules(array $rules) |
|
278 | - { |
|
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
282 | - : $rule; |
|
283 | - } |
|
284 | - $this->rules = $rules; |
|
285 | - } |
|
273 | + /** |
|
274 | + * Set the validation rules. |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + protected function setRules(array $rules) |
|
278 | + { |
|
279 | + foreach ($rules as $key => $rule) { |
|
280 | + $rules[$key] = is_string($rule) |
|
281 | + ? explode('|', $rule) |
|
282 | + : $rule; |
|
283 | + } |
|
284 | + $this->rules = $rules; |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * Check if we should stop further validations on a given attribute. |
|
289 | - * @param string $attribute |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - protected function shouldStopValidating($attribute) |
|
293 | - { |
|
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
295 | - && isset($this->failedRules[$attribute]) |
|
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | - } |
|
287 | + /** |
|
288 | + * Check if we should stop further validations on a given attribute. |
|
289 | + * @param string $attribute |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + protected function shouldStopValidating($attribute) |
|
293 | + { |
|
294 | + return $this->hasRule($attribute, $this->implicitRules) |
|
295 | + && isset($this->failedRules[$attribute]) |
|
296 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Returns a translated message for the attribute. |
|
301 | - * @param string $key |
|
302 | - * @param string $attribute |
|
303 | - * @return void|string |
|
304 | - */ |
|
305 | - protected function translator($key, array $parameters) |
|
306 | - { |
|
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
310 | - } |
|
311 | - return 'error'; |
|
312 | - } |
|
299 | + /** |
|
300 | + * Returns a translated message for the attribute. |
|
301 | + * @param string $key |
|
302 | + * @param string $attribute |
|
303 | + * @return void|string |
|
304 | + */ |
|
305 | + protected function translator($key, array $parameters) |
|
306 | + { |
|
307 | + $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | + if (isset($strings[$key])) { |
|
309 | + return $this->replace($strings[$key], $parameters); |
|
310 | + } |
|
311 | + return 'error'; |
|
312 | + } |
|
313 | 313 | } |
@@ -66,14 +66,14 @@ discard block |
||
66 | 66 | * @param mixed $data |
67 | 67 | * @return array |
68 | 68 | */ |
69 | - public function validate($data, array $rules = []) |
|
69 | + public function validate( $data, array $rules = [] ) |
|
70 | 70 | { |
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
71 | + $this->normalizeData( $data ); |
|
72 | + $this->setRules( $rules ); |
|
73 | + foreach( $this->rules as $attribute => $rules ) { |
|
74 | + foreach( $rules as $rule ) { |
|
75 | + $this->validateAttribute( $attribute, $rule ); |
|
76 | + if( $this->shouldStopValidating( $attribute ) ) { |
|
77 | 77 | break; |
78 | 78 | } |
79 | 79 | } |
@@ -88,18 +88,18 @@ discard block |
||
88 | 88 | * @return void |
89 | 89 | * @throws BadMethodCallException |
90 | 90 | */ |
91 | - public function validateAttribute($attribute, $rule) |
|
91 | + public function validateAttribute( $attribute, $rule ) |
|
92 | 92 | { |
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
93 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
94 | + if( '' == $rule ) { |
|
95 | 95 | return; |
96 | 96 | } |
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
97 | + $value = $this->getValue( $attribute ); |
|
98 | + if( !method_exists( $this, $method = 'validate'.$rule ) ) { |
|
99 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
100 | 100 | } |
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
101 | + if( !$this->$method( $value, $attribute, $parameters ) ) { |
|
102 | + $this->addFailure( $attribute, $rule, $parameters ); |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | * @param string $rule |
110 | 110 | * @return void |
111 | 111 | */ |
112 | - protected function addError($attribute, $rule, array $parameters) |
|
112 | + protected function addError( $attribute, $rule, array $parameters ) |
|
113 | 113 | { |
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
114 | + $message = $this->getMessage( $attribute, $rule, $parameters ); |
|
115 | 115 | $this->errors[$attribute][] = $message; |
116 | 116 | } |
117 | 117 | |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | * @param string $rule |
122 | 122 | * @return void |
123 | 123 | */ |
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
124 | + protected function addFailure( $attribute, $rule, array $parameters ) |
|
125 | 125 | { |
126 | - $this->addError($attribute, $rule, $parameters); |
|
126 | + $this->addError( $attribute, $rule, $parameters ); |
|
127 | 127 | $this->failedRules[$attribute][$rule] = $parameters; |
128 | 128 | } |
129 | 129 | |
@@ -132,9 +132,9 @@ discard block |
||
132 | 132 | * @param string $attribute |
133 | 133 | * @return string |
134 | 134 | */ |
135 | - protected function getAttributeType($attribute) |
|
135 | + protected function getAttributeType( $attribute ) |
|
136 | 136 | { |
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
137 | + return !$this->hasRule( $attribute, $this->numericRules ) |
|
138 | 138 | ? 'length' |
139 | 139 | : ''; |
140 | 140 | } |
@@ -145,13 +145,13 @@ discard block |
||
145 | 145 | * @param string $rule |
146 | 146 | * @return string|null |
147 | 147 | */ |
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
148 | + protected function getMessage( $attribute, $rule, array $parameters ) |
|
149 | 149 | { |
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
150 | + if( in_array( $rule, $this->sizeRules ) ) { |
|
151 | + return $this->getSizeMessage( $attribute, $rule, $parameters ); |
|
152 | 152 | } |
153 | - $lowerRule = Str::snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
153 | + $lowerRule = Str::snakeCase( $rule ); |
|
154 | + return $this->translator( $lowerRule, $parameters ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -160,15 +160,15 @@ discard block |
||
160 | 160 | * @param string|array $rules |
161 | 161 | * @return array|null |
162 | 162 | */ |
163 | - protected function getRule($attribute, $rules) |
|
163 | + protected function getRule( $attribute, $rules ) |
|
164 | 164 | { |
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
165 | + if( !array_key_exists( $attribute, $this->rules ) ) { |
|
166 | 166 | return; |
167 | 167 | } |
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
168 | + $rules = (array)$rules; |
|
169 | + foreach( $this->rules[$attribute] as $rule ) { |
|
170 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
171 | + if( in_array( $rule, $rules ) ) { |
|
172 | 172 | return [$rule, $parameters]; |
173 | 173 | } |
174 | 174 | } |
@@ -180,17 +180,17 @@ discard block |
||
180 | 180 | * @param mixed $value |
181 | 181 | * @return mixed |
182 | 182 | */ |
183 | - protected function getSize($attribute, $value) |
|
183 | + protected function getSize( $attribute, $value ) |
|
184 | 184 | { |
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
185 | + $hasNumeric = $this->hasRule( $attribute, $this->numericRules ); |
|
186 | + if( is_numeric( $value ) && $hasNumeric ) { |
|
187 | 187 | return $value; |
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
188 | + } elseif( is_array( $value ) ) { |
|
189 | + return count( $value ); |
|
190 | 190 | } |
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
191 | + return function_exists( 'mb_strlen' ) |
|
192 | + ? mb_strlen( $value ) |
|
193 | + : strlen( $value ); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -199,11 +199,11 @@ discard block |
||
199 | 199 | * @param string $rule |
200 | 200 | * @return string|null |
201 | 201 | */ |
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
202 | + protected function getSizeMessage( $attribute, $rule, array $parameters ) |
|
203 | 203 | { |
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = Str::snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
204 | + $type = $this->getAttributeType( $attribute ); |
|
205 | + $lowerRule = Str::snakeCase( $rule.$type ); |
|
206 | + return $this->translator( $lowerRule, $parameters ); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | /** |
@@ -211,9 +211,9 @@ discard block |
||
211 | 211 | * @param string $attribute |
212 | 212 | * @return mixed |
213 | 213 | */ |
214 | - protected function getValue($attribute) |
|
214 | + protected function getValue( $attribute ) |
|
215 | 215 | { |
216 | - if (isset($this->data[$attribute])) { |
|
216 | + if( isset($this->data[$attribute]) ) { |
|
217 | 217 | return $this->data[$attribute]; |
218 | 218 | } |
219 | 219 | } |
@@ -224,9 +224,9 @@ discard block |
||
224 | 224 | * @param string|array $rules |
225 | 225 | * @return bool |
226 | 226 | */ |
227 | - protected function hasRule($attribute, $rules) |
|
227 | + protected function hasRule( $attribute, $rules ) |
|
228 | 228 | { |
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
229 | + return !is_null( $this->getRule( $attribute, $rules ) ); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
@@ -234,10 +234,10 @@ discard block |
||
234 | 234 | * @param mixed $data |
235 | 235 | * @return void |
236 | 236 | */ |
237 | - protected function normalizeData($data) |
|
237 | + protected function normalizeData( $data ) |
|
238 | 238 | { |
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
239 | + $this->data = is_object( $data ) |
|
240 | + ? get_object_vars( $data ) |
|
241 | 241 | : $data; |
242 | 242 | } |
243 | 243 | |
@@ -247,11 +247,11 @@ discard block |
||
247 | 247 | * @param string $parameter |
248 | 248 | * @return array |
249 | 249 | */ |
250 | - protected function parseParameters($rule, $parameter) |
|
250 | + protected function parseParameters( $rule, $parameter ) |
|
251 | 251 | { |
252 | - return 'regex' == strtolower($rule) |
|
252 | + return 'regex' == strtolower( $rule ) |
|
253 | 253 | ? [$parameter] |
254 | - : str_getcsv($parameter); |
|
254 | + : str_getcsv( $parameter ); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
@@ -259,14 +259,14 @@ discard block |
||
259 | 259 | * @param string $rule |
260 | 260 | * @return array |
261 | 261 | */ |
262 | - protected function parseRule($rule) |
|
262 | + protected function parseRule( $rule ) |
|
263 | 263 | { |
264 | 264 | $parameters = []; |
265 | - if (Str::contains($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
265 | + if( Str::contains( $rule, ':' ) ) { |
|
266 | + list($rule, $parameter) = explode( ':', $rule, 2 ); |
|
267 | + $parameters = $this->parseParameters( $rule, $parameter ); |
|
268 | 268 | } |
269 | - $rule = Str::camelCase($rule); |
|
269 | + $rule = Str::camelCase( $rule ); |
|
270 | 270 | return [$rule, $parameters]; |
271 | 271 | } |
272 | 272 | |
@@ -274,11 +274,11 @@ discard block |
||
274 | 274 | * Set the validation rules. |
275 | 275 | * @return void |
276 | 276 | */ |
277 | - protected function setRules(array $rules) |
|
277 | + protected function setRules( array $rules ) |
|
278 | 278 | { |
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
279 | + foreach( $rules as $key => $rule ) { |
|
280 | + $rules[$key] = is_string( $rule ) |
|
281 | + ? explode( '|', $rule ) |
|
282 | 282 | : $rule; |
283 | 283 | } |
284 | 284 | $this->rules = $rules; |
@@ -289,11 +289,11 @@ discard block |
||
289 | 289 | * @param string $attribute |
290 | 290 | * @return bool |
291 | 291 | */ |
292 | - protected function shouldStopValidating($attribute) |
|
292 | + protected function shouldStopValidating( $attribute ) |
|
293 | 293 | { |
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
294 | + return $this->hasRule( $attribute, $this->implicitRules ) |
|
295 | 295 | && isset($this->failedRules[$attribute]) |
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
296 | + && array_intersect( array_keys( $this->failedRules[$attribute] ), $this->implicitRules ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -302,11 +302,11 @@ discard block |
||
302 | 302 | * @param string $attribute |
303 | 303 | * @return void|string |
304 | 304 | */ |
305 | - protected function translator($key, array $parameters) |
|
305 | + protected function translator( $key, array $parameters ) |
|
306 | 306 | { |
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
307 | + $strings = glsr( ValidationStringsDefaults::class )->defaults(); |
|
308 | + if( isset($strings[$key]) ) { |
|
309 | + return $this->replace( $strings[$key], $parameters ); |
|
310 | 310 | } |
311 | 311 | return 'error'; |
312 | 312 | } |
@@ -4,9 +4,9 @@ |
||
4 | 4 | |
5 | 5 | interface ShortcodeContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @params string|array $atts |
|
9 | - * @return string |
|
10 | - */ |
|
11 | - public function buildShortcode($atts = []); |
|
7 | + /** |
|
8 | + * @params string|array $atts |
|
9 | + * @return string |
|
10 | + */ |
|
11 | + public function buildShortcode($atts = []); |
|
12 | 12 | } |
@@ -8,5 +8,5 @@ |
||
8 | 8 | * @params string|array $atts |
9 | 9 | * @return string |
10 | 10 | */ |
11 | - public function buildShortcode($atts = []); |
|
11 | + public function buildShortcode( $atts = [] ); |
|
12 | 12 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | interface HooksContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @return void |
|
9 | - */ |
|
10 | - public function run(); |
|
7 | + /** |
|
8 | + * @return void |
|
9 | + */ |
|
10 | + public function run(); |
|
11 | 11 | } |
@@ -6,8 +6,8 @@ |
||
6 | 6 | |
7 | 7 | interface ProviderContract |
8 | 8 | { |
9 | - /** |
|
10 | - * @return void |
|
11 | - */ |
|
12 | - public function register(Application $app); |
|
9 | + /** |
|
10 | + * @return void |
|
11 | + */ |
|
12 | + public function register(Application $app); |
|
13 | 13 | } |
@@ -9,5 +9,5 @@ |
||
9 | 9 | /** |
10 | 10 | * @return void |
11 | 11 | */ |
12 | - public function register(Application $app); |
|
12 | + public function register( Application $app ); |
|
13 | 13 | } |
@@ -6,83 +6,83 @@ |
||
6 | 6 | |
7 | 7 | class SiteReviewsSummaryBlock extends BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'assigned_to' => [ |
|
16 | - 'default' => '', |
|
17 | - 'type' => 'string', |
|
18 | - ], |
|
19 | - 'category' => [ |
|
20 | - 'default' => '', |
|
21 | - 'type' => 'string', |
|
22 | - ], |
|
23 | - 'className' => [ |
|
24 | - 'default' => '', |
|
25 | - 'type' => 'string', |
|
26 | - ], |
|
27 | - 'hide' => [ |
|
28 | - 'default' => '', |
|
29 | - 'type' => 'string', |
|
30 | - ], |
|
31 | - 'post_id' => [ |
|
32 | - 'default' => '', |
|
33 | - 'type' => 'string', |
|
34 | - ], |
|
35 | - 'rating' => [ |
|
36 | - 'default' => '1', |
|
37 | - 'type' => 'number', |
|
38 | - ], |
|
39 | - 'schema' => [ |
|
40 | - 'default' => false, |
|
41 | - 'type' => 'boolean', |
|
42 | - ], |
|
43 | - 'type' => [ |
|
44 | - 'default' => 'local', |
|
45 | - 'type' => 'string', |
|
46 | - ], |
|
47 | - ]; |
|
48 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'assigned_to' => [ |
|
16 | + 'default' => '', |
|
17 | + 'type' => 'string', |
|
18 | + ], |
|
19 | + 'category' => [ |
|
20 | + 'default' => '', |
|
21 | + 'type' => 'string', |
|
22 | + ], |
|
23 | + 'className' => [ |
|
24 | + 'default' => '', |
|
25 | + 'type' => 'string', |
|
26 | + ], |
|
27 | + 'hide' => [ |
|
28 | + 'default' => '', |
|
29 | + 'type' => 'string', |
|
30 | + ], |
|
31 | + 'post_id' => [ |
|
32 | + 'default' => '', |
|
33 | + 'type' => 'string', |
|
34 | + ], |
|
35 | + 'rating' => [ |
|
36 | + 'default' => '1', |
|
37 | + 'type' => 'number', |
|
38 | + ], |
|
39 | + 'schema' => [ |
|
40 | + 'default' => false, |
|
41 | + 'type' => 'boolean', |
|
42 | + ], |
|
43 | + 'type' => [ |
|
44 | + 'default' => 'local', |
|
45 | + 'type' => 'string', |
|
46 | + ], |
|
47 | + ]; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function render(array $attributes) |
|
54 | - { |
|
55 | - $attributes['class'] = $attributes['className']; |
|
56 | - $shortcode = glsr(Shortcode::class); |
|
57 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
58 | - $attributes = $this->normalize($attributes); |
|
59 | - $this->filterShortcodeClass(); |
|
60 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
61 | - $this->filterInterpolation(); |
|
62 | - } |
|
63 | - } |
|
64 | - return $shortcode->buildShortcode($attributes); |
|
65 | - } |
|
50 | + /** |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function render(array $attributes) |
|
54 | + { |
|
55 | + $attributes['class'] = $attributes['className']; |
|
56 | + $shortcode = glsr(Shortcode::class); |
|
57 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
58 | + $attributes = $this->normalize($attributes); |
|
59 | + $this->filterShortcodeClass(); |
|
60 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
61 | + $this->filterInterpolation(); |
|
62 | + } |
|
63 | + } |
|
64 | + return $shortcode->buildShortcode($attributes); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - protected function filterInterpolation() |
|
71 | - { |
|
72 | - add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
73 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
74 | - $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
75 | - return $context; |
|
76 | - }); |
|
77 | - } |
|
67 | + /** |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + protected function filterInterpolation() |
|
71 | + { |
|
72 | + add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
73 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
74 | + $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
75 | + return $context; |
|
76 | + }); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - protected function filterShortcodeClass() |
|
83 | - { |
|
84 | - add_filter('site-reviews/style', function () { |
|
85 | - return 'default'; |
|
86 | - }); |
|
87 | - } |
|
79 | + /** |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + protected function filterShortcodeClass() |
|
83 | + { |
|
84 | + add_filter('site-reviews/style', function () { |
|
85 | + return 'default'; |
|
86 | + }); |
|
87 | + } |
|
88 | 88 | } |
@@ -50,18 +50,18 @@ discard block |
||
50 | 50 | /** |
51 | 51 | * @return string |
52 | 52 | */ |
53 | - public function render(array $attributes) |
|
53 | + public function render( array $attributes ) |
|
54 | 54 | { |
55 | 55 | $attributes['class'] = $attributes['className']; |
56 | - $shortcode = glsr(Shortcode::class); |
|
57 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
58 | - $attributes = $this->normalize($attributes); |
|
56 | + $shortcode = glsr( Shortcode::class ); |
|
57 | + if( 'edit' == filter_input( INPUT_GET, 'context' ) ) { |
|
58 | + $attributes = $this->normalize( $attributes ); |
|
59 | 59 | $this->filterShortcodeClass(); |
60 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
60 | + if( !$this->hasVisibleFields( $shortcode, $attributes ) ) { |
|
61 | 61 | $this->filterInterpolation(); |
62 | 62 | } |
63 | 63 | } |
64 | - return $shortcode->buildShortcode($attributes); |
|
64 | + return $shortcode->buildShortcode( $attributes ); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -69,9 +69,9 @@ discard block |
||
69 | 69 | */ |
70 | 70 | protected function filterInterpolation() |
71 | 71 | { |
72 | - add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
72 | + add_filter( 'site-reviews/interpolate/reviews-summary', function( $context ) { |
|
73 | 73 | $context['class'] = 'glsr-default glsr-block-disabled'; |
74 | - $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
74 | + $context['text'] = __( 'You have hidden all of the fields for this block.', 'site-reviews' ); |
|
75 | 75 | return $context; |
76 | 76 | }); |
77 | 77 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | protected function filterShortcodeClass() |
83 | 83 | { |
84 | - add_filter('site-reviews/style', function () { |
|
84 | + add_filter( 'site-reviews/style', function() { |
|
85 | 85 | return 'default'; |
86 | 86 | }); |
87 | 87 | } |
@@ -6,65 +6,65 @@ |
||
6 | 6 | |
7 | 7 | abstract class BlockGenerator |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - public function attributes() |
|
13 | - { |
|
14 | - return []; |
|
15 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + public function attributes() |
|
13 | + { |
|
14 | + return []; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - public function normalize(array $attributes) |
|
21 | - { |
|
22 | - $hide = array_flip(explode(',', $attributes['hide'])); |
|
23 | - unset($hide['if_empty']); |
|
24 | - $attributes['hide'] = implode(',', array_keys($hide)); |
|
25 | - if (!isset($attributes['assigned_to'])) { |
|
26 | - return $attributes; |
|
27 | - } |
|
28 | - if ('post_id' == $attributes['assigned_to']) { |
|
29 | - $attributes['assigned_to'] = $attributes['post_id']; |
|
30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | - $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
32 | - } |
|
33 | - return $attributes; |
|
34 | - } |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + public function normalize(array $attributes) |
|
21 | + { |
|
22 | + $hide = array_flip(explode(',', $attributes['hide'])); |
|
23 | + unset($hide['if_empty']); |
|
24 | + $attributes['hide'] = implode(',', array_keys($hide)); |
|
25 | + if (!isset($attributes['assigned_to'])) { |
|
26 | + return $attributes; |
|
27 | + } |
|
28 | + if ('post_id' == $attributes['assigned_to']) { |
|
29 | + $attributes['assigned_to'] = $attributes['post_id']; |
|
30 | + } elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | + $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
32 | + } |
|
33 | + return $attributes; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function register($block) |
|
40 | - { |
|
41 | - if (!function_exists('register_block_type')) { |
|
42 | - return; |
|
43 | - } |
|
44 | - register_block_type(Application::ID.'/'.$block, [ |
|
45 | - 'attributes' => $this->attributes(), |
|
46 | - 'editor_script' => Application::ID.'/blocks', |
|
47 | - 'editor_style' => Application::ID.'/blocks', |
|
48 | - 'render_callback' => [$this, 'render'], |
|
49 | - 'style' => Application::ID, |
|
50 | - ]); |
|
51 | - } |
|
36 | + /** |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function register($block) |
|
40 | + { |
|
41 | + if (!function_exists('register_block_type')) { |
|
42 | + return; |
|
43 | + } |
|
44 | + register_block_type(Application::ID.'/'.$block, [ |
|
45 | + 'attributes' => $this->attributes(), |
|
46 | + 'editor_script' => Application::ID.'/blocks', |
|
47 | + 'editor_style' => Application::ID.'/blocks', |
|
48 | + 'render_callback' => [$this, 'render'], |
|
49 | + 'style' => Application::ID, |
|
50 | + ]); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return void |
|
55 | - */ |
|
56 | - abstract public function render(array $attributes); |
|
53 | + /** |
|
54 | + * @return void |
|
55 | + */ |
|
56 | + abstract public function render(array $attributes); |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param mixed $shortcode |
|
60 | - * @return bool |
|
61 | - */ |
|
62 | - protected function hasVisibleFields($shortcode, array $attributes) |
|
63 | - { |
|
64 | - $args = $shortcode->normalizeAtts($attributes); |
|
65 | - $defaults = $shortcode->getHideOptions(); |
|
66 | - $hide = array_flip($args['hide']); |
|
67 | - unset($defaults['if_empty'], $hide['if_empty']); |
|
68 | - return !empty(array_diff_key($defaults, $hide)); |
|
69 | - } |
|
58 | + /** |
|
59 | + * @param mixed $shortcode |
|
60 | + * @return bool |
|
61 | + */ |
|
62 | + protected function hasVisibleFields($shortcode, array $attributes) |
|
63 | + { |
|
64 | + $args = $shortcode->normalizeAtts($attributes); |
|
65 | + $defaults = $shortcode->getHideOptions(); |
|
66 | + $hide = array_flip($args['hide']); |
|
67 | + unset($defaults['if_empty'], $hide['if_empty']); |
|
68 | + return !empty(array_diff_key($defaults, $hide)); |
|
69 | + } |
|
70 | 70 | } |
@@ -17,18 +17,18 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @return array |
19 | 19 | */ |
20 | - public function normalize(array $attributes) |
|
20 | + public function normalize( array $attributes ) |
|
21 | 21 | { |
22 | - $hide = array_flip(explode(',', $attributes['hide'])); |
|
22 | + $hide = array_flip( explode( ',', $attributes['hide'] ) ); |
|
23 | 23 | unset($hide['if_empty']); |
24 | - $attributes['hide'] = implode(',', array_keys($hide)); |
|
25 | - if (!isset($attributes['assigned_to'])) { |
|
24 | + $attributes['hide'] = implode( ',', array_keys( $hide ) ); |
|
25 | + if( !isset($attributes['assigned_to']) ) { |
|
26 | 26 | return $attributes; |
27 | 27 | } |
28 | - if ('post_id' == $attributes['assigned_to']) { |
|
28 | + if( 'post_id' == $attributes['assigned_to'] ) { |
|
29 | 29 | $attributes['assigned_to'] = $attributes['post_id']; |
30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | - $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
30 | + } elseif( 'parent_id' == $attributes['assigned_to'] ) { |
|
31 | + $attributes['assigned_to'] = wp_get_post_parent_id( $attributes['post_id'] ); |
|
32 | 32 | } |
33 | 33 | return $attributes; |
34 | 34 | } |
@@ -36,35 +36,35 @@ discard block |
||
36 | 36 | /** |
37 | 37 | * @return void |
38 | 38 | */ |
39 | - public function register($block) |
|
39 | + public function register( $block ) |
|
40 | 40 | { |
41 | - if (!function_exists('register_block_type')) { |
|
41 | + if( !function_exists( 'register_block_type' ) ) { |
|
42 | 42 | return; |
43 | 43 | } |
44 | - register_block_type(Application::ID.'/'.$block, [ |
|
44 | + register_block_type( Application::ID.'/'.$block, [ |
|
45 | 45 | 'attributes' => $this->attributes(), |
46 | 46 | 'editor_script' => Application::ID.'/blocks', |
47 | 47 | 'editor_style' => Application::ID.'/blocks', |
48 | 48 | 'render_callback' => [$this, 'render'], |
49 | 49 | 'style' => Application::ID, |
50 | - ]); |
|
50 | + ] ); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * @return void |
55 | 55 | */ |
56 | - abstract public function render(array $attributes); |
|
56 | + abstract public function render( array $attributes ); |
|
57 | 57 | |
58 | 58 | /** |
59 | 59 | * @param mixed $shortcode |
60 | 60 | * @return bool |
61 | 61 | */ |
62 | - protected function hasVisibleFields($shortcode, array $attributes) |
|
62 | + protected function hasVisibleFields( $shortcode, array $attributes ) |
|
63 | 63 | { |
64 | - $args = $shortcode->normalizeAtts($attributes); |
|
64 | + $args = $shortcode->normalizeAtts( $attributes ); |
|
65 | 65 | $defaults = $shortcode->getHideOptions(); |
66 | - $hide = array_flip($args['hide']); |
|
66 | + $hide = array_flip( $args['hide'] ); |
|
67 | 67 | unset($defaults['if_empty'], $hide['if_empty']); |
68 | - return !empty(array_diff_key($defaults, $hide)); |
|
68 | + return !empty(array_diff_key( $defaults, $hide )); |
|
69 | 69 | } |
70 | 70 | } |
@@ -27,7 +27,8 @@ |
||
27 | 27 | } |
28 | 28 | if ('post_id' == $attributes['assigned_to']) { |
29 | 29 | $attributes['assigned_to'] = $attributes['post_id']; |
30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
30 | + } |
|
31 | + elseif ('parent_id' == $attributes['assigned_to']) { |
|
31 | 32 | $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
32 | 33 | } |
33 | 34 | return $attributes; |
@@ -68,9 +68,11 @@ discard block |
||
68 | 68 | { |
69 | 69 | if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
70 | 70 | $this->storage[$property] = $value; |
71 | - } elseif (!isset($this->$property)) { |
|
71 | + } |
|
72 | + elseif (!isset($this->$property)) { |
|
72 | 73 | $this->$property = $value; |
73 | - } else { |
|
74 | + } |
|
75 | + else { |
|
74 | 76 | throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
75 | 77 | } |
76 | 78 | } |
@@ -165,7 +167,8 @@ discard block |
||
165 | 167 | { |
166 | 168 | try { |
167 | 169 | return $this->make($parameter->getClass()->name); |
168 | - } catch (Exception $error) { |
|
170 | + } |
|
171 | + catch (Exception $error) { |
|
169 | 172 | if ($parameter->isOptional()) { |
170 | 173 | return $parameter->getDefaultValue(); |
171 | 174 | } |
@@ -11,226 +11,226 @@ |
||
11 | 11 | |
12 | 12 | abstract class Container |
13 | 13 | { |
14 | - const PROTECTED_PROPERTIES = [ |
|
15 | - 'instance', |
|
16 | - 'services', |
|
17 | - 'session', |
|
18 | - 'storage', |
|
19 | - ]; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var static |
|
23 | - */ |
|
24 | - protected static $instance; |
|
25 | - |
|
26 | - /** |
|
27 | - * The container's bound services. |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $services = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $session = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * The container's storage items. |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $storage = []; |
|
42 | - |
|
43 | - /** |
|
44 | - * @return static |
|
45 | - */ |
|
46 | - public static function load() |
|
47 | - { |
|
48 | - if (empty(static::$instance)) { |
|
49 | - static::$instance = new static(); |
|
50 | - } |
|
51 | - return static::$instance; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param string $property |
|
56 | - * @return mixed |
|
57 | - */ |
|
58 | - public function __get($property) |
|
59 | - { |
|
60 | - if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
61 | - return $this->$property; |
|
62 | - } |
|
63 | - $constant = 'static::'.strtoupper(Str::snakeCase($property)); |
|
64 | - if (defined($constant)) { |
|
65 | - return constant($constant); |
|
66 | - } |
|
67 | - return Arr::get($this->storage, $property, null); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param string $property |
|
72 | - * @param string $value |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function __set($property, $value) |
|
76 | - { |
|
77 | - if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
78 | - $this->storage[$property] = $value; |
|
79 | - } elseif (!isset($this->$property)) { |
|
80 | - $this->$property = $value; |
|
81 | - } else { |
|
82 | - throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Bind a service to the container. |
|
88 | - * @param string $alias |
|
89 | - * @param mixed $concrete |
|
90 | - * @return mixed |
|
91 | - */ |
|
92 | - public function bind($alias, $concrete) |
|
93 | - { |
|
94 | - $this->services[$alias] = $concrete; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Request a service from the container. |
|
99 | - * @param mixed $abstract |
|
100 | - * @return mixed |
|
101 | - */ |
|
102 | - public function make($abstract) |
|
103 | - { |
|
104 | - if (!isset($this->services[$abstract])) { |
|
105 | - $abstract = $this->addNamespace($abstract); |
|
106 | - } |
|
107 | - if (isset($this->services[$abstract])) { |
|
108 | - $abstract = $this->services[$abstract]; |
|
109 | - } |
|
110 | - if (is_callable($abstract)) { |
|
111 | - return call_user_func_array($abstract, [$this]); |
|
112 | - } |
|
113 | - if (is_object($abstract)) { |
|
114 | - return $abstract; |
|
115 | - } |
|
116 | - return $this->resolve($abstract); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function sessionClear() |
|
123 | - { |
|
124 | - $this->session = []; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @return mixed |
|
129 | - */ |
|
130 | - public function sessionGet($key, $fallback = '') |
|
131 | - { |
|
132 | - $value = Arr::get($this->session, $key, $fallback); |
|
133 | - unset($this->session[$key]); |
|
134 | - return $value; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return void |
|
139 | - */ |
|
140 | - public function sessionSet($key, $value) |
|
141 | - { |
|
142 | - $this->session[$key] = $value; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Bind a singleton instance to the container. |
|
147 | - * @param string $alias |
|
148 | - * @param callable|string|null $binding |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - public function singleton($alias, $binding) |
|
152 | - { |
|
153 | - $this->bind($alias, $this->make($binding)); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Prefix the current namespace to the abstract if absent. |
|
158 | - * @param string $abstract |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - protected function addNamespace($abstract) |
|
162 | - { |
|
163 | - if (!Str::contains($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
164 | - $abstract = __NAMESPACE__.'\\'.$abstract; |
|
165 | - } |
|
166 | - return $abstract; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Resolve a service from the container. |
|
171 | - * @param mixed $concrete |
|
172 | - * @return mixed |
|
173 | - * @throws Exception |
|
174 | - */ |
|
175 | - protected function resolve($concrete) |
|
176 | - { |
|
177 | - if ($concrete instanceof Closure) { |
|
178 | - return $concrete($this); |
|
179 | - } |
|
180 | - $reflector = new ReflectionClass($concrete); |
|
181 | - if (!$reflector->isInstantiable()) { |
|
182 | - throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
183 | - } |
|
184 | - $constructor = $reflector->getConstructor(); |
|
185 | - if (empty($constructor)) { |
|
186 | - return new $concrete(); |
|
187 | - } |
|
188 | - return $reflector->newInstanceArgs( |
|
189 | - $this->resolveDependencies($constructor->getParameters()) |
|
190 | - ); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Resolve a class based dependency from the container. |
|
195 | - * @return mixed |
|
196 | - * @throws Exception |
|
197 | - */ |
|
198 | - protected function resolveClass(ReflectionParameter $parameter) |
|
199 | - { |
|
200 | - try { |
|
201 | - return $this->make($parameter->getClass()->name); |
|
202 | - } catch (Exception $error) { |
|
203 | - if ($parameter->isOptional()) { |
|
204 | - return $parameter->getDefaultValue(); |
|
205 | - } |
|
206 | - throw $error; |
|
207 | - } |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Resolve all of the dependencies from the ReflectionParameters. |
|
212 | - * @return array |
|
213 | - */ |
|
214 | - protected function resolveDependencies(array $dependencies) |
|
215 | - { |
|
216 | - $results = []; |
|
217 | - foreach ($dependencies as $dependency) { |
|
218 | - $results[] = !is_null($class = $dependency->getClass()) |
|
219 | - ? $this->resolveClass($dependency) |
|
220 | - : $this->resolveDependency($dependency); |
|
221 | - } |
|
222 | - return $results; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Resolve a single ReflectionParameter dependency. |
|
227 | - * @return array|null |
|
228 | - */ |
|
229 | - protected function resolveDependency(ReflectionParameter $parameter) |
|
230 | - { |
|
231 | - if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
232 | - return $parameter->getDefaultValue(); |
|
233 | - } |
|
234 | - return null; |
|
235 | - } |
|
14 | + const PROTECTED_PROPERTIES = [ |
|
15 | + 'instance', |
|
16 | + 'services', |
|
17 | + 'session', |
|
18 | + 'storage', |
|
19 | + ]; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var static |
|
23 | + */ |
|
24 | + protected static $instance; |
|
25 | + |
|
26 | + /** |
|
27 | + * The container's bound services. |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $services = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $session = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * The container's storage items. |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $storage = []; |
|
42 | + |
|
43 | + /** |
|
44 | + * @return static |
|
45 | + */ |
|
46 | + public static function load() |
|
47 | + { |
|
48 | + if (empty(static::$instance)) { |
|
49 | + static::$instance = new static(); |
|
50 | + } |
|
51 | + return static::$instance; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param string $property |
|
56 | + * @return mixed |
|
57 | + */ |
|
58 | + public function __get($property) |
|
59 | + { |
|
60 | + if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
61 | + return $this->$property; |
|
62 | + } |
|
63 | + $constant = 'static::'.strtoupper(Str::snakeCase($property)); |
|
64 | + if (defined($constant)) { |
|
65 | + return constant($constant); |
|
66 | + } |
|
67 | + return Arr::get($this->storage, $property, null); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param string $property |
|
72 | + * @param string $value |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function __set($property, $value) |
|
76 | + { |
|
77 | + if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
78 | + $this->storage[$property] = $value; |
|
79 | + } elseif (!isset($this->$property)) { |
|
80 | + $this->$property = $value; |
|
81 | + } else { |
|
82 | + throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Bind a service to the container. |
|
88 | + * @param string $alias |
|
89 | + * @param mixed $concrete |
|
90 | + * @return mixed |
|
91 | + */ |
|
92 | + public function bind($alias, $concrete) |
|
93 | + { |
|
94 | + $this->services[$alias] = $concrete; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Request a service from the container. |
|
99 | + * @param mixed $abstract |
|
100 | + * @return mixed |
|
101 | + */ |
|
102 | + public function make($abstract) |
|
103 | + { |
|
104 | + if (!isset($this->services[$abstract])) { |
|
105 | + $abstract = $this->addNamespace($abstract); |
|
106 | + } |
|
107 | + if (isset($this->services[$abstract])) { |
|
108 | + $abstract = $this->services[$abstract]; |
|
109 | + } |
|
110 | + if (is_callable($abstract)) { |
|
111 | + return call_user_func_array($abstract, [$this]); |
|
112 | + } |
|
113 | + if (is_object($abstract)) { |
|
114 | + return $abstract; |
|
115 | + } |
|
116 | + return $this->resolve($abstract); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function sessionClear() |
|
123 | + { |
|
124 | + $this->session = []; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @return mixed |
|
129 | + */ |
|
130 | + public function sessionGet($key, $fallback = '') |
|
131 | + { |
|
132 | + $value = Arr::get($this->session, $key, $fallback); |
|
133 | + unset($this->session[$key]); |
|
134 | + return $value; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return void |
|
139 | + */ |
|
140 | + public function sessionSet($key, $value) |
|
141 | + { |
|
142 | + $this->session[$key] = $value; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Bind a singleton instance to the container. |
|
147 | + * @param string $alias |
|
148 | + * @param callable|string|null $binding |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + public function singleton($alias, $binding) |
|
152 | + { |
|
153 | + $this->bind($alias, $this->make($binding)); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Prefix the current namespace to the abstract if absent. |
|
158 | + * @param string $abstract |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + protected function addNamespace($abstract) |
|
162 | + { |
|
163 | + if (!Str::contains($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
164 | + $abstract = __NAMESPACE__.'\\'.$abstract; |
|
165 | + } |
|
166 | + return $abstract; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Resolve a service from the container. |
|
171 | + * @param mixed $concrete |
|
172 | + * @return mixed |
|
173 | + * @throws Exception |
|
174 | + */ |
|
175 | + protected function resolve($concrete) |
|
176 | + { |
|
177 | + if ($concrete instanceof Closure) { |
|
178 | + return $concrete($this); |
|
179 | + } |
|
180 | + $reflector = new ReflectionClass($concrete); |
|
181 | + if (!$reflector->isInstantiable()) { |
|
182 | + throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
183 | + } |
|
184 | + $constructor = $reflector->getConstructor(); |
|
185 | + if (empty($constructor)) { |
|
186 | + return new $concrete(); |
|
187 | + } |
|
188 | + return $reflector->newInstanceArgs( |
|
189 | + $this->resolveDependencies($constructor->getParameters()) |
|
190 | + ); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Resolve a class based dependency from the container. |
|
195 | + * @return mixed |
|
196 | + * @throws Exception |
|
197 | + */ |
|
198 | + protected function resolveClass(ReflectionParameter $parameter) |
|
199 | + { |
|
200 | + try { |
|
201 | + return $this->make($parameter->getClass()->name); |
|
202 | + } catch (Exception $error) { |
|
203 | + if ($parameter->isOptional()) { |
|
204 | + return $parameter->getDefaultValue(); |
|
205 | + } |
|
206 | + throw $error; |
|
207 | + } |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Resolve all of the dependencies from the ReflectionParameters. |
|
212 | + * @return array |
|
213 | + */ |
|
214 | + protected function resolveDependencies(array $dependencies) |
|
215 | + { |
|
216 | + $results = []; |
|
217 | + foreach ($dependencies as $dependency) { |
|
218 | + $results[] = !is_null($class = $dependency->getClass()) |
|
219 | + ? $this->resolveClass($dependency) |
|
220 | + : $this->resolveDependency($dependency); |
|
221 | + } |
|
222 | + return $results; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Resolve a single ReflectionParameter dependency. |
|
227 | + * @return array|null |
|
228 | + */ |
|
229 | + protected function resolveDependency(ReflectionParameter $parameter) |
|
230 | + { |
|
231 | + if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
232 | + return $parameter->getDefaultValue(); |
|
233 | + } |
|
234 | + return null; |
|
235 | + } |
|
236 | 236 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | */ |
46 | 46 | public static function load() |
47 | 47 | { |
48 | - if (empty(static::$instance)) { |
|
48 | + if( empty(static::$instance) ) { |
|
49 | 49 | static::$instance = new static(); |
50 | 50 | } |
51 | 51 | return static::$instance; |
@@ -55,16 +55,16 @@ discard block |
||
55 | 55 | * @param string $property |
56 | 56 | * @return mixed |
57 | 57 | */ |
58 | - public function __get($property) |
|
58 | + public function __get( $property ) |
|
59 | 59 | { |
60 | - if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
60 | + if( property_exists( $this, $property ) && !in_array( $property, static::PROTECTED_PROPERTIES ) ) { |
|
61 | 61 | return $this->$property; |
62 | 62 | } |
63 | - $constant = 'static::'.strtoupper(Str::snakeCase($property)); |
|
64 | - if (defined($constant)) { |
|
65 | - return constant($constant); |
|
63 | + $constant = 'static::'.strtoupper( Str::snakeCase( $property ) ); |
|
64 | + if( defined( $constant ) ) { |
|
65 | + return constant( $constant ); |
|
66 | 66 | } |
67 | - return Arr::get($this->storage, $property, null); |
|
67 | + return Arr::get( $this->storage, $property, null ); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -72,14 +72,14 @@ discard block |
||
72 | 72 | * @param string $value |
73 | 73 | * @return void |
74 | 74 | */ |
75 | - public function __set($property, $value) |
|
75 | + public function __set( $property, $value ) |
|
76 | 76 | { |
77 | - if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
77 | + if( !property_exists( $this, $property ) || in_array( $property, static::PROTECTED_PROPERTIES ) ) { |
|
78 | 78 | $this->storage[$property] = $value; |
79 | - } elseif (!isset($this->$property)) { |
|
79 | + } elseif( !isset($this->$property) ) { |
|
80 | 80 | $this->$property = $value; |
81 | 81 | } else { |
82 | - throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
82 | + throw new Exception( sprintf( 'The "%s" property cannot be changed once set.', $property ) ); |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | * @param mixed $concrete |
90 | 90 | * @return mixed |
91 | 91 | */ |
92 | - public function bind($alias, $concrete) |
|
92 | + public function bind( $alias, $concrete ) |
|
93 | 93 | { |
94 | 94 | $this->services[$alias] = $concrete; |
95 | 95 | } |
@@ -99,21 +99,21 @@ discard block |
||
99 | 99 | * @param mixed $abstract |
100 | 100 | * @return mixed |
101 | 101 | */ |
102 | - public function make($abstract) |
|
102 | + public function make( $abstract ) |
|
103 | 103 | { |
104 | - if (!isset($this->services[$abstract])) { |
|
105 | - $abstract = $this->addNamespace($abstract); |
|
104 | + if( !isset($this->services[$abstract]) ) { |
|
105 | + $abstract = $this->addNamespace( $abstract ); |
|
106 | 106 | } |
107 | - if (isset($this->services[$abstract])) { |
|
107 | + if( isset($this->services[$abstract]) ) { |
|
108 | 108 | $abstract = $this->services[$abstract]; |
109 | 109 | } |
110 | - if (is_callable($abstract)) { |
|
111 | - return call_user_func_array($abstract, [$this]); |
|
110 | + if( is_callable( $abstract ) ) { |
|
111 | + return call_user_func_array( $abstract, [$this] ); |
|
112 | 112 | } |
113 | - if (is_object($abstract)) { |
|
113 | + if( is_object( $abstract ) ) { |
|
114 | 114 | return $abstract; |
115 | 115 | } |
116 | - return $this->resolve($abstract); |
|
116 | + return $this->resolve( $abstract ); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | /** |
128 | 128 | * @return mixed |
129 | 129 | */ |
130 | - public function sessionGet($key, $fallback = '') |
|
130 | + public function sessionGet( $key, $fallback = '' ) |
|
131 | 131 | { |
132 | - $value = Arr::get($this->session, $key, $fallback); |
|
132 | + $value = Arr::get( $this->session, $key, $fallback ); |
|
133 | 133 | unset($this->session[$key]); |
134 | 134 | return $value; |
135 | 135 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | /** |
138 | 138 | * @return void |
139 | 139 | */ |
140 | - public function sessionSet($key, $value) |
|
140 | + public function sessionSet( $key, $value ) |
|
141 | 141 | { |
142 | 142 | $this->session[$key] = $value; |
143 | 143 | } |
@@ -148,9 +148,9 @@ discard block |
||
148 | 148 | * @param callable|string|null $binding |
149 | 149 | * @return void |
150 | 150 | */ |
151 | - public function singleton($alias, $binding) |
|
151 | + public function singleton( $alias, $binding ) |
|
152 | 152 | { |
153 | - $this->bind($alias, $this->make($binding)); |
|
153 | + $this->bind( $alias, $this->make( $binding ) ); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
@@ -158,9 +158,9 @@ discard block |
||
158 | 158 | * @param string $abstract |
159 | 159 | * @return string |
160 | 160 | */ |
161 | - protected function addNamespace($abstract) |
|
161 | + protected function addNamespace( $abstract ) |
|
162 | 162 | { |
163 | - if (!Str::contains($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
163 | + if( !Str::contains( $abstract, __NAMESPACE__ ) && !class_exists( $abstract ) ) { |
|
164 | 164 | $abstract = __NAMESPACE__.'\\'.$abstract; |
165 | 165 | } |
166 | 166 | return $abstract; |
@@ -172,21 +172,21 @@ discard block |
||
172 | 172 | * @return mixed |
173 | 173 | * @throws Exception |
174 | 174 | */ |
175 | - protected function resolve($concrete) |
|
175 | + protected function resolve( $concrete ) |
|
176 | 176 | { |
177 | - if ($concrete instanceof Closure) { |
|
178 | - return $concrete($this); |
|
177 | + if( $concrete instanceof Closure ) { |
|
178 | + return $concrete( $this ); |
|
179 | 179 | } |
180 | - $reflector = new ReflectionClass($concrete); |
|
181 | - if (!$reflector->isInstantiable()) { |
|
182 | - throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
180 | + $reflector = new ReflectionClass( $concrete ); |
|
181 | + if( !$reflector->isInstantiable() ) { |
|
182 | + throw new Exception( 'Target ['.$concrete.'] is not instantiable.' ); |
|
183 | 183 | } |
184 | 184 | $constructor = $reflector->getConstructor(); |
185 | - if (empty($constructor)) { |
|
185 | + if( empty($constructor) ) { |
|
186 | 186 | return new $concrete(); |
187 | 187 | } |
188 | 188 | return $reflector->newInstanceArgs( |
189 | - $this->resolveDependencies($constructor->getParameters()) |
|
189 | + $this->resolveDependencies( $constructor->getParameters() ) |
|
190 | 190 | ); |
191 | 191 | } |
192 | 192 | |
@@ -195,12 +195,12 @@ discard block |
||
195 | 195 | * @return mixed |
196 | 196 | * @throws Exception |
197 | 197 | */ |
198 | - protected function resolveClass(ReflectionParameter $parameter) |
|
198 | + protected function resolveClass( ReflectionParameter $parameter ) |
|
199 | 199 | { |
200 | 200 | try { |
201 | - return $this->make($parameter->getClass()->name); |
|
202 | - } catch (Exception $error) { |
|
203 | - if ($parameter->isOptional()) { |
|
201 | + return $this->make( $parameter->getClass()->name ); |
|
202 | + } catch( Exception $error ) { |
|
203 | + if( $parameter->isOptional() ) { |
|
204 | 204 | return $parameter->getDefaultValue(); |
205 | 205 | } |
206 | 206 | throw $error; |
@@ -211,13 +211,13 @@ discard block |
||
211 | 211 | * Resolve all of the dependencies from the ReflectionParameters. |
212 | 212 | * @return array |
213 | 213 | */ |
214 | - protected function resolveDependencies(array $dependencies) |
|
214 | + protected function resolveDependencies( array $dependencies ) |
|
215 | 215 | { |
216 | 216 | $results = []; |
217 | - foreach ($dependencies as $dependency) { |
|
218 | - $results[] = !is_null($class = $dependency->getClass()) |
|
219 | - ? $this->resolveClass($dependency) |
|
220 | - : $this->resolveDependency($dependency); |
|
217 | + foreach( $dependencies as $dependency ) { |
|
218 | + $results[] = !is_null( $class = $dependency->getClass() ) |
|
219 | + ? $this->resolveClass( $dependency ) |
|
220 | + : $this->resolveDependency( $dependency ); |
|
221 | 221 | } |
222 | 222 | return $results; |
223 | 223 | } |
@@ -226,9 +226,9 @@ discard block |
||
226 | 226 | * Resolve a single ReflectionParameter dependency. |
227 | 227 | * @return array|null |
228 | 228 | */ |
229 | - protected function resolveDependency(ReflectionParameter $parameter) |
|
229 | + protected function resolveDependency( ReflectionParameter $parameter ) |
|
230 | 230 | { |
231 | - if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
231 | + if( $parameter->isArray() && $parameter->isDefaultValueAvailable() ) { |
|
232 | 232 | return $parameter->getDefaultValue(); |
233 | 233 | } |
234 | 234 | return null; |
@@ -7,12 +7,12 @@ |
||
7 | 7 | |
8 | 8 | class RegisterTaxonomy |
9 | 9 | { |
10 | - /** |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function handle(Command $command) |
|
14 | - { |
|
15 | - register_taxonomy(Application::TAXONOMY, Application::POST_TYPE, $command->args); |
|
16 | - register_taxonomy_for_object_type(Application::TAXONOMY, Application::POST_TYPE); |
|
17 | - } |
|
10 | + /** |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function handle(Command $command) |
|
14 | + { |
|
15 | + register_taxonomy(Application::TAXONOMY, Application::POST_TYPE, $command->args); |
|
16 | + register_taxonomy_for_object_type(Application::TAXONOMY, Application::POST_TYPE); |
|
17 | + } |
|
18 | 18 | } |
@@ -10,9 +10,9 @@ |
||
10 | 10 | /** |
11 | 11 | * @return void |
12 | 12 | */ |
13 | - public function handle(Command $command) |
|
13 | + public function handle( Command $command ) |
|
14 | 14 | { |
15 | - register_taxonomy(Application::TAXONOMY, Application::POST_TYPE, $command->args); |
|
16 | - register_taxonomy_for_object_type(Application::TAXONOMY, Application::POST_TYPE); |
|
15 | + register_taxonomy( Application::TAXONOMY, Application::POST_TYPE, $command->args ); |
|
16 | + register_taxonomy_for_object_type( Application::TAXONOMY, Application::POST_TYPE ); |
|
17 | 17 | } |
18 | 18 | } |
@@ -6,16 +6,16 @@ |
||
6 | 6 | |
7 | 7 | class RegisterPostType |
8 | 8 | { |
9 | - /** |
|
10 | - * @return void |
|
11 | - */ |
|
12 | - public function handle(Command $command) |
|
13 | - { |
|
14 | - if (!in_array($command->postType, get_post_types(['_builtin' => true]))) { |
|
15 | - register_post_type($command->postType, $command->args); |
|
16 | - glsr()->postTypeColumns = wp_parse_args(glsr()->postTypeColumns, [ |
|
17 | - $command->postType => $command->columns, |
|
18 | - ]); |
|
19 | - } |
|
20 | - } |
|
9 | + /** |
|
10 | + * @return void |
|
11 | + */ |
|
12 | + public function handle(Command $command) |
|
13 | + { |
|
14 | + if (!in_array($command->postType, get_post_types(['_builtin' => true]))) { |
|
15 | + register_post_type($command->postType, $command->args); |
|
16 | + glsr()->postTypeColumns = wp_parse_args(glsr()->postTypeColumns, [ |
|
17 | + $command->postType => $command->columns, |
|
18 | + ]); |
|
19 | + } |
|
20 | + } |
|
21 | 21 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | /** |
10 | 10 | * @return void |
11 | 11 | */ |
12 | - public function handle(Command $command) |
|
12 | + public function handle( Command $command ) |
|
13 | 13 | { |
14 | - if (!in_array($command->postType, get_post_types(['_builtin' => true]))) { |
|
15 | - register_post_type($command->postType, $command->args); |
|
16 | - glsr()->postTypeColumns = wp_parse_args(glsr()->postTypeColumns, [ |
|
14 | + if( !in_array( $command->postType, get_post_types( ['_builtin' => true] ) ) ) { |
|
15 | + register_post_type( $command->postType, $command->args ); |
|
16 | + glsr()->postTypeColumns = wp_parse_args( glsr()->postTypeColumns, [ |
|
17 | 17 | $command->postType => $command->columns, |
18 | - ]); |
|
18 | + ] ); |
|
19 | 19 | } |
20 | 20 | } |
21 | 21 | } |