@@ -7,255 +7,255 @@ |
||
7 | 7 | |
8 | 8 | class Form |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - protected $args; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - protected $dependencies; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var Field |
|
22 | - */ |
|
23 | - protected $field; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $fields; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Normalizer |
|
32 | - */ |
|
33 | - protected $normalize; |
|
34 | - |
|
35 | - public function __construct(Field $field, Normalizer $normalize) |
|
36 | - { |
|
37 | - $this->args = []; |
|
38 | - $this->dependencies = []; |
|
39 | - $this->field = $field; |
|
40 | - $this->fields = []; |
|
41 | - $this->normalize = $normalize; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @param string $property |
|
46 | - * |
|
47 | - * @return mixed |
|
48 | - * @throws Exception |
|
49 | - */ |
|
50 | - public function __get($property) |
|
51 | - { |
|
52 | - switch ($property) { |
|
53 | - case 'args': |
|
54 | - case 'dependencies': |
|
55 | - case 'fields': |
|
56 | - return $this->$property; |
|
57 | - } |
|
58 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param string $property |
|
63 | - * @param string $value |
|
64 | - * |
|
65 | - * @return void |
|
66 | - * @throws Exception |
|
67 | - */ |
|
68 | - public function __set($property, $value) |
|
69 | - { |
|
70 | - switch ($property) { |
|
71 | - case 'args': |
|
72 | - case 'dependencies': |
|
73 | - case 'fields': |
|
74 | - $this->$property = $value; |
|
75 | - break; |
|
76 | - default: |
|
77 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Add a field to the form. |
|
83 | - * |
|
84 | - * @return Form |
|
85 | - */ |
|
86 | - public function addField(array $args = []) |
|
87 | - { |
|
88 | - $field = $this->field->normalize($args); |
|
89 | - |
|
90 | - if (false !== $field->args['render']) { |
|
91 | - $this->dependencies = array_unique( |
|
92 | - array_merge($field->dependencies, $this->dependencies) |
|
93 | - ); |
|
94 | - $this->fields[] = $field; |
|
95 | - } |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Normalize the form arguments. |
|
102 | - * |
|
103 | - * @return Form |
|
104 | - */ |
|
105 | - public function normalize(array $args = []) |
|
106 | - { |
|
107 | - $defaults = [ |
|
108 | - 'action' => '', |
|
109 | - 'attributes' => '', |
|
110 | - 'id' => '', |
|
111 | - 'class' => '', |
|
112 | - 'enctype' => 'multipart/form-data', |
|
113 | - 'method' => 'post', |
|
114 | - 'nonce' => '', |
|
115 | - 'submit' => __('Submit', 'site-reviews'), |
|
116 | - ]; |
|
117 | - |
|
118 | - $this->args = array_merge($defaults, $args); |
|
119 | - |
|
120 | - $attributes = $this->normalize->form($this->args, 'implode'); |
|
121 | - |
|
122 | - $this->args['attributes'] = $attributes; |
|
123 | - |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Render the form. |
|
129 | - * |
|
130 | - * @param mixed $print |
|
131 | - * |
|
132 | - * @return string|void |
|
133 | - */ |
|
134 | - public function render($print = true) |
|
135 | - { |
|
136 | - $rendered = sprintf('<form %s>%s%s</form>', |
|
137 | - $this->args['attributes'], |
|
138 | - $this->generateFields(), |
|
139 | - $this->generateSubmitButton() |
|
140 | - ); |
|
141 | - |
|
142 | - if ((bool) $print && 'return' !== $print) { |
|
143 | - echo $rendered; |
|
144 | - } |
|
145 | - |
|
146 | - return $rendered; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Reset the Form. |
|
151 | - * |
|
152 | - * @return Form |
|
153 | - */ |
|
154 | - public function reset() |
|
155 | - { |
|
156 | - $this->args = []; |
|
157 | - $this->dependencies = []; |
|
158 | - $this->fields = []; |
|
159 | - return $this; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Generate the form fields. |
|
164 | - * |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - protected function generateFields() |
|
168 | - { |
|
169 | - $hiddenFields = ''; |
|
170 | - |
|
171 | - $fields = array_reduce($this->fields, function ($carry, $formField) use (&$hiddenFields) { |
|
172 | - $stringLegend = '<legend class="screen-reader-text"><span>%s</span></legend>'; |
|
173 | - $stringFieldset = '<fieldset%s>%s%s</fieldset>'; |
|
174 | - $stringRendered = '<tr class="glsr-field %s"><th scope="row">%s</th><td>%s</td></tr>'; |
|
175 | - $outsideRendered = '</tbody></table>%s<table class="form-table"><tbody>'; |
|
176 | - |
|
177 | - // set field value only when rendering because we need to check the default setting |
|
178 | - // against the database |
|
179 | - $field = $formField->setValue()->getField(); |
|
180 | - |
|
181 | - $multi = true === $field->multi; |
|
182 | - $label = $field->generateLabel(); |
|
183 | - $rendered = $field->render(); |
|
184 | - |
|
185 | - // render hidden inputs outside the table |
|
186 | - if ('hidden' === $field->args['type']) { |
|
187 | - $hiddenFields .= $rendered; |
|
188 | - return $carry; |
|
189 | - } |
|
190 | - |
|
191 | - $hiddenClass = $this->isFieldHidden($formField) ? 'hidden' : ''; |
|
192 | - |
|
193 | - if ($multi) { |
|
194 | - if ($depends = $formField->getDataDepends()) { |
|
195 | - $depends = sprintf(' data-depends=\'%s\'', json_encode($depends)); |
|
196 | - } |
|
197 | - |
|
198 | - $legend = $label ? sprintf($stringLegend, $label) : ''; |
|
199 | - $rendered = sprintf($stringFieldset, $depends, $legend, $rendered); |
|
200 | - } |
|
201 | - |
|
202 | - $renderedField = $field->outside |
|
203 | - ? sprintf($outsideRendered, $rendered) |
|
204 | - : sprintf($stringRendered, $hiddenClass, $label, $rendered); |
|
205 | - |
|
206 | - return $carry.$renderedField; |
|
207 | - }); |
|
208 | - |
|
209 | - return sprintf('<table class="form-table"><tbody>%s</tbody></table>%s', $fields, $hiddenFields); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Generate the form submit button. |
|
214 | - * |
|
215 | - * @return string|null |
|
216 | - */ |
|
217 | - protected function generateSubmitButton() |
|
218 | - { |
|
219 | - $args = $this->args['submit']; |
|
220 | - |
|
221 | - is_array($args) ?: $args = ['text' => $args]; |
|
222 | - |
|
223 | - $args = shortcode_atts([ |
|
224 | - 'text' => __('Save Changes', 'site-reviews'), |
|
225 | - 'type' => 'primary', |
|
226 | - 'name' => 'submit', |
|
227 | - 'wrap' => true, |
|
228 | - 'other_attributes' => null, |
|
229 | - ], $args); |
|
230 | - |
|
231 | - if (is_admin()) { |
|
232 | - ob_start(); |
|
233 | - submit_button($args['text'], $args['type'], $args['name'], $args['wrap'], $args['other_attributes']); |
|
234 | - return ob_get_clean(); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @param object $field GeminiLabs\Castor\Form\Fields\* |
|
240 | - * |
|
241 | - * @return bool|null |
|
242 | - */ |
|
243 | - protected function isFieldHidden($field) |
|
244 | - { |
|
245 | - if (!($dependsOn = $field->getDataDepends())) { |
|
246 | - return; |
|
247 | - } |
|
248 | - |
|
249 | - foreach ($this->fields as $formField) { |
|
250 | - if ($dependsOn['name'] !== $formField->args['name']) { |
|
251 | - continue; |
|
252 | - } |
|
253 | - |
|
254 | - if (is_array($dependsOn['value'])) { |
|
255 | - return !in_array($formField->args['value'], $dependsOn['value']); |
|
256 | - } |
|
257 | - |
|
258 | - return $dependsOn['value'] != $formField->args['value']; |
|
259 | - } |
|
260 | - } |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + protected $args; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + protected $dependencies; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var Field |
|
22 | + */ |
|
23 | + protected $field; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $fields; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Normalizer |
|
32 | + */ |
|
33 | + protected $normalize; |
|
34 | + |
|
35 | + public function __construct(Field $field, Normalizer $normalize) |
|
36 | + { |
|
37 | + $this->args = []; |
|
38 | + $this->dependencies = []; |
|
39 | + $this->field = $field; |
|
40 | + $this->fields = []; |
|
41 | + $this->normalize = $normalize; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @param string $property |
|
46 | + * |
|
47 | + * @return mixed |
|
48 | + * @throws Exception |
|
49 | + */ |
|
50 | + public function __get($property) |
|
51 | + { |
|
52 | + switch ($property) { |
|
53 | + case 'args': |
|
54 | + case 'dependencies': |
|
55 | + case 'fields': |
|
56 | + return $this->$property; |
|
57 | + } |
|
58 | + throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param string $property |
|
63 | + * @param string $value |
|
64 | + * |
|
65 | + * @return void |
|
66 | + * @throws Exception |
|
67 | + */ |
|
68 | + public function __set($property, $value) |
|
69 | + { |
|
70 | + switch ($property) { |
|
71 | + case 'args': |
|
72 | + case 'dependencies': |
|
73 | + case 'fields': |
|
74 | + $this->$property = $value; |
|
75 | + break; |
|
76 | + default: |
|
77 | + throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Add a field to the form. |
|
83 | + * |
|
84 | + * @return Form |
|
85 | + */ |
|
86 | + public function addField(array $args = []) |
|
87 | + { |
|
88 | + $field = $this->field->normalize($args); |
|
89 | + |
|
90 | + if (false !== $field->args['render']) { |
|
91 | + $this->dependencies = array_unique( |
|
92 | + array_merge($field->dependencies, $this->dependencies) |
|
93 | + ); |
|
94 | + $this->fields[] = $field; |
|
95 | + } |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Normalize the form arguments. |
|
102 | + * |
|
103 | + * @return Form |
|
104 | + */ |
|
105 | + public function normalize(array $args = []) |
|
106 | + { |
|
107 | + $defaults = [ |
|
108 | + 'action' => '', |
|
109 | + 'attributes' => '', |
|
110 | + 'id' => '', |
|
111 | + 'class' => '', |
|
112 | + 'enctype' => 'multipart/form-data', |
|
113 | + 'method' => 'post', |
|
114 | + 'nonce' => '', |
|
115 | + 'submit' => __('Submit', 'site-reviews'), |
|
116 | + ]; |
|
117 | + |
|
118 | + $this->args = array_merge($defaults, $args); |
|
119 | + |
|
120 | + $attributes = $this->normalize->form($this->args, 'implode'); |
|
121 | + |
|
122 | + $this->args['attributes'] = $attributes; |
|
123 | + |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Render the form. |
|
129 | + * |
|
130 | + * @param mixed $print |
|
131 | + * |
|
132 | + * @return string|void |
|
133 | + */ |
|
134 | + public function render($print = true) |
|
135 | + { |
|
136 | + $rendered = sprintf('<form %s>%s%s</form>', |
|
137 | + $this->args['attributes'], |
|
138 | + $this->generateFields(), |
|
139 | + $this->generateSubmitButton() |
|
140 | + ); |
|
141 | + |
|
142 | + if ((bool) $print && 'return' !== $print) { |
|
143 | + echo $rendered; |
|
144 | + } |
|
145 | + |
|
146 | + return $rendered; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Reset the Form. |
|
151 | + * |
|
152 | + * @return Form |
|
153 | + */ |
|
154 | + public function reset() |
|
155 | + { |
|
156 | + $this->args = []; |
|
157 | + $this->dependencies = []; |
|
158 | + $this->fields = []; |
|
159 | + return $this; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Generate the form fields. |
|
164 | + * |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + protected function generateFields() |
|
168 | + { |
|
169 | + $hiddenFields = ''; |
|
170 | + |
|
171 | + $fields = array_reduce($this->fields, function ($carry, $formField) use (&$hiddenFields) { |
|
172 | + $stringLegend = '<legend class="screen-reader-text"><span>%s</span></legend>'; |
|
173 | + $stringFieldset = '<fieldset%s>%s%s</fieldset>'; |
|
174 | + $stringRendered = '<tr class="glsr-field %s"><th scope="row">%s</th><td>%s</td></tr>'; |
|
175 | + $outsideRendered = '</tbody></table>%s<table class="form-table"><tbody>'; |
|
176 | + |
|
177 | + // set field value only when rendering because we need to check the default setting |
|
178 | + // against the database |
|
179 | + $field = $formField->setValue()->getField(); |
|
180 | + |
|
181 | + $multi = true === $field->multi; |
|
182 | + $label = $field->generateLabel(); |
|
183 | + $rendered = $field->render(); |
|
184 | + |
|
185 | + // render hidden inputs outside the table |
|
186 | + if ('hidden' === $field->args['type']) { |
|
187 | + $hiddenFields .= $rendered; |
|
188 | + return $carry; |
|
189 | + } |
|
190 | + |
|
191 | + $hiddenClass = $this->isFieldHidden($formField) ? 'hidden' : ''; |
|
192 | + |
|
193 | + if ($multi) { |
|
194 | + if ($depends = $formField->getDataDepends()) { |
|
195 | + $depends = sprintf(' data-depends=\'%s\'', json_encode($depends)); |
|
196 | + } |
|
197 | + |
|
198 | + $legend = $label ? sprintf($stringLegend, $label) : ''; |
|
199 | + $rendered = sprintf($stringFieldset, $depends, $legend, $rendered); |
|
200 | + } |
|
201 | + |
|
202 | + $renderedField = $field->outside |
|
203 | + ? sprintf($outsideRendered, $rendered) |
|
204 | + : sprintf($stringRendered, $hiddenClass, $label, $rendered); |
|
205 | + |
|
206 | + return $carry.$renderedField; |
|
207 | + }); |
|
208 | + |
|
209 | + return sprintf('<table class="form-table"><tbody>%s</tbody></table>%s', $fields, $hiddenFields); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Generate the form submit button. |
|
214 | + * |
|
215 | + * @return string|null |
|
216 | + */ |
|
217 | + protected function generateSubmitButton() |
|
218 | + { |
|
219 | + $args = $this->args['submit']; |
|
220 | + |
|
221 | + is_array($args) ?: $args = ['text' => $args]; |
|
222 | + |
|
223 | + $args = shortcode_atts([ |
|
224 | + 'text' => __('Save Changes', 'site-reviews'), |
|
225 | + 'type' => 'primary', |
|
226 | + 'name' => 'submit', |
|
227 | + 'wrap' => true, |
|
228 | + 'other_attributes' => null, |
|
229 | + ], $args); |
|
230 | + |
|
231 | + if (is_admin()) { |
|
232 | + ob_start(); |
|
233 | + submit_button($args['text'], $args['type'], $args['name'], $args['wrap'], $args['other_attributes']); |
|
234 | + return ob_get_clean(); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @param object $field GeminiLabs\Castor\Form\Fields\* |
|
240 | + * |
|
241 | + * @return bool|null |
|
242 | + */ |
|
243 | + protected function isFieldHidden($field) |
|
244 | + { |
|
245 | + if (!($dependsOn = $field->getDataDepends())) { |
|
246 | + return; |
|
247 | + } |
|
248 | + |
|
249 | + foreach ($this->fields as $formField) { |
|
250 | + if ($dependsOn['name'] !== $formField->args['name']) { |
|
251 | + continue; |
|
252 | + } |
|
253 | + |
|
254 | + if (is_array($dependsOn['value'])) { |
|
255 | + return !in_array($formField->args['value'], $dependsOn['value']); |
|
256 | + } |
|
257 | + |
|
258 | + return $dependsOn['value'] != $formField->args['value']; |
|
259 | + } |
|
260 | + } |
|
261 | 261 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | protected $normalize; |
34 | 34 | |
35 | - public function __construct(Field $field, Normalizer $normalize) |
|
35 | + public function __construct( Field $field, Normalizer $normalize ) |
|
36 | 36 | { |
37 | 37 | $this->args = []; |
38 | 38 | $this->dependencies = []; |
@@ -47,15 +47,15 @@ discard block |
||
47 | 47 | * @return mixed |
48 | 48 | * @throws Exception |
49 | 49 | */ |
50 | - public function __get($property) |
|
50 | + public function __get( $property ) |
|
51 | 51 | { |
52 | - switch ($property) { |
|
52 | + switch( $property ) { |
|
53 | 53 | case 'args': |
54 | 54 | case 'dependencies': |
55 | 55 | case 'fields': |
56 | 56 | return $this->$property; |
57 | 57 | } |
58 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
58 | + throw new Exception( sprintf( 'Invalid %s property: %s', __CLASS__, $property ) ); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | /** |
@@ -65,16 +65,16 @@ discard block |
||
65 | 65 | * @return void |
66 | 66 | * @throws Exception |
67 | 67 | */ |
68 | - public function __set($property, $value) |
|
68 | + public function __set( $property, $value ) |
|
69 | 69 | { |
70 | - switch ($property) { |
|
70 | + switch( $property ) { |
|
71 | 71 | case 'args': |
72 | 72 | case 'dependencies': |
73 | 73 | case 'fields': |
74 | 74 | $this->$property = $value; |
75 | 75 | break; |
76 | 76 | default: |
77 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
77 | + throw new Exception( sprintf( 'Invalid %s property: %s', __CLASS__, $property ) ); |
|
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
@@ -83,13 +83,13 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @return Form |
85 | 85 | */ |
86 | - public function addField(array $args = []) |
|
86 | + public function addField( array $args = [] ) |
|
87 | 87 | { |
88 | - $field = $this->field->normalize($args); |
|
88 | + $field = $this->field->normalize( $args ); |
|
89 | 89 | |
90 | - if (false !== $field->args['render']) { |
|
90 | + if( false !== $field->args['render'] ) { |
|
91 | 91 | $this->dependencies = array_unique( |
92 | - array_merge($field->dependencies, $this->dependencies) |
|
92 | + array_merge( $field->dependencies, $this->dependencies ) |
|
93 | 93 | ); |
94 | 94 | $this->fields[] = $field; |
95 | 95 | } |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @return Form |
104 | 104 | */ |
105 | - public function normalize(array $args = []) |
|
105 | + public function normalize( array $args = [] ) |
|
106 | 106 | { |
107 | 107 | $defaults = [ |
108 | 108 | 'action' => '', |
@@ -112,12 +112,12 @@ discard block |
||
112 | 112 | 'enctype' => 'multipart/form-data', |
113 | 113 | 'method' => 'post', |
114 | 114 | 'nonce' => '', |
115 | - 'submit' => __('Submit', 'site-reviews'), |
|
115 | + 'submit' => __( 'Submit', 'site-reviews' ), |
|
116 | 116 | ]; |
117 | 117 | |
118 | - $this->args = array_merge($defaults, $args); |
|
118 | + $this->args = array_merge( $defaults, $args ); |
|
119 | 119 | |
120 | - $attributes = $this->normalize->form($this->args, 'implode'); |
|
120 | + $attributes = $this->normalize->form( $this->args, 'implode' ); |
|
121 | 121 | |
122 | 122 | $this->args['attributes'] = $attributes; |
123 | 123 | |
@@ -131,15 +131,15 @@ discard block |
||
131 | 131 | * |
132 | 132 | * @return string|void |
133 | 133 | */ |
134 | - public function render($print = true) |
|
134 | + public function render( $print = true ) |
|
135 | 135 | { |
136 | - $rendered = sprintf('<form %s>%s%s</form>', |
|
136 | + $rendered = sprintf( '<form %s>%s%s</form>', |
|
137 | 137 | $this->args['attributes'], |
138 | 138 | $this->generateFields(), |
139 | 139 | $this->generateSubmitButton() |
140 | 140 | ); |
141 | 141 | |
142 | - if ((bool) $print && 'return' !== $print) { |
|
142 | + if( (bool) $print && 'return' !== $print ) { |
|
143 | 143 | echo $rendered; |
144 | 144 | } |
145 | 145 | |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | { |
169 | 169 | $hiddenFields = ''; |
170 | 170 | |
171 | - $fields = array_reduce($this->fields, function ($carry, $formField) use (&$hiddenFields) { |
|
171 | + $fields = array_reduce( $this->fields, function( $carry, $formField ) use ( &$hiddenFields ) { |
|
172 | 172 | $stringLegend = '<legend class="screen-reader-text"><span>%s</span></legend>'; |
173 | 173 | $stringFieldset = '<fieldset%s>%s%s</fieldset>'; |
174 | 174 | $stringRendered = '<tr class="glsr-field %s"><th scope="row">%s</th><td>%s</td></tr>'; |
@@ -183,30 +183,30 @@ discard block |
||
183 | 183 | $rendered = $field->render(); |
184 | 184 | |
185 | 185 | // render hidden inputs outside the table |
186 | - if ('hidden' === $field->args['type']) { |
|
186 | + if( 'hidden' === $field->args['type'] ) { |
|
187 | 187 | $hiddenFields .= $rendered; |
188 | 188 | return $carry; |
189 | 189 | } |
190 | 190 | |
191 | - $hiddenClass = $this->isFieldHidden($formField) ? 'hidden' : ''; |
|
191 | + $hiddenClass = $this->isFieldHidden( $formField ) ? 'hidden' : ''; |
|
192 | 192 | |
193 | - if ($multi) { |
|
194 | - if ($depends = $formField->getDataDepends()) { |
|
195 | - $depends = sprintf(' data-depends=\'%s\'', json_encode($depends)); |
|
193 | + if( $multi ) { |
|
194 | + if( $depends = $formField->getDataDepends() ) { |
|
195 | + $depends = sprintf( ' data-depends=\'%s\'', json_encode( $depends ) ); |
|
196 | 196 | } |
197 | 197 | |
198 | - $legend = $label ? sprintf($stringLegend, $label) : ''; |
|
199 | - $rendered = sprintf($stringFieldset, $depends, $legend, $rendered); |
|
198 | + $legend = $label ? sprintf( $stringLegend, $label ) : ''; |
|
199 | + $rendered = sprintf( $stringFieldset, $depends, $legend, $rendered ); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | $renderedField = $field->outside |
203 | - ? sprintf($outsideRendered, $rendered) |
|
204 | - : sprintf($stringRendered, $hiddenClass, $label, $rendered); |
|
203 | + ? sprintf( $outsideRendered, $rendered ) |
|
204 | + : sprintf( $stringRendered, $hiddenClass, $label, $rendered ); |
|
205 | 205 | |
206 | 206 | return $carry.$renderedField; |
207 | 207 | }); |
208 | 208 | |
209 | - return sprintf('<table class="form-table"><tbody>%s</tbody></table>%s', $fields, $hiddenFields); |
|
209 | + return sprintf( '<table class="form-table"><tbody>%s</tbody></table>%s', $fields, $hiddenFields ); |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | /** |
@@ -218,19 +218,19 @@ discard block |
||
218 | 218 | { |
219 | 219 | $args = $this->args['submit']; |
220 | 220 | |
221 | - is_array($args) ?: $args = ['text' => $args]; |
|
221 | + is_array( $args ) ?: $args = ['text' => $args]; |
|
222 | 222 | |
223 | - $args = shortcode_atts([ |
|
224 | - 'text' => __('Save Changes', 'site-reviews'), |
|
223 | + $args = shortcode_atts( [ |
|
224 | + 'text' => __( 'Save Changes', 'site-reviews' ), |
|
225 | 225 | 'type' => 'primary', |
226 | 226 | 'name' => 'submit', |
227 | 227 | 'wrap' => true, |
228 | 228 | 'other_attributes' => null, |
229 | - ], $args); |
|
229 | + ], $args ); |
|
230 | 230 | |
231 | - if (is_admin()) { |
|
231 | + if( is_admin() ) { |
|
232 | 232 | ob_start(); |
233 | - submit_button($args['text'], $args['type'], $args['name'], $args['wrap'], $args['other_attributes']); |
|
233 | + submit_button( $args['text'], $args['type'], $args['name'], $args['wrap'], $args['other_attributes'] ); |
|
234 | 234 | return ob_get_clean(); |
235 | 235 | } |
236 | 236 | } |
@@ -240,19 +240,19 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @return bool|null |
242 | 242 | */ |
243 | - protected function isFieldHidden($field) |
|
243 | + protected function isFieldHidden( $field ) |
|
244 | 244 | { |
245 | - if (!($dependsOn = $field->getDataDepends())) { |
|
245 | + if( !( $dependsOn = $field->getDataDepends() ) ) { |
|
246 | 246 | return; |
247 | 247 | } |
248 | 248 | |
249 | - foreach ($this->fields as $formField) { |
|
250 | - if ($dependsOn['name'] !== $formField->args['name']) { |
|
249 | + foreach( $this->fields as $formField ) { |
|
250 | + if( $dependsOn['name'] !== $formField->args['name'] ) { |
|
251 | 251 | continue; |
252 | 252 | } |
253 | 253 | |
254 | - if (is_array($dependsOn['value'])) { |
|
255 | - return !in_array($formField->args['value'], $dependsOn['value']); |
|
254 | + if( is_array( $dependsOn['value'] ) ) { |
|
255 | + return !in_array( $formField->args['value'], $dependsOn['value'] ); |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | return $dependsOn['value'] != $formField->args['value']; |
@@ -5,8 +5,7 @@ |
||
5 | 5 | use Exception; |
6 | 6 | use GeminiLabs\Castor\Services\Normalizer; |
7 | 7 | |
8 | -class Form |
|
9 | -{ |
|
8 | +class Form { |
|
10 | 9 | /** |
11 | 10 | * @var array |
12 | 11 | */ |
@@ -4,78 +4,78 @@ |
||
4 | 4 | |
5 | 5 | class AliasLoader |
6 | 6 | { |
7 | - /** |
|
8 | - * The singleton instance of the loader. |
|
9 | - * |
|
10 | - * @var AliasLoader |
|
11 | - */ |
|
12 | - protected static $instance; |
|
7 | + /** |
|
8 | + * The singleton instance of the loader. |
|
9 | + * |
|
10 | + * @var AliasLoader |
|
11 | + */ |
|
12 | + protected static $instance; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The array of class aliases. |
|
16 | - * |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - protected $aliases; |
|
14 | + /** |
|
15 | + * The array of class aliases. |
|
16 | + * |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + protected $aliases; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Indicates if a loader has been registered. |
|
23 | - * |
|
24 | - * @var bool |
|
25 | - */ |
|
26 | - protected $registered = false; |
|
21 | + /** |
|
22 | + * Indicates if a loader has been registered. |
|
23 | + * |
|
24 | + * @var bool |
|
25 | + */ |
|
26 | + protected $registered = false; |
|
27 | 27 | |
28 | - private function __construct(array $aliases) |
|
29 | - { |
|
30 | - $this->aliases = $aliases; |
|
31 | - } |
|
28 | + private function __construct(array $aliases) |
|
29 | + { |
|
30 | + $this->aliases = $aliases; |
|
31 | + } |
|
32 | 32 | |
33 | - private function __clone() |
|
34 | - { |
|
35 | - } |
|
33 | + private function __clone() |
|
34 | + { |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get or create the singleton alias loader instance. |
|
39 | - * |
|
40 | - * @return AliasLoader |
|
41 | - */ |
|
42 | - public static function getInstance(array $aliases = []) |
|
43 | - { |
|
44 | - if (is_null(static::$instance)) { |
|
45 | - return static::$instance = new static($aliases); |
|
46 | - } |
|
37 | + /** |
|
38 | + * Get or create the singleton alias loader instance. |
|
39 | + * |
|
40 | + * @return AliasLoader |
|
41 | + */ |
|
42 | + public static function getInstance(array $aliases = []) |
|
43 | + { |
|
44 | + if (is_null(static::$instance)) { |
|
45 | + return static::$instance = new static($aliases); |
|
46 | + } |
|
47 | 47 | |
48 | - $aliases = array_merge(static::$instance->aliases, $aliases); |
|
48 | + $aliases = array_merge(static::$instance->aliases, $aliases); |
|
49 | 49 | |
50 | - static::$instance->aliases = $aliases; |
|
50 | + static::$instance->aliases = $aliases; |
|
51 | 51 | |
52 | - return static::$instance; |
|
53 | - } |
|
52 | + return static::$instance; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Load a class alias if it is registered. |
|
57 | - * |
|
58 | - * @param string $alias |
|
59 | - * |
|
60 | - * @return bool|null |
|
61 | - */ |
|
62 | - public function load($alias) |
|
63 | - { |
|
64 | - if (isset($this->aliases[$alias])) { |
|
65 | - return class_alias($this->aliases[$alias], $alias); |
|
66 | - } |
|
67 | - } |
|
55 | + /** |
|
56 | + * Load a class alias if it is registered. |
|
57 | + * |
|
58 | + * @param string $alias |
|
59 | + * |
|
60 | + * @return bool|null |
|
61 | + */ |
|
62 | + public function load($alias) |
|
63 | + { |
|
64 | + if (isset($this->aliases[$alias])) { |
|
65 | + return class_alias($this->aliases[$alias], $alias); |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Register the loader on the auto-loader stack. |
|
71 | - * |
|
72 | - * @return void |
|
73 | - */ |
|
74 | - public function register() |
|
75 | - { |
|
76 | - if (!$this->registered) { |
|
77 | - spl_autoload_register([$this, 'load'], true, true); |
|
78 | - $this->registered = true; |
|
79 | - } |
|
80 | - } |
|
69 | + /** |
|
70 | + * Register the loader on the auto-loader stack. |
|
71 | + * |
|
72 | + * @return void |
|
73 | + */ |
|
74 | + public function register() |
|
75 | + { |
|
76 | + if (!$this->registered) { |
|
77 | + spl_autoload_register([$this, 'load'], true, true); |
|
78 | + $this->registered = true; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | */ |
26 | 26 | protected $registered = false; |
27 | 27 | |
28 | - private function __construct(array $aliases) |
|
28 | + private function __construct( array $aliases ) |
|
29 | 29 | { |
30 | 30 | $this->aliases = $aliases; |
31 | 31 | } |
@@ -39,13 +39,13 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return AliasLoader |
41 | 41 | */ |
42 | - public static function getInstance(array $aliases = []) |
|
42 | + public static function getInstance( array $aliases = [] ) |
|
43 | 43 | { |
44 | - if (is_null(static::$instance)) { |
|
45 | - return static::$instance = new static($aliases); |
|
44 | + if( is_null( static::$instance ) ) { |
|
45 | + return static::$instance = new static( $aliases ); |
|
46 | 46 | } |
47 | 47 | |
48 | - $aliases = array_merge(static::$instance->aliases, $aliases); |
|
48 | + $aliases = array_merge( static::$instance->aliases, $aliases ); |
|
49 | 49 | |
50 | 50 | static::$instance->aliases = $aliases; |
51 | 51 | |
@@ -59,10 +59,10 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @return bool|null |
61 | 61 | */ |
62 | - public function load($alias) |
|
62 | + public function load( $alias ) |
|
63 | 63 | { |
64 | - if (isset($this->aliases[$alias])) { |
|
65 | - return class_alias($this->aliases[$alias], $alias); |
|
64 | + if( isset( $this->aliases[$alias] ) ) { |
|
65 | + return class_alias( $this->aliases[$alias], $alias ); |
|
66 | 66 | } |
67 | 67 | } |
68 | 68 | |
@@ -73,8 +73,8 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function register() |
75 | 75 | { |
76 | - if (!$this->registered) { |
|
77 | - spl_autoload_register([$this, 'load'], true, true); |
|
76 | + if( !$this->registered ) { |
|
77 | + spl_autoload_register( [$this, 'load'], true, true ); |
|
78 | 78 | $this->registered = true; |
79 | 79 | } |
80 | 80 | } |
@@ -2,8 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace GeminiLabs\Castor; |
4 | 4 | |
5 | -class AliasLoader |
|
6 | -{ |
|
5 | +class AliasLoader { |
|
7 | 6 | /** |
8 | 7 | * The singleton instance of the loader. |
9 | 8 | * |
@@ -7,566 +7,566 @@ |
||
7 | 7 | |
8 | 8 | class Validator |
9 | 9 | { |
10 | - /** |
|
11 | - * @var array |
|
12 | - */ |
|
13 | - public $errors = []; |
|
14 | - |
|
15 | - /** |
|
16 | - * The data under validation. |
|
17 | - * |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - protected $data = []; |
|
21 | - |
|
22 | - /** |
|
23 | - * The failed validation rules. |
|
24 | - * |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - protected $failedRules = []; |
|
28 | - |
|
29 | - /** |
|
30 | - * The rules to be applied to the data. |
|
31 | - * |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - protected $rules = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * The size related validation rules. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
41 | - protected $sizeRules = [ |
|
42 | - 'Between', |
|
43 | - 'Max', |
|
44 | - 'Min', |
|
45 | - ]; |
|
46 | - |
|
47 | - /** |
|
48 | - * The validation rules that imply the field is required. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $implicitRules = [ |
|
53 | - 'Required', |
|
54 | - ]; |
|
55 | - |
|
56 | - /** |
|
57 | - * The numeric related validation rules. |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $numericRules = [ |
|
62 | - 'Numeric', |
|
63 | - ]; |
|
64 | - |
|
65 | - /** |
|
66 | - * Run the validator's rules against its data. |
|
67 | - * |
|
68 | - * @param mixed $data |
|
69 | - * |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - public function validate($data, array $rules = []) |
|
73 | - { |
|
74 | - $this->normalizeData($data); |
|
75 | - $this->setRules($rules); |
|
76 | - |
|
77 | - foreach ($this->rules as $attribute => $rules) { |
|
78 | - foreach ($rules as $rule) { |
|
79 | - $this->validateAttribute($rule, $attribute); |
|
80 | - |
|
81 | - if ($this->shouldStopValidating($attribute)) { |
|
82 | - break; |
|
83 | - } |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - return $this->errors; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Add an error message to the validator's collection of errors. |
|
92 | - * |
|
93 | - * @param string $attribute |
|
94 | - * @param string $rule |
|
95 | - * |
|
96 | - * @return void |
|
97 | - */ |
|
98 | - protected function addError($attribute, $rule, array $parameters) |
|
99 | - { |
|
100 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
101 | - |
|
102 | - $this->errors[$attribute]['errors'][] = $message; |
|
103 | - |
|
104 | - if (!isset($this->errors[$attribute]['value'])) { |
|
105 | - $this->errors[$attribute]['value'] = $this->getValue($attribute); |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Add a failed rule and error message to the collection. |
|
111 | - * |
|
112 | - * @param string $attribute |
|
113 | - * @param string $rule |
|
114 | - * |
|
115 | - * @return void |
|
116 | - */ |
|
117 | - protected function addFailure($attribute, $rule, array $parameters) |
|
118 | - { |
|
119 | - $this->addError($attribute, $rule, $parameters); |
|
120 | - |
|
121 | - $this->failedRules[$attribute][$rule] = $parameters; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Get the data type of the given attribute. |
|
126 | - * |
|
127 | - * @param string $attribute |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - protected function getAttributeType($attribute) |
|
131 | - { |
|
132 | - return $this->hasRule($attribute, $this->numericRules) |
|
133 | - ? 'numeric' |
|
134 | - : 'string'; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Get the validation message for an attribute and rule. |
|
139 | - * |
|
140 | - * @param string $attribute |
|
141 | - * @param string $rule |
|
142 | - * |
|
143 | - * @return string|null |
|
144 | - */ |
|
145 | - protected function getMessage($attribute, $rule, array $parameters) |
|
146 | - { |
|
147 | - if (in_array($rule, $this->sizeRules)) { |
|
148 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
149 | - } |
|
150 | - |
|
151 | - $lowerRule = $this->snakeCase($rule); |
|
152 | - |
|
153 | - return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get a rule and its parameters for a given attribute. |
|
158 | - * |
|
159 | - * @param string $attribute |
|
160 | - * @param string|array $rules |
|
161 | - * |
|
162 | - * @return array|null |
|
163 | - */ |
|
164 | - protected function getRule($attribute, $rules) |
|
165 | - { |
|
166 | - if (!array_key_exists($attribute, $this->rules)) { |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - $rules = (array) $rules; |
|
171 | - |
|
172 | - foreach ($this->rules[$attribute] as $rule) { |
|
173 | - list($rule, $parameters) = $this->parseRule($rule); |
|
174 | - |
|
175 | - if (in_array($rule, $rules)) { |
|
176 | - return [$rule, $parameters]; |
|
177 | - } |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the size of an attribute. |
|
183 | - * |
|
184 | - * @param string $attribute |
|
185 | - * @param mixed $value |
|
186 | - * |
|
187 | - * @return mixed |
|
188 | - */ |
|
189 | - protected function getSize($attribute, $value) |
|
190 | - { |
|
191 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
192 | - |
|
193 | - if (is_numeric($value) && $hasNumeric) { |
|
194 | - return $value; |
|
195 | - } elseif (is_array($value)) { |
|
196 | - return count($value); |
|
197 | - } |
|
198 | - |
|
199 | - return mb_strlen($value); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Get the proper error message for an attribute and size rule. |
|
204 | - * |
|
205 | - * @param string $attribute |
|
206 | - * @param string $rule |
|
207 | - * |
|
208 | - * @return string|null |
|
209 | - */ |
|
210 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
211 | - { |
|
212 | - $lowerRule = $this->snakeCase($rule); |
|
213 | - $type = $this->getAttributeType($attribute); |
|
214 | - |
|
215 | - $lowerRule .= ".{$type}"; |
|
216 | - |
|
217 | - return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Get the value of a given attribute. |
|
222 | - * |
|
223 | - * @param string $attribute |
|
224 | - * |
|
225 | - * @return mixed |
|
226 | - */ |
|
227 | - protected function getValue($attribute) |
|
228 | - { |
|
229 | - if (isset($this->data[$attribute])) { |
|
230 | - return $this->data[$attribute]; |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Determine if the given attribute has a rule in the given set. |
|
236 | - * |
|
237 | - * @param string $attribute |
|
238 | - * @param string|array $rules |
|
239 | - * |
|
240 | - * @return bool |
|
241 | - */ |
|
242 | - protected function hasRule($attribute, $rules) |
|
243 | - { |
|
244 | - return !is_null($this->getRule($attribute, $rules)); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Normalize the provided data to an array. |
|
249 | - * |
|
250 | - * @param mixed $data |
|
251 | - * |
|
252 | - * @return $this |
|
253 | - */ |
|
254 | - protected function normalizeData($data) |
|
255 | - { |
|
256 | - // If an object was provided, get its public properties |
|
257 | - if (is_object($data)) { |
|
258 | - $this->data = get_object_vars($data); |
|
259 | - } else { |
|
260 | - $this->data = $data; |
|
261 | - } |
|
262 | - |
|
263 | - return $this; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Parse a parameter list. |
|
268 | - * |
|
269 | - * @param string $rule |
|
270 | - * @param string $parameter |
|
271 | - * |
|
272 | - * @return array |
|
273 | - */ |
|
274 | - protected function parseParameters($rule, $parameter) |
|
275 | - { |
|
276 | - if ('regex' == strtolower($rule)) { |
|
277 | - return [$parameter]; |
|
278 | - } |
|
279 | - |
|
280 | - return str_getcsv($parameter); |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Extract the rule name and parameters from a rule. |
|
285 | - * |
|
286 | - * @param string $rule |
|
287 | - * |
|
288 | - * @return array |
|
289 | - */ |
|
290 | - protected function parseRule($rule) |
|
291 | - { |
|
292 | - $parameters = []; |
|
293 | - |
|
294 | - // example: {rule}:{parameters} |
|
295 | - if (false !== strpos($rule, ':')) { |
|
296 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
297 | - |
|
298 | - // example: {parameter1,parameter2,...} |
|
299 | - $parameters = $this->parseParameters($rule, $parameter); |
|
300 | - } |
|
301 | - |
|
302 | - $rule = ucwords(str_replace(['-', '_'], ' ', trim($rule))); |
|
303 | - $rule = str_replace(' ', '', $rule); |
|
304 | - |
|
305 | - return [$rule, $parameters]; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Replace all placeholders for the between rule. |
|
310 | - * |
|
311 | - * @param string $message |
|
312 | - * |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - protected function replaceBetween($message, array $parameters) |
|
316 | - { |
|
317 | - return str_replace([':min', ':max'], $parameters, $message); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Replace all placeholders for the max rule. |
|
322 | - * |
|
323 | - * @param string $message |
|
324 | - * |
|
325 | - * @return string |
|
326 | - */ |
|
327 | - protected function replaceMax($message, array $parameters) |
|
328 | - { |
|
329 | - return str_replace(':max', $parameters[0], $message); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Replace all placeholders for the min rule. |
|
334 | - * |
|
335 | - * @param string $message |
|
336 | - * |
|
337 | - * @return string |
|
338 | - */ |
|
339 | - protected function replaceMin($message, array $parameters) |
|
340 | - { |
|
341 | - return str_replace(':min', $parameters[0], $message); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Require a certain number of parameters to be present. |
|
346 | - * |
|
347 | - * @param int $count |
|
348 | - * @param string $rule |
|
349 | - * |
|
350 | - * @return void |
|
351 | - * @throws InvalidArgumentException |
|
352 | - */ |
|
353 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
354 | - { |
|
355 | - if (count($parameters) < $count) { |
|
356 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
357 | - } |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * Set the validation rules. |
|
362 | - * |
|
363 | - * @return $this |
|
364 | - */ |
|
365 | - protected function setRules(array $rules) |
|
366 | - { |
|
367 | - foreach ($rules as $key => $rule) { |
|
368 | - $rules[$key] = is_string($rule) ? explode('|', $rule) : $rule; |
|
369 | - } |
|
370 | - |
|
371 | - $this->rules = $rules; |
|
372 | - |
|
373 | - return $this; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Check if we should stop further validations on a given attribute. |
|
378 | - * |
|
379 | - * @param string $attribute |
|
380 | - * |
|
381 | - * @return bool |
|
382 | - */ |
|
383 | - protected function shouldStopValidating($attribute) |
|
384 | - { |
|
385 | - return $this->hasRule($attribute, $this->implicitRules) |
|
386 | - && isset($this->failedRules[$attribute]) |
|
387 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Convert a string to snake case. |
|
392 | - * |
|
393 | - * @param string $string |
|
394 | - * |
|
395 | - * @return string |
|
396 | - */ |
|
397 | - protected function snakeCase($string) |
|
398 | - { |
|
399 | - if (!ctype_lower($string)) { |
|
400 | - $string = preg_replace('/\s+/u', '', $string); |
|
401 | - $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
402 | - $string = mb_strtolower($string, 'UTF-8'); |
|
403 | - } |
|
404 | - |
|
405 | - return $string; |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Returns a translated message for the attribute. |
|
410 | - * |
|
411 | - * @param string $key |
|
412 | - * @param string $rule |
|
413 | - * @param string $attribute |
|
414 | - * |
|
415 | - * @return string|null |
|
416 | - */ |
|
417 | - protected function translator($key, $rule, $attribute, array $parameters) |
|
418 | - { |
|
419 | - $strings = []; //glsr_resolve( 'Strings' )->validation(); |
|
420 | - |
|
421 | - $message = isset($strings[$key]) |
|
422 | - ? $strings[$key] |
|
423 | - : false; |
|
424 | - |
|
425 | - if (!$message) { |
|
426 | - return; |
|
427 | - } |
|
428 | - |
|
429 | - $message = str_replace(':attribute', $attribute, $message); |
|
430 | - |
|
431 | - if (method_exists($this, $replacer = "replace{$rule}")) { |
|
432 | - $message = $this->$replacer($message, $parameters); |
|
433 | - } |
|
434 | - |
|
435 | - return $message; |
|
436 | - } |
|
437 | - |
|
438 | - // Rules Validation |
|
439 | - // --------------------------------------------------------------------------------------------- |
|
440 | - |
|
441 | - /** |
|
442 | - * Validate that an attribute was "accepted". |
|
443 | - * |
|
444 | - * This validation rule implies the attribute is "required". |
|
445 | - * |
|
446 | - * @param mixed $value |
|
447 | - * |
|
448 | - * @return bool |
|
449 | - */ |
|
450 | - protected function validateAccepted($value) |
|
451 | - { |
|
452 | - $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
453 | - |
|
454 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Validate a given attribute against a rule. |
|
459 | - * |
|
460 | - * @param string $rule |
|
461 | - * @param string $attribute |
|
462 | - * |
|
463 | - * @return void |
|
464 | - * @throws BadMethodCallException |
|
465 | - */ |
|
466 | - protected function validateAttribute($rule, $attribute) |
|
467 | - { |
|
468 | - list($rule, $parameters) = $this->parseRule($rule); |
|
469 | - |
|
470 | - if ('' == $rule) { |
|
471 | - return; |
|
472 | - } |
|
473 | - |
|
474 | - $method = "validate{$rule}"; |
|
475 | - |
|
476 | - if (!method_exists($this, $method)) { |
|
477 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
478 | - } |
|
479 | - |
|
480 | - if (!$this->$method($this->getValue($attribute), $attribute, $parameters)) { |
|
481 | - $this->addFailure($attribute, $rule, $parameters); |
|
482 | - } |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Validate the size of an attribute is between a set of values. |
|
487 | - * |
|
488 | - * @param mixed $value |
|
489 | - * @param string $attribute |
|
490 | - * |
|
491 | - * @return bool |
|
492 | - */ |
|
493 | - protected function validateBetween($value, $attribute, array $parameters) |
|
494 | - { |
|
495 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
496 | - |
|
497 | - $size = $this->getSize($attribute, $value); |
|
498 | - |
|
499 | - return $size >= $parameters[0] && $size <= $parameters[1]; |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Validate that an attribute is a valid e-mail address. |
|
504 | - * |
|
505 | - * @param mixed $value |
|
506 | - * |
|
507 | - * @return bool |
|
508 | - */ |
|
509 | - protected function validateEmail($value) |
|
510 | - { |
|
511 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Validate the size of an attribute is less than a maximum value. |
|
516 | - * |
|
517 | - * @param mixed $value |
|
518 | - * @param string $attribute |
|
519 | - * |
|
520 | - * @return bool |
|
521 | - */ |
|
522 | - protected function validateMax($value, $attribute, array $parameters) |
|
523 | - { |
|
524 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
525 | - |
|
526 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Validate the size of an attribute is greater than a minimum value. |
|
531 | - * |
|
532 | - * @param mixed $value |
|
533 | - * @param string $attribute |
|
534 | - * |
|
535 | - * @return bool |
|
536 | - */ |
|
537 | - protected function validateMin($value, $attribute, array $parameters) |
|
538 | - { |
|
539 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
540 | - |
|
541 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Validate that an attribute is numeric. |
|
546 | - * |
|
547 | - * @param mixed $value |
|
548 | - * |
|
549 | - * @return bool |
|
550 | - */ |
|
551 | - protected function validateNumeric($value) |
|
552 | - { |
|
553 | - return is_numeric($value); |
|
554 | - } |
|
555 | - |
|
556 | - /** |
|
557 | - * Validate that a required attribute exists. |
|
558 | - * |
|
559 | - * @param mixed $value |
|
560 | - * |
|
561 | - * @return bool |
|
562 | - */ |
|
563 | - protected function validateRequired($value) |
|
564 | - { |
|
565 | - if (is_string($value)) { |
|
566 | - $value = trim($value); |
|
567 | - } |
|
568 | - return is_null($value) || empty($value) |
|
569 | - ? false |
|
570 | - : true; |
|
571 | - } |
|
10 | + /** |
|
11 | + * @var array |
|
12 | + */ |
|
13 | + public $errors = []; |
|
14 | + |
|
15 | + /** |
|
16 | + * The data under validation. |
|
17 | + * |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + protected $data = []; |
|
21 | + |
|
22 | + /** |
|
23 | + * The failed validation rules. |
|
24 | + * |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + protected $failedRules = []; |
|
28 | + |
|
29 | + /** |
|
30 | + * The rules to be applied to the data. |
|
31 | + * |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + protected $rules = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * The size related validation rules. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | + protected $sizeRules = [ |
|
42 | + 'Between', |
|
43 | + 'Max', |
|
44 | + 'Min', |
|
45 | + ]; |
|
46 | + |
|
47 | + /** |
|
48 | + * The validation rules that imply the field is required. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $implicitRules = [ |
|
53 | + 'Required', |
|
54 | + ]; |
|
55 | + |
|
56 | + /** |
|
57 | + * The numeric related validation rules. |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $numericRules = [ |
|
62 | + 'Numeric', |
|
63 | + ]; |
|
64 | + |
|
65 | + /** |
|
66 | + * Run the validator's rules against its data. |
|
67 | + * |
|
68 | + * @param mixed $data |
|
69 | + * |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + public function validate($data, array $rules = []) |
|
73 | + { |
|
74 | + $this->normalizeData($data); |
|
75 | + $this->setRules($rules); |
|
76 | + |
|
77 | + foreach ($this->rules as $attribute => $rules) { |
|
78 | + foreach ($rules as $rule) { |
|
79 | + $this->validateAttribute($rule, $attribute); |
|
80 | + |
|
81 | + if ($this->shouldStopValidating($attribute)) { |
|
82 | + break; |
|
83 | + } |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + return $this->errors; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Add an error message to the validator's collection of errors. |
|
92 | + * |
|
93 | + * @param string $attribute |
|
94 | + * @param string $rule |
|
95 | + * |
|
96 | + * @return void |
|
97 | + */ |
|
98 | + protected function addError($attribute, $rule, array $parameters) |
|
99 | + { |
|
100 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
101 | + |
|
102 | + $this->errors[$attribute]['errors'][] = $message; |
|
103 | + |
|
104 | + if (!isset($this->errors[$attribute]['value'])) { |
|
105 | + $this->errors[$attribute]['value'] = $this->getValue($attribute); |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Add a failed rule and error message to the collection. |
|
111 | + * |
|
112 | + * @param string $attribute |
|
113 | + * @param string $rule |
|
114 | + * |
|
115 | + * @return void |
|
116 | + */ |
|
117 | + protected function addFailure($attribute, $rule, array $parameters) |
|
118 | + { |
|
119 | + $this->addError($attribute, $rule, $parameters); |
|
120 | + |
|
121 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Get the data type of the given attribute. |
|
126 | + * |
|
127 | + * @param string $attribute |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + protected function getAttributeType($attribute) |
|
131 | + { |
|
132 | + return $this->hasRule($attribute, $this->numericRules) |
|
133 | + ? 'numeric' |
|
134 | + : 'string'; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Get the validation message for an attribute and rule. |
|
139 | + * |
|
140 | + * @param string $attribute |
|
141 | + * @param string $rule |
|
142 | + * |
|
143 | + * @return string|null |
|
144 | + */ |
|
145 | + protected function getMessage($attribute, $rule, array $parameters) |
|
146 | + { |
|
147 | + if (in_array($rule, $this->sizeRules)) { |
|
148 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
149 | + } |
|
150 | + |
|
151 | + $lowerRule = $this->snakeCase($rule); |
|
152 | + |
|
153 | + return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get a rule and its parameters for a given attribute. |
|
158 | + * |
|
159 | + * @param string $attribute |
|
160 | + * @param string|array $rules |
|
161 | + * |
|
162 | + * @return array|null |
|
163 | + */ |
|
164 | + protected function getRule($attribute, $rules) |
|
165 | + { |
|
166 | + if (!array_key_exists($attribute, $this->rules)) { |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + $rules = (array) $rules; |
|
171 | + |
|
172 | + foreach ($this->rules[$attribute] as $rule) { |
|
173 | + list($rule, $parameters) = $this->parseRule($rule); |
|
174 | + |
|
175 | + if (in_array($rule, $rules)) { |
|
176 | + return [$rule, $parameters]; |
|
177 | + } |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the size of an attribute. |
|
183 | + * |
|
184 | + * @param string $attribute |
|
185 | + * @param mixed $value |
|
186 | + * |
|
187 | + * @return mixed |
|
188 | + */ |
|
189 | + protected function getSize($attribute, $value) |
|
190 | + { |
|
191 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
192 | + |
|
193 | + if (is_numeric($value) && $hasNumeric) { |
|
194 | + return $value; |
|
195 | + } elseif (is_array($value)) { |
|
196 | + return count($value); |
|
197 | + } |
|
198 | + |
|
199 | + return mb_strlen($value); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Get the proper error message for an attribute and size rule. |
|
204 | + * |
|
205 | + * @param string $attribute |
|
206 | + * @param string $rule |
|
207 | + * |
|
208 | + * @return string|null |
|
209 | + */ |
|
210 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
211 | + { |
|
212 | + $lowerRule = $this->snakeCase($rule); |
|
213 | + $type = $this->getAttributeType($attribute); |
|
214 | + |
|
215 | + $lowerRule .= ".{$type}"; |
|
216 | + |
|
217 | + return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Get the value of a given attribute. |
|
222 | + * |
|
223 | + * @param string $attribute |
|
224 | + * |
|
225 | + * @return mixed |
|
226 | + */ |
|
227 | + protected function getValue($attribute) |
|
228 | + { |
|
229 | + if (isset($this->data[$attribute])) { |
|
230 | + return $this->data[$attribute]; |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Determine if the given attribute has a rule in the given set. |
|
236 | + * |
|
237 | + * @param string $attribute |
|
238 | + * @param string|array $rules |
|
239 | + * |
|
240 | + * @return bool |
|
241 | + */ |
|
242 | + protected function hasRule($attribute, $rules) |
|
243 | + { |
|
244 | + return !is_null($this->getRule($attribute, $rules)); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Normalize the provided data to an array. |
|
249 | + * |
|
250 | + * @param mixed $data |
|
251 | + * |
|
252 | + * @return $this |
|
253 | + */ |
|
254 | + protected function normalizeData($data) |
|
255 | + { |
|
256 | + // If an object was provided, get its public properties |
|
257 | + if (is_object($data)) { |
|
258 | + $this->data = get_object_vars($data); |
|
259 | + } else { |
|
260 | + $this->data = $data; |
|
261 | + } |
|
262 | + |
|
263 | + return $this; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Parse a parameter list. |
|
268 | + * |
|
269 | + * @param string $rule |
|
270 | + * @param string $parameter |
|
271 | + * |
|
272 | + * @return array |
|
273 | + */ |
|
274 | + protected function parseParameters($rule, $parameter) |
|
275 | + { |
|
276 | + if ('regex' == strtolower($rule)) { |
|
277 | + return [$parameter]; |
|
278 | + } |
|
279 | + |
|
280 | + return str_getcsv($parameter); |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Extract the rule name and parameters from a rule. |
|
285 | + * |
|
286 | + * @param string $rule |
|
287 | + * |
|
288 | + * @return array |
|
289 | + */ |
|
290 | + protected function parseRule($rule) |
|
291 | + { |
|
292 | + $parameters = []; |
|
293 | + |
|
294 | + // example: {rule}:{parameters} |
|
295 | + if (false !== strpos($rule, ':')) { |
|
296 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
297 | + |
|
298 | + // example: {parameter1,parameter2,...} |
|
299 | + $parameters = $this->parseParameters($rule, $parameter); |
|
300 | + } |
|
301 | + |
|
302 | + $rule = ucwords(str_replace(['-', '_'], ' ', trim($rule))); |
|
303 | + $rule = str_replace(' ', '', $rule); |
|
304 | + |
|
305 | + return [$rule, $parameters]; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Replace all placeholders for the between rule. |
|
310 | + * |
|
311 | + * @param string $message |
|
312 | + * |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + protected function replaceBetween($message, array $parameters) |
|
316 | + { |
|
317 | + return str_replace([':min', ':max'], $parameters, $message); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Replace all placeholders for the max rule. |
|
322 | + * |
|
323 | + * @param string $message |
|
324 | + * |
|
325 | + * @return string |
|
326 | + */ |
|
327 | + protected function replaceMax($message, array $parameters) |
|
328 | + { |
|
329 | + return str_replace(':max', $parameters[0], $message); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Replace all placeholders for the min rule. |
|
334 | + * |
|
335 | + * @param string $message |
|
336 | + * |
|
337 | + * @return string |
|
338 | + */ |
|
339 | + protected function replaceMin($message, array $parameters) |
|
340 | + { |
|
341 | + return str_replace(':min', $parameters[0], $message); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Require a certain number of parameters to be present. |
|
346 | + * |
|
347 | + * @param int $count |
|
348 | + * @param string $rule |
|
349 | + * |
|
350 | + * @return void |
|
351 | + * @throws InvalidArgumentException |
|
352 | + */ |
|
353 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
354 | + { |
|
355 | + if (count($parameters) < $count) { |
|
356 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
357 | + } |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * Set the validation rules. |
|
362 | + * |
|
363 | + * @return $this |
|
364 | + */ |
|
365 | + protected function setRules(array $rules) |
|
366 | + { |
|
367 | + foreach ($rules as $key => $rule) { |
|
368 | + $rules[$key] = is_string($rule) ? explode('|', $rule) : $rule; |
|
369 | + } |
|
370 | + |
|
371 | + $this->rules = $rules; |
|
372 | + |
|
373 | + return $this; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Check if we should stop further validations on a given attribute. |
|
378 | + * |
|
379 | + * @param string $attribute |
|
380 | + * |
|
381 | + * @return bool |
|
382 | + */ |
|
383 | + protected function shouldStopValidating($attribute) |
|
384 | + { |
|
385 | + return $this->hasRule($attribute, $this->implicitRules) |
|
386 | + && isset($this->failedRules[$attribute]) |
|
387 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Convert a string to snake case. |
|
392 | + * |
|
393 | + * @param string $string |
|
394 | + * |
|
395 | + * @return string |
|
396 | + */ |
|
397 | + protected function snakeCase($string) |
|
398 | + { |
|
399 | + if (!ctype_lower($string)) { |
|
400 | + $string = preg_replace('/\s+/u', '', $string); |
|
401 | + $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
402 | + $string = mb_strtolower($string, 'UTF-8'); |
|
403 | + } |
|
404 | + |
|
405 | + return $string; |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Returns a translated message for the attribute. |
|
410 | + * |
|
411 | + * @param string $key |
|
412 | + * @param string $rule |
|
413 | + * @param string $attribute |
|
414 | + * |
|
415 | + * @return string|null |
|
416 | + */ |
|
417 | + protected function translator($key, $rule, $attribute, array $parameters) |
|
418 | + { |
|
419 | + $strings = []; //glsr_resolve( 'Strings' )->validation(); |
|
420 | + |
|
421 | + $message = isset($strings[$key]) |
|
422 | + ? $strings[$key] |
|
423 | + : false; |
|
424 | + |
|
425 | + if (!$message) { |
|
426 | + return; |
|
427 | + } |
|
428 | + |
|
429 | + $message = str_replace(':attribute', $attribute, $message); |
|
430 | + |
|
431 | + if (method_exists($this, $replacer = "replace{$rule}")) { |
|
432 | + $message = $this->$replacer($message, $parameters); |
|
433 | + } |
|
434 | + |
|
435 | + return $message; |
|
436 | + } |
|
437 | + |
|
438 | + // Rules Validation |
|
439 | + // --------------------------------------------------------------------------------------------- |
|
440 | + |
|
441 | + /** |
|
442 | + * Validate that an attribute was "accepted". |
|
443 | + * |
|
444 | + * This validation rule implies the attribute is "required". |
|
445 | + * |
|
446 | + * @param mixed $value |
|
447 | + * |
|
448 | + * @return bool |
|
449 | + */ |
|
450 | + protected function validateAccepted($value) |
|
451 | + { |
|
452 | + $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
453 | + |
|
454 | + return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Validate a given attribute against a rule. |
|
459 | + * |
|
460 | + * @param string $rule |
|
461 | + * @param string $attribute |
|
462 | + * |
|
463 | + * @return void |
|
464 | + * @throws BadMethodCallException |
|
465 | + */ |
|
466 | + protected function validateAttribute($rule, $attribute) |
|
467 | + { |
|
468 | + list($rule, $parameters) = $this->parseRule($rule); |
|
469 | + |
|
470 | + if ('' == $rule) { |
|
471 | + return; |
|
472 | + } |
|
473 | + |
|
474 | + $method = "validate{$rule}"; |
|
475 | + |
|
476 | + if (!method_exists($this, $method)) { |
|
477 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
478 | + } |
|
479 | + |
|
480 | + if (!$this->$method($this->getValue($attribute), $attribute, $parameters)) { |
|
481 | + $this->addFailure($attribute, $rule, $parameters); |
|
482 | + } |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Validate the size of an attribute is between a set of values. |
|
487 | + * |
|
488 | + * @param mixed $value |
|
489 | + * @param string $attribute |
|
490 | + * |
|
491 | + * @return bool |
|
492 | + */ |
|
493 | + protected function validateBetween($value, $attribute, array $parameters) |
|
494 | + { |
|
495 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
496 | + |
|
497 | + $size = $this->getSize($attribute, $value); |
|
498 | + |
|
499 | + return $size >= $parameters[0] && $size <= $parameters[1]; |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Validate that an attribute is a valid e-mail address. |
|
504 | + * |
|
505 | + * @param mixed $value |
|
506 | + * |
|
507 | + * @return bool |
|
508 | + */ |
|
509 | + protected function validateEmail($value) |
|
510 | + { |
|
511 | + return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Validate the size of an attribute is less than a maximum value. |
|
516 | + * |
|
517 | + * @param mixed $value |
|
518 | + * @param string $attribute |
|
519 | + * |
|
520 | + * @return bool |
|
521 | + */ |
|
522 | + protected function validateMax($value, $attribute, array $parameters) |
|
523 | + { |
|
524 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
525 | + |
|
526 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Validate the size of an attribute is greater than a minimum value. |
|
531 | + * |
|
532 | + * @param mixed $value |
|
533 | + * @param string $attribute |
|
534 | + * |
|
535 | + * @return bool |
|
536 | + */ |
|
537 | + protected function validateMin($value, $attribute, array $parameters) |
|
538 | + { |
|
539 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
540 | + |
|
541 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Validate that an attribute is numeric. |
|
546 | + * |
|
547 | + * @param mixed $value |
|
548 | + * |
|
549 | + * @return bool |
|
550 | + */ |
|
551 | + protected function validateNumeric($value) |
|
552 | + { |
|
553 | + return is_numeric($value); |
|
554 | + } |
|
555 | + |
|
556 | + /** |
|
557 | + * Validate that a required attribute exists. |
|
558 | + * |
|
559 | + * @param mixed $value |
|
560 | + * |
|
561 | + * @return bool |
|
562 | + */ |
|
563 | + protected function validateRequired($value) |
|
564 | + { |
|
565 | + if (is_string($value)) { |
|
566 | + $value = trim($value); |
|
567 | + } |
|
568 | + return is_null($value) || empty($value) |
|
569 | + ? false |
|
570 | + : true; |
|
571 | + } |
|
572 | 572 | } |
@@ -69,16 +69,16 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return array |
71 | 71 | */ |
72 | - public function validate($data, array $rules = []) |
|
72 | + public function validate( $data, array $rules = [] ) |
|
73 | 73 | { |
74 | - $this->normalizeData($data); |
|
75 | - $this->setRules($rules); |
|
74 | + $this->normalizeData( $data ); |
|
75 | + $this->setRules( $rules ); |
|
76 | 76 | |
77 | - foreach ($this->rules as $attribute => $rules) { |
|
78 | - foreach ($rules as $rule) { |
|
79 | - $this->validateAttribute($rule, $attribute); |
|
77 | + foreach( $this->rules as $attribute => $rules ) { |
|
78 | + foreach( $rules as $rule ) { |
|
79 | + $this->validateAttribute( $rule, $attribute ); |
|
80 | 80 | |
81 | - if ($this->shouldStopValidating($attribute)) { |
|
81 | + if( $this->shouldStopValidating( $attribute ) ) { |
|
82 | 82 | break; |
83 | 83 | } |
84 | 84 | } |
@@ -95,14 +95,14 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @return void |
97 | 97 | */ |
98 | - protected function addError($attribute, $rule, array $parameters) |
|
98 | + protected function addError( $attribute, $rule, array $parameters ) |
|
99 | 99 | { |
100 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
100 | + $message = $this->getMessage( $attribute, $rule, $parameters ); |
|
101 | 101 | |
102 | 102 | $this->errors[$attribute]['errors'][] = $message; |
103 | 103 | |
104 | - if (!isset($this->errors[$attribute]['value'])) { |
|
105 | - $this->errors[$attribute]['value'] = $this->getValue($attribute); |
|
104 | + if( !isset( $this->errors[$attribute]['value'] ) ) { |
|
105 | + $this->errors[$attribute]['value'] = $this->getValue( $attribute ); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | |
@@ -114,9 +114,9 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @return void |
116 | 116 | */ |
117 | - protected function addFailure($attribute, $rule, array $parameters) |
|
117 | + protected function addFailure( $attribute, $rule, array $parameters ) |
|
118 | 118 | { |
119 | - $this->addError($attribute, $rule, $parameters); |
|
119 | + $this->addError( $attribute, $rule, $parameters ); |
|
120 | 120 | |
121 | 121 | $this->failedRules[$attribute][$rule] = $parameters; |
122 | 122 | } |
@@ -127,9 +127,9 @@ discard block |
||
127 | 127 | * @param string $attribute |
128 | 128 | * @return string |
129 | 129 | */ |
130 | - protected function getAttributeType($attribute) |
|
130 | + protected function getAttributeType( $attribute ) |
|
131 | 131 | { |
132 | - return $this->hasRule($attribute, $this->numericRules) |
|
132 | + return $this->hasRule( $attribute, $this->numericRules ) |
|
133 | 133 | ? 'numeric' |
134 | 134 | : 'string'; |
135 | 135 | } |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @return string|null |
144 | 144 | */ |
145 | - protected function getMessage($attribute, $rule, array $parameters) |
|
145 | + protected function getMessage( $attribute, $rule, array $parameters ) |
|
146 | 146 | { |
147 | - if (in_array($rule, $this->sizeRules)) { |
|
148 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
147 | + if( in_array( $rule, $this->sizeRules ) ) { |
|
148 | + return $this->getSizeMessage( $attribute, $rule, $parameters ); |
|
149 | 149 | } |
150 | 150 | |
151 | - $lowerRule = $this->snakeCase($rule); |
|
151 | + $lowerRule = $this->snakeCase( $rule ); |
|
152 | 152 | |
153 | - return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
153 | + return $this->translator( $lowerRule, $rule, $attribute, $parameters ); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
@@ -161,18 +161,18 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @return array|null |
163 | 163 | */ |
164 | - protected function getRule($attribute, $rules) |
|
164 | + protected function getRule( $attribute, $rules ) |
|
165 | 165 | { |
166 | - if (!array_key_exists($attribute, $this->rules)) { |
|
166 | + if( !array_key_exists( $attribute, $this->rules ) ) { |
|
167 | 167 | return; |
168 | 168 | } |
169 | 169 | |
170 | 170 | $rules = (array) $rules; |
171 | 171 | |
172 | - foreach ($this->rules[$attribute] as $rule) { |
|
173 | - list($rule, $parameters) = $this->parseRule($rule); |
|
172 | + foreach( $this->rules[$attribute] as $rule ) { |
|
173 | + list( $rule, $parameters ) = $this->parseRule( $rule ); |
|
174 | 174 | |
175 | - if (in_array($rule, $rules)) { |
|
175 | + if( in_array( $rule, $rules ) ) { |
|
176 | 176 | return [$rule, $parameters]; |
177 | 177 | } |
178 | 178 | } |
@@ -186,17 +186,17 @@ discard block |
||
186 | 186 | * |
187 | 187 | * @return mixed |
188 | 188 | */ |
189 | - protected function getSize($attribute, $value) |
|
189 | + protected function getSize( $attribute, $value ) |
|
190 | 190 | { |
191 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
191 | + $hasNumeric = $this->hasRule( $attribute, $this->numericRules ); |
|
192 | 192 | |
193 | - if (is_numeric($value) && $hasNumeric) { |
|
193 | + if( is_numeric( $value ) && $hasNumeric ) { |
|
194 | 194 | return $value; |
195 | - } elseif (is_array($value)) { |
|
196 | - return count($value); |
|
195 | + } elseif( is_array( $value ) ) { |
|
196 | + return count( $value ); |
|
197 | 197 | } |
198 | 198 | |
199 | - return mb_strlen($value); |
|
199 | + return mb_strlen( $value ); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
@@ -207,14 +207,14 @@ discard block |
||
207 | 207 | * |
208 | 208 | * @return string|null |
209 | 209 | */ |
210 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
210 | + protected function getSizeMessage( $attribute, $rule, array $parameters ) |
|
211 | 211 | { |
212 | - $lowerRule = $this->snakeCase($rule); |
|
213 | - $type = $this->getAttributeType($attribute); |
|
212 | + $lowerRule = $this->snakeCase( $rule ); |
|
213 | + $type = $this->getAttributeType( $attribute ); |
|
214 | 214 | |
215 | 215 | $lowerRule .= ".{$type}"; |
216 | 216 | |
217 | - return $this->translator($lowerRule, $rule, $attribute, $parameters); |
|
217 | + return $this->translator( $lowerRule, $rule, $attribute, $parameters ); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -224,9 +224,9 @@ discard block |
||
224 | 224 | * |
225 | 225 | * @return mixed |
226 | 226 | */ |
227 | - protected function getValue($attribute) |
|
227 | + protected function getValue( $attribute ) |
|
228 | 228 | { |
229 | - if (isset($this->data[$attribute])) { |
|
229 | + if( isset( $this->data[$attribute] ) ) { |
|
230 | 230 | return $this->data[$attribute]; |
231 | 231 | } |
232 | 232 | } |
@@ -239,9 +239,9 @@ discard block |
||
239 | 239 | * |
240 | 240 | * @return bool |
241 | 241 | */ |
242 | - protected function hasRule($attribute, $rules) |
|
242 | + protected function hasRule( $attribute, $rules ) |
|
243 | 243 | { |
244 | - return !is_null($this->getRule($attribute, $rules)); |
|
244 | + return !is_null( $this->getRule( $attribute, $rules ) ); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -251,11 +251,11 @@ discard block |
||
251 | 251 | * |
252 | 252 | * @return $this |
253 | 253 | */ |
254 | - protected function normalizeData($data) |
|
254 | + protected function normalizeData( $data ) |
|
255 | 255 | { |
256 | 256 | // If an object was provided, get its public properties |
257 | - if (is_object($data)) { |
|
258 | - $this->data = get_object_vars($data); |
|
257 | + if( is_object( $data ) ) { |
|
258 | + $this->data = get_object_vars( $data ); |
|
259 | 259 | } else { |
260 | 260 | $this->data = $data; |
261 | 261 | } |
@@ -271,13 +271,13 @@ discard block |
||
271 | 271 | * |
272 | 272 | * @return array |
273 | 273 | */ |
274 | - protected function parseParameters($rule, $parameter) |
|
274 | + protected function parseParameters( $rule, $parameter ) |
|
275 | 275 | { |
276 | - if ('regex' == strtolower($rule)) { |
|
276 | + if( 'regex' == strtolower( $rule ) ) { |
|
277 | 277 | return [$parameter]; |
278 | 278 | } |
279 | 279 | |
280 | - return str_getcsv($parameter); |
|
280 | + return str_getcsv( $parameter ); |
|
281 | 281 | } |
282 | 282 | |
283 | 283 | /** |
@@ -287,20 +287,20 @@ discard block |
||
287 | 287 | * |
288 | 288 | * @return array |
289 | 289 | */ |
290 | - protected function parseRule($rule) |
|
290 | + protected function parseRule( $rule ) |
|
291 | 291 | { |
292 | 292 | $parameters = []; |
293 | 293 | |
294 | 294 | // example: {rule}:{parameters} |
295 | - if (false !== strpos($rule, ':')) { |
|
296 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
295 | + if( false !== strpos( $rule, ':' ) ) { |
|
296 | + list( $rule, $parameter ) = explode( ':', $rule, 2 ); |
|
297 | 297 | |
298 | 298 | // example: {parameter1,parameter2,...} |
299 | - $parameters = $this->parseParameters($rule, $parameter); |
|
299 | + $parameters = $this->parseParameters( $rule, $parameter ); |
|
300 | 300 | } |
301 | 301 | |
302 | - $rule = ucwords(str_replace(['-', '_'], ' ', trim($rule))); |
|
303 | - $rule = str_replace(' ', '', $rule); |
|
302 | + $rule = ucwords( str_replace( ['-', '_'], ' ', trim( $rule ) ) ); |
|
303 | + $rule = str_replace( ' ', '', $rule ); |
|
304 | 304 | |
305 | 305 | return [$rule, $parameters]; |
306 | 306 | } |
@@ -312,9 +312,9 @@ discard block |
||
312 | 312 | * |
313 | 313 | * @return string |
314 | 314 | */ |
315 | - protected function replaceBetween($message, array $parameters) |
|
315 | + protected function replaceBetween( $message, array $parameters ) |
|
316 | 316 | { |
317 | - return str_replace([':min', ':max'], $parameters, $message); |
|
317 | + return str_replace( [':min', ':max'], $parameters, $message ); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | /** |
@@ -324,9 +324,9 @@ discard block |
||
324 | 324 | * |
325 | 325 | * @return string |
326 | 326 | */ |
327 | - protected function replaceMax($message, array $parameters) |
|
327 | + protected function replaceMax( $message, array $parameters ) |
|
328 | 328 | { |
329 | - return str_replace(':max', $parameters[0], $message); |
|
329 | + return str_replace( ':max', $parameters[0], $message ); |
|
330 | 330 | } |
331 | 331 | |
332 | 332 | /** |
@@ -336,9 +336,9 @@ discard block |
||
336 | 336 | * |
337 | 337 | * @return string |
338 | 338 | */ |
339 | - protected function replaceMin($message, array $parameters) |
|
339 | + protected function replaceMin( $message, array $parameters ) |
|
340 | 340 | { |
341 | - return str_replace(':min', $parameters[0], $message); |
|
341 | + return str_replace( ':min', $parameters[0], $message ); |
|
342 | 342 | } |
343 | 343 | |
344 | 344 | /** |
@@ -350,10 +350,10 @@ discard block |
||
350 | 350 | * @return void |
351 | 351 | * @throws InvalidArgumentException |
352 | 352 | */ |
353 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
353 | + protected function requireParameterCount( $count, array $parameters, $rule ) |
|
354 | 354 | { |
355 | - if (count($parameters) < $count) { |
|
356 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
355 | + if( count( $parameters ) < $count ) { |
|
356 | + throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
|
357 | 357 | } |
358 | 358 | } |
359 | 359 | |
@@ -362,10 +362,10 @@ discard block |
||
362 | 362 | * |
363 | 363 | * @return $this |
364 | 364 | */ |
365 | - protected function setRules(array $rules) |
|
365 | + protected function setRules( array $rules ) |
|
366 | 366 | { |
367 | - foreach ($rules as $key => $rule) { |
|
368 | - $rules[$key] = is_string($rule) ? explode('|', $rule) : $rule; |
|
367 | + foreach( $rules as $key => $rule ) { |
|
368 | + $rules[$key] = is_string( $rule ) ? explode( '|', $rule ) : $rule; |
|
369 | 369 | } |
370 | 370 | |
371 | 371 | $this->rules = $rules; |
@@ -380,11 +380,11 @@ discard block |
||
380 | 380 | * |
381 | 381 | * @return bool |
382 | 382 | */ |
383 | - protected function shouldStopValidating($attribute) |
|
383 | + protected function shouldStopValidating( $attribute ) |
|
384 | 384 | { |
385 | - return $this->hasRule($attribute, $this->implicitRules) |
|
386 | - && isset($this->failedRules[$attribute]) |
|
387 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
385 | + return $this->hasRule( $attribute, $this->implicitRules ) |
|
386 | + && isset( $this->failedRules[$attribute] ) |
|
387 | + && array_intersect( array_keys( $this->failedRules[$attribute] ), $this->implicitRules ); |
|
388 | 388 | } |
389 | 389 | |
390 | 390 | /** |
@@ -394,12 +394,12 @@ discard block |
||
394 | 394 | * |
395 | 395 | * @return string |
396 | 396 | */ |
397 | - protected function snakeCase($string) |
|
397 | + protected function snakeCase( $string ) |
|
398 | 398 | { |
399 | - if (!ctype_lower($string)) { |
|
400 | - $string = preg_replace('/\s+/u', '', $string); |
|
401 | - $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
402 | - $string = mb_strtolower($string, 'UTF-8'); |
|
399 | + if( !ctype_lower( $string ) ) { |
|
400 | + $string = preg_replace( '/\s+/u', '', $string ); |
|
401 | + $string = preg_replace( '/(.)(?=[A-Z])/u', '$1_', $string ); |
|
402 | + $string = mb_strtolower( $string, 'UTF-8' ); |
|
403 | 403 | } |
404 | 404 | |
405 | 405 | return $string; |
@@ -414,22 +414,22 @@ discard block |
||
414 | 414 | * |
415 | 415 | * @return string|null |
416 | 416 | */ |
417 | - protected function translator($key, $rule, $attribute, array $parameters) |
|
417 | + protected function translator( $key, $rule, $attribute, array $parameters ) |
|
418 | 418 | { |
419 | 419 | $strings = []; //glsr_resolve( 'Strings' )->validation(); |
420 | 420 | |
421 | - $message = isset($strings[$key]) |
|
421 | + $message = isset( $strings[$key] ) |
|
422 | 422 | ? $strings[$key] |
423 | 423 | : false; |
424 | 424 | |
425 | - if (!$message) { |
|
425 | + if( !$message ) { |
|
426 | 426 | return; |
427 | 427 | } |
428 | 428 | |
429 | - $message = str_replace(':attribute', $attribute, $message); |
|
429 | + $message = str_replace( ':attribute', $attribute, $message ); |
|
430 | 430 | |
431 | - if (method_exists($this, $replacer = "replace{$rule}")) { |
|
432 | - $message = $this->$replacer($message, $parameters); |
|
431 | + if( method_exists( $this, $replacer = "replace{$rule}" ) ) { |
|
432 | + $message = $this->$replacer( $message, $parameters ); |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | return $message; |
@@ -447,11 +447,11 @@ discard block |
||
447 | 447 | * |
448 | 448 | * @return bool |
449 | 449 | */ |
450 | - protected function validateAccepted($value) |
|
450 | + protected function validateAccepted( $value ) |
|
451 | 451 | { |
452 | 452 | $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
453 | 453 | |
454 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
454 | + return $this->validateRequired( $value ) && in_array( $value, $acceptable, true ); |
|
455 | 455 | } |
456 | 456 | |
457 | 457 | /** |
@@ -463,22 +463,22 @@ discard block |
||
463 | 463 | * @return void |
464 | 464 | * @throws BadMethodCallException |
465 | 465 | */ |
466 | - protected function validateAttribute($rule, $attribute) |
|
466 | + protected function validateAttribute( $rule, $attribute ) |
|
467 | 467 | { |
468 | - list($rule, $parameters) = $this->parseRule($rule); |
|
468 | + list( $rule, $parameters ) = $this->parseRule( $rule ); |
|
469 | 469 | |
470 | - if ('' == $rule) { |
|
470 | + if( '' == $rule ) { |
|
471 | 471 | return; |
472 | 472 | } |
473 | 473 | |
474 | 474 | $method = "validate{$rule}"; |
475 | 475 | |
476 | - if (!method_exists($this, $method)) { |
|
477 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
476 | + if( !method_exists( $this, $method ) ) { |
|
477 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
478 | 478 | } |
479 | 479 | |
480 | - if (!$this->$method($this->getValue($attribute), $attribute, $parameters)) { |
|
481 | - $this->addFailure($attribute, $rule, $parameters); |
|
480 | + if( !$this->$method( $this->getValue( $attribute ), $attribute, $parameters ) ) { |
|
481 | + $this->addFailure( $attribute, $rule, $parameters ); |
|
482 | 482 | } |
483 | 483 | } |
484 | 484 | |
@@ -490,11 +490,11 @@ discard block |
||
490 | 490 | * |
491 | 491 | * @return bool |
492 | 492 | */ |
493 | - protected function validateBetween($value, $attribute, array $parameters) |
|
493 | + protected function validateBetween( $value, $attribute, array $parameters ) |
|
494 | 494 | { |
495 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
495 | + $this->requireParameterCount( 2, $parameters, 'between' ); |
|
496 | 496 | |
497 | - $size = $this->getSize($attribute, $value); |
|
497 | + $size = $this->getSize( $attribute, $value ); |
|
498 | 498 | |
499 | 499 | return $size >= $parameters[0] && $size <= $parameters[1]; |
500 | 500 | } |
@@ -506,9 +506,9 @@ discard block |
||
506 | 506 | * |
507 | 507 | * @return bool |
508 | 508 | */ |
509 | - protected function validateEmail($value) |
|
509 | + protected function validateEmail( $value ) |
|
510 | 510 | { |
511 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
511 | + return false !== filter_var( $value, FILTER_VALIDATE_EMAIL ); |
|
512 | 512 | } |
513 | 513 | |
514 | 514 | /** |
@@ -519,11 +519,11 @@ discard block |
||
519 | 519 | * |
520 | 520 | * @return bool |
521 | 521 | */ |
522 | - protected function validateMax($value, $attribute, array $parameters) |
|
522 | + protected function validateMax( $value, $attribute, array $parameters ) |
|
523 | 523 | { |
524 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
524 | + $this->requireParameterCount( 1, $parameters, 'max' ); |
|
525 | 525 | |
526 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
526 | + return $this->getSize( $attribute, $value ) <= $parameters[0]; |
|
527 | 527 | } |
528 | 528 | |
529 | 529 | /** |
@@ -534,11 +534,11 @@ discard block |
||
534 | 534 | * |
535 | 535 | * @return bool |
536 | 536 | */ |
537 | - protected function validateMin($value, $attribute, array $parameters) |
|
537 | + protected function validateMin( $value, $attribute, array $parameters ) |
|
538 | 538 | { |
539 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
539 | + $this->requireParameterCount( 1, $parameters, 'min' ); |
|
540 | 540 | |
541 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
541 | + return $this->getSize( $attribute, $value ) >= $parameters[0]; |
|
542 | 542 | } |
543 | 543 | |
544 | 544 | /** |
@@ -548,9 +548,9 @@ discard block |
||
548 | 548 | * |
549 | 549 | * @return bool |
550 | 550 | */ |
551 | - protected function validateNumeric($value) |
|
551 | + protected function validateNumeric( $value ) |
|
552 | 552 | { |
553 | - return is_numeric($value); |
|
553 | + return is_numeric( $value ); |
|
554 | 554 | } |
555 | 555 | |
556 | 556 | /** |
@@ -560,12 +560,12 @@ discard block |
||
560 | 560 | * |
561 | 561 | * @return bool |
562 | 562 | */ |
563 | - protected function validateRequired($value) |
|
563 | + protected function validateRequired( $value ) |
|
564 | 564 | { |
565 | - if (is_string($value)) { |
|
566 | - $value = trim($value); |
|
565 | + if( is_string( $value ) ) { |
|
566 | + $value = trim( $value ); |
|
567 | 567 | } |
568 | - return is_null($value) || empty($value) |
|
568 | + return is_null( $value ) || empty( $value ) |
|
569 | 569 | ? false |
570 | 570 | : true; |
571 | 571 | } |
@@ -5,8 +5,7 @@ discard block |
||
5 | 5 | use BadMethodCallException; |
6 | 6 | use InvalidArgumentException; |
7 | 7 | |
8 | -class Validator |
|
9 | -{ |
|
8 | +class Validator { |
|
10 | 9 | /** |
11 | 10 | * @var array |
12 | 11 | */ |
@@ -192,7 +191,8 @@ discard block |
||
192 | 191 | |
193 | 192 | if (is_numeric($value) && $hasNumeric) { |
194 | 193 | return $value; |
195 | - } elseif (is_array($value)) { |
|
194 | + } |
|
195 | + elseif (is_array($value)) { |
|
196 | 196 | return count($value); |
197 | 197 | } |
198 | 198 | |
@@ -256,7 +256,8 @@ discard block |
||
256 | 256 | // If an object was provided, get its public properties |
257 | 257 | if (is_object($data)) { |
258 | 258 | $this->data = get_object_vars($data); |
259 | - } else { |
|
259 | + } |
|
260 | + else { |
|
260 | 261 | $this->data = $data; |
261 | 262 | } |
262 | 263 |
@@ -4,231 +4,231 @@ |
||
4 | 4 | |
5 | 5 | class Normalizer |
6 | 6 | { |
7 | - const BOOLEAN_ATTRIBUTES = [ |
|
8 | - 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
9 | - 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
10 | - 'webkitdirectory', |
|
11 | - ]; |
|
12 | - |
|
13 | - const FORM_ATTRIBUTES = [ |
|
14 | - 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', |
|
15 | - 'method', 'name', 'novalidate', 'target', |
|
16 | - ]; |
|
17 | - |
|
18 | - const GLOBAL_ATTRIBUTES = [ |
|
19 | - 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
20 | - 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
21 | - ]; |
|
22 | - |
|
23 | - const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
24 | - 'aria-', 'data-', 'item', 'on', |
|
25 | - ]; |
|
26 | - |
|
27 | - const INPUT_ATTRIBUTES = [ |
|
28 | - 'accept', 'autocapitalize', 'autocomplete', 'autocorrect', 'autofocus', 'capture', |
|
29 | - 'checked', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
30 | - 'formnovalidate', 'formtarget', 'height', 'incremental', 'inputmode', 'list', 'max', |
|
31 | - 'maxlength', 'min', 'minlength', 'mozactionhint', 'multiple', 'name', 'pattern', |
|
32 | - 'placeholder', 'readonly', 'required', 'results', 'selectionDirection', 'size', 'src', |
|
33 | - 'step', 'type', 'value', 'webkitdirectory', 'width', 'x-moz-errormessage', |
|
34 | - ]; |
|
35 | - |
|
36 | - const INPUT_TYPES = [ |
|
37 | - 'button', 'checkbox', 'color', 'date', 'datetime', 'datetime-local', 'email', 'file', |
|
38 | - 'hidden', 'image', 'max', 'min', 'month', 'number', 'password', 'radio', 'range', |
|
39 | - 'reset', 'search', 'step', 'submit', 'tel', 'text', 'time', 'url', 'value', 'week', |
|
40 | - ]; |
|
41 | - |
|
42 | - const SELECT_ATTRIBUTES = [ |
|
43 | - 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
44 | - ]; |
|
45 | - |
|
46 | - const TEXTAREA_ATTRIBUTES = [ |
|
47 | - 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
48 | - 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', |
|
49 | - 'selectionDirection', 'selectionEnd', 'selectionStart', 'wrap', |
|
50 | - ]; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - protected $args; |
|
56 | - |
|
57 | - public function __construct() |
|
58 | - { |
|
59 | - $this->args = []; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Normalize form attributes. |
|
64 | - * |
|
65 | - * @return array|string |
|
66 | - */ |
|
67 | - public function form(array $args = [], $implode = false) |
|
68 | - { |
|
69 | - $attributes = $this->parseAttributes(self::FORM_ATTRIBUTES, $args); |
|
70 | - |
|
71 | - return $this->maybeImplode($attributes, $implode); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Normalize input attributes. |
|
76 | - * |
|
77 | - * @return array|string |
|
78 | - */ |
|
79 | - public function input(array $args = [], $implode = false) |
|
80 | - { |
|
81 | - $this->filterInputType(); |
|
82 | - |
|
83 | - $attributes = $this->parseAttributes(self::INPUT_ATTRIBUTES, $args); |
|
84 | - |
|
85 | - return $this->maybeImplode($attributes, $implode); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Possibly implode attributes into a string. |
|
90 | - * |
|
91 | - * @param bool|string $implode |
|
92 | - * |
|
93 | - * @return array|string |
|
94 | - */ |
|
95 | - public function maybeImplode(array $attributes, $implode = true) |
|
96 | - { |
|
97 | - if (!$implode || 'implode' !== $implode) { |
|
98 | - return $attributes; |
|
99 | - } |
|
100 | - $results = []; |
|
101 | - foreach ($attributes as $key => $value) { |
|
102 | - // if data attributes, use single quotes in case of json encoded values |
|
103 | - $quotes = false !== stripos($key, 'data-') ? "'" : '"'; |
|
104 | - if (is_array($value)) { |
|
105 | - $value = json_encode($value); |
|
106 | - $quotes = "'"; |
|
107 | - } |
|
108 | - $results[] = is_string($key) |
|
109 | - ? sprintf('%1$s=%3$s%2$s%3$s', $key, $value, $quotes) |
|
110 | - : $value; |
|
111 | - } |
|
112 | - return implode(' ', $results); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Normalize select attributes. |
|
117 | - * |
|
118 | - * @return array|string |
|
119 | - */ |
|
120 | - public function select(array $args = [], $implode = false) |
|
121 | - { |
|
122 | - $attributes = $this->parseAttributes(self::SELECT_ATTRIBUTES, $args); |
|
123 | - |
|
124 | - return $this->maybeImplode($attributes, $implode); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Normalize textarea attributes. |
|
129 | - * |
|
130 | - * @return array|string |
|
131 | - */ |
|
132 | - public function textarea(array $args = [], $implode = false) |
|
133 | - { |
|
134 | - $attributes = $this->parseAttributes(self::TEXTAREA_ATTRIBUTES, $args); |
|
135 | - |
|
136 | - return $this->maybeImplode($attributes, $implode); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Filter attributes by the provided attrribute keys and remove any non-boolean keys |
|
141 | - * with empty values. |
|
142 | - * |
|
143 | - * @return array |
|
144 | - */ |
|
145 | - protected function filterAttributes(array $attributeKeys) |
|
146 | - { |
|
147 | - $filtered = array_intersect_key($this->args, array_flip($attributeKeys)); |
|
148 | - |
|
149 | - // normalize truthy boolean attributes |
|
150 | - foreach ($filtered as $key => $value) { |
|
151 | - if (!in_array($key, self::BOOLEAN_ATTRIBUTES)) { |
|
152 | - continue; |
|
153 | - } |
|
154 | - |
|
155 | - if (false !== $value) { |
|
156 | - $filtered[$key] = ''; |
|
157 | - continue; |
|
158 | - } |
|
159 | - |
|
160 | - unset($filtered[$key]); |
|
161 | - } |
|
162 | - |
|
163 | - $filteredKeys = array_filter(array_keys($filtered), function ($key) use ($filtered) { |
|
164 | - return !( |
|
165 | - empty($filtered[$key]) |
|
166 | - && !is_numeric($filtered[$key]) |
|
167 | - && !in_array($key, self::BOOLEAN_ATTRIBUTES) |
|
168 | - ); |
|
169 | - }); |
|
170 | - |
|
171 | - return array_intersect_key($filtered, array_flip($filteredKeys)); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @return array |
|
176 | - */ |
|
177 | - protected function filterGlobalAttributes() |
|
178 | - { |
|
179 | - $global = $this->filterAttributes(self::GLOBAL_ATTRIBUTES); |
|
180 | - |
|
181 | - $wildcards = []; |
|
182 | - |
|
183 | - foreach (self::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
184 | - foreach ($this->args as $key => $value) { |
|
185 | - $length = strlen($wildcard); |
|
186 | - $result = substr($key, 0, $length) === $wildcard; |
|
187 | - |
|
188 | - if ($result) { |
|
189 | - // only allow data attributes to have an empty value |
|
190 | - if ('data-' != $wildcard && empty($value)) { |
|
191 | - continue; |
|
192 | - } |
|
193 | - |
|
194 | - if (is_array($value)) { |
|
195 | - if ('data-' != $wildcard) { |
|
196 | - continue; |
|
197 | - } |
|
198 | - |
|
199 | - $value = json_encode($value); |
|
200 | - } |
|
201 | - |
|
202 | - $wildcards[$key] = $value; |
|
203 | - } |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - return array_merge($global, $wildcards); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - protected function filterInputType() |
|
214 | - { |
|
215 | - if (!isset($this->args['type']) || !in_array($this->args['type'], self::INPUT_TYPES)) { |
|
216 | - $this->args['type'] = 'text'; |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @return array |
|
222 | - */ |
|
223 | - protected function parseAttributes(array $attributes, array $args = []) |
|
224 | - { |
|
225 | - if (!empty($args)) { |
|
226 | - $this->args = array_change_key_case($args); |
|
227 | - } |
|
228 | - |
|
229 | - $global = $this->filterGlobalAttributes(); |
|
230 | - $local = $this->filterAttributes($attributes); |
|
231 | - |
|
232 | - return array_merge($global, $local); |
|
233 | - } |
|
7 | + const BOOLEAN_ATTRIBUTES = [ |
|
8 | + 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
9 | + 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
10 | + 'webkitdirectory', |
|
11 | + ]; |
|
12 | + |
|
13 | + const FORM_ATTRIBUTES = [ |
|
14 | + 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', |
|
15 | + 'method', 'name', 'novalidate', 'target', |
|
16 | + ]; |
|
17 | + |
|
18 | + const GLOBAL_ATTRIBUTES = [ |
|
19 | + 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
20 | + 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
21 | + ]; |
|
22 | + |
|
23 | + const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
24 | + 'aria-', 'data-', 'item', 'on', |
|
25 | + ]; |
|
26 | + |
|
27 | + const INPUT_ATTRIBUTES = [ |
|
28 | + 'accept', 'autocapitalize', 'autocomplete', 'autocorrect', 'autofocus', 'capture', |
|
29 | + 'checked', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
30 | + 'formnovalidate', 'formtarget', 'height', 'incremental', 'inputmode', 'list', 'max', |
|
31 | + 'maxlength', 'min', 'minlength', 'mozactionhint', 'multiple', 'name', 'pattern', |
|
32 | + 'placeholder', 'readonly', 'required', 'results', 'selectionDirection', 'size', 'src', |
|
33 | + 'step', 'type', 'value', 'webkitdirectory', 'width', 'x-moz-errormessage', |
|
34 | + ]; |
|
35 | + |
|
36 | + const INPUT_TYPES = [ |
|
37 | + 'button', 'checkbox', 'color', 'date', 'datetime', 'datetime-local', 'email', 'file', |
|
38 | + 'hidden', 'image', 'max', 'min', 'month', 'number', 'password', 'radio', 'range', |
|
39 | + 'reset', 'search', 'step', 'submit', 'tel', 'text', 'time', 'url', 'value', 'week', |
|
40 | + ]; |
|
41 | + |
|
42 | + const SELECT_ATTRIBUTES = [ |
|
43 | + 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
44 | + ]; |
|
45 | + |
|
46 | + const TEXTAREA_ATTRIBUTES = [ |
|
47 | + 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
48 | + 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', |
|
49 | + 'selectionDirection', 'selectionEnd', 'selectionStart', 'wrap', |
|
50 | + ]; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + protected $args; |
|
56 | + |
|
57 | + public function __construct() |
|
58 | + { |
|
59 | + $this->args = []; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Normalize form attributes. |
|
64 | + * |
|
65 | + * @return array|string |
|
66 | + */ |
|
67 | + public function form(array $args = [], $implode = false) |
|
68 | + { |
|
69 | + $attributes = $this->parseAttributes(self::FORM_ATTRIBUTES, $args); |
|
70 | + |
|
71 | + return $this->maybeImplode($attributes, $implode); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Normalize input attributes. |
|
76 | + * |
|
77 | + * @return array|string |
|
78 | + */ |
|
79 | + public function input(array $args = [], $implode = false) |
|
80 | + { |
|
81 | + $this->filterInputType(); |
|
82 | + |
|
83 | + $attributes = $this->parseAttributes(self::INPUT_ATTRIBUTES, $args); |
|
84 | + |
|
85 | + return $this->maybeImplode($attributes, $implode); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Possibly implode attributes into a string. |
|
90 | + * |
|
91 | + * @param bool|string $implode |
|
92 | + * |
|
93 | + * @return array|string |
|
94 | + */ |
|
95 | + public function maybeImplode(array $attributes, $implode = true) |
|
96 | + { |
|
97 | + if (!$implode || 'implode' !== $implode) { |
|
98 | + return $attributes; |
|
99 | + } |
|
100 | + $results = []; |
|
101 | + foreach ($attributes as $key => $value) { |
|
102 | + // if data attributes, use single quotes in case of json encoded values |
|
103 | + $quotes = false !== stripos($key, 'data-') ? "'" : '"'; |
|
104 | + if (is_array($value)) { |
|
105 | + $value = json_encode($value); |
|
106 | + $quotes = "'"; |
|
107 | + } |
|
108 | + $results[] = is_string($key) |
|
109 | + ? sprintf('%1$s=%3$s%2$s%3$s', $key, $value, $quotes) |
|
110 | + : $value; |
|
111 | + } |
|
112 | + return implode(' ', $results); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Normalize select attributes. |
|
117 | + * |
|
118 | + * @return array|string |
|
119 | + */ |
|
120 | + public function select(array $args = [], $implode = false) |
|
121 | + { |
|
122 | + $attributes = $this->parseAttributes(self::SELECT_ATTRIBUTES, $args); |
|
123 | + |
|
124 | + return $this->maybeImplode($attributes, $implode); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Normalize textarea attributes. |
|
129 | + * |
|
130 | + * @return array|string |
|
131 | + */ |
|
132 | + public function textarea(array $args = [], $implode = false) |
|
133 | + { |
|
134 | + $attributes = $this->parseAttributes(self::TEXTAREA_ATTRIBUTES, $args); |
|
135 | + |
|
136 | + return $this->maybeImplode($attributes, $implode); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Filter attributes by the provided attrribute keys and remove any non-boolean keys |
|
141 | + * with empty values. |
|
142 | + * |
|
143 | + * @return array |
|
144 | + */ |
|
145 | + protected function filterAttributes(array $attributeKeys) |
|
146 | + { |
|
147 | + $filtered = array_intersect_key($this->args, array_flip($attributeKeys)); |
|
148 | + |
|
149 | + // normalize truthy boolean attributes |
|
150 | + foreach ($filtered as $key => $value) { |
|
151 | + if (!in_array($key, self::BOOLEAN_ATTRIBUTES)) { |
|
152 | + continue; |
|
153 | + } |
|
154 | + |
|
155 | + if (false !== $value) { |
|
156 | + $filtered[$key] = ''; |
|
157 | + continue; |
|
158 | + } |
|
159 | + |
|
160 | + unset($filtered[$key]); |
|
161 | + } |
|
162 | + |
|
163 | + $filteredKeys = array_filter(array_keys($filtered), function ($key) use ($filtered) { |
|
164 | + return !( |
|
165 | + empty($filtered[$key]) |
|
166 | + && !is_numeric($filtered[$key]) |
|
167 | + && !in_array($key, self::BOOLEAN_ATTRIBUTES) |
|
168 | + ); |
|
169 | + }); |
|
170 | + |
|
171 | + return array_intersect_key($filtered, array_flip($filteredKeys)); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @return array |
|
176 | + */ |
|
177 | + protected function filterGlobalAttributes() |
|
178 | + { |
|
179 | + $global = $this->filterAttributes(self::GLOBAL_ATTRIBUTES); |
|
180 | + |
|
181 | + $wildcards = []; |
|
182 | + |
|
183 | + foreach (self::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
184 | + foreach ($this->args as $key => $value) { |
|
185 | + $length = strlen($wildcard); |
|
186 | + $result = substr($key, 0, $length) === $wildcard; |
|
187 | + |
|
188 | + if ($result) { |
|
189 | + // only allow data attributes to have an empty value |
|
190 | + if ('data-' != $wildcard && empty($value)) { |
|
191 | + continue; |
|
192 | + } |
|
193 | + |
|
194 | + if (is_array($value)) { |
|
195 | + if ('data-' != $wildcard) { |
|
196 | + continue; |
|
197 | + } |
|
198 | + |
|
199 | + $value = json_encode($value); |
|
200 | + } |
|
201 | + |
|
202 | + $wildcards[$key] = $value; |
|
203 | + } |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + return array_merge($global, $wildcards); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + protected function filterInputType() |
|
214 | + { |
|
215 | + if (!isset($this->args['type']) || !in_array($this->args['type'], self::INPUT_TYPES)) { |
|
216 | + $this->args['type'] = 'text'; |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @return array |
|
222 | + */ |
|
223 | + protected function parseAttributes(array $attributes, array $args = []) |
|
224 | + { |
|
225 | + if (!empty($args)) { |
|
226 | + $this->args = array_change_key_case($args); |
|
227 | + } |
|
228 | + |
|
229 | + $global = $this->filterGlobalAttributes(); |
|
230 | + $local = $this->filterAttributes($attributes); |
|
231 | + |
|
232 | + return array_merge($global, $local); |
|
233 | + } |
|
234 | 234 | } |
@@ -64,11 +64,11 @@ discard block |
||
64 | 64 | * |
65 | 65 | * @return array|string |
66 | 66 | */ |
67 | - public function form(array $args = [], $implode = false) |
|
67 | + public function form( array $args = [], $implode = false ) |
|
68 | 68 | { |
69 | - $attributes = $this->parseAttributes(self::FORM_ATTRIBUTES, $args); |
|
69 | + $attributes = $this->parseAttributes( self::FORM_ATTRIBUTES, $args ); |
|
70 | 70 | |
71 | - return $this->maybeImplode($attributes, $implode); |
|
71 | + return $this->maybeImplode( $attributes, $implode ); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -76,13 +76,13 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return array|string |
78 | 78 | */ |
79 | - public function input(array $args = [], $implode = false) |
|
79 | + public function input( array $args = [], $implode = false ) |
|
80 | 80 | { |
81 | 81 | $this->filterInputType(); |
82 | 82 | |
83 | - $attributes = $this->parseAttributes(self::INPUT_ATTRIBUTES, $args); |
|
83 | + $attributes = $this->parseAttributes( self::INPUT_ATTRIBUTES, $args ); |
|
84 | 84 | |
85 | - return $this->maybeImplode($attributes, $implode); |
|
85 | + return $this->maybeImplode( $attributes, $implode ); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -92,24 +92,24 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @return array|string |
94 | 94 | */ |
95 | - public function maybeImplode(array $attributes, $implode = true) |
|
95 | + public function maybeImplode( array $attributes, $implode = true ) |
|
96 | 96 | { |
97 | - if (!$implode || 'implode' !== $implode) { |
|
97 | + if( !$implode || 'implode' !== $implode ) { |
|
98 | 98 | return $attributes; |
99 | 99 | } |
100 | 100 | $results = []; |
101 | - foreach ($attributes as $key => $value) { |
|
101 | + foreach( $attributes as $key => $value ) { |
|
102 | 102 | // if data attributes, use single quotes in case of json encoded values |
103 | - $quotes = false !== stripos($key, 'data-') ? "'" : '"'; |
|
104 | - if (is_array($value)) { |
|
105 | - $value = json_encode($value); |
|
103 | + $quotes = false !== stripos( $key, 'data-' ) ? "'" : '"'; |
|
104 | + if( is_array( $value ) ) { |
|
105 | + $value = json_encode( $value ); |
|
106 | 106 | $quotes = "'"; |
107 | 107 | } |
108 | - $results[] = is_string($key) |
|
109 | - ? sprintf('%1$s=%3$s%2$s%3$s', $key, $value, $quotes) |
|
108 | + $results[] = is_string( $key ) |
|
109 | + ? sprintf( '%1$s=%3$s%2$s%3$s', $key, $value, $quotes ) |
|
110 | 110 | : $value; |
111 | 111 | } |
112 | - return implode(' ', $results); |
|
112 | + return implode( ' ', $results ); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -117,11 +117,11 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return array|string |
119 | 119 | */ |
120 | - public function select(array $args = [], $implode = false) |
|
120 | + public function select( array $args = [], $implode = false ) |
|
121 | 121 | { |
122 | - $attributes = $this->parseAttributes(self::SELECT_ATTRIBUTES, $args); |
|
122 | + $attributes = $this->parseAttributes( self::SELECT_ATTRIBUTES, $args ); |
|
123 | 123 | |
124 | - return $this->maybeImplode($attributes, $implode); |
|
124 | + return $this->maybeImplode( $attributes, $implode ); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
@@ -129,11 +129,11 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @return array|string |
131 | 131 | */ |
132 | - public function textarea(array $args = [], $implode = false) |
|
132 | + public function textarea( array $args = [], $implode = false ) |
|
133 | 133 | { |
134 | - $attributes = $this->parseAttributes(self::TEXTAREA_ATTRIBUTES, $args); |
|
134 | + $attributes = $this->parseAttributes( self::TEXTAREA_ATTRIBUTES, $args ); |
|
135 | 135 | |
136 | - return $this->maybeImplode($attributes, $implode); |
|
136 | + return $this->maybeImplode( $attributes, $implode ); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | /** |
@@ -142,33 +142,33 @@ discard block |
||
142 | 142 | * |
143 | 143 | * @return array |
144 | 144 | */ |
145 | - protected function filterAttributes(array $attributeKeys) |
|
145 | + protected function filterAttributes( array $attributeKeys ) |
|
146 | 146 | { |
147 | - $filtered = array_intersect_key($this->args, array_flip($attributeKeys)); |
|
147 | + $filtered = array_intersect_key( $this->args, array_flip( $attributeKeys ) ); |
|
148 | 148 | |
149 | 149 | // normalize truthy boolean attributes |
150 | - foreach ($filtered as $key => $value) { |
|
151 | - if (!in_array($key, self::BOOLEAN_ATTRIBUTES)) { |
|
150 | + foreach( $filtered as $key => $value ) { |
|
151 | + if( !in_array( $key, self::BOOLEAN_ATTRIBUTES ) ) { |
|
152 | 152 | continue; |
153 | 153 | } |
154 | 154 | |
155 | - if (false !== $value) { |
|
155 | + if( false !== $value ) { |
|
156 | 156 | $filtered[$key] = ''; |
157 | 157 | continue; |
158 | 158 | } |
159 | 159 | |
160 | - unset($filtered[$key]); |
|
160 | + unset( $filtered[$key] ); |
|
161 | 161 | } |
162 | 162 | |
163 | - $filteredKeys = array_filter(array_keys($filtered), function ($key) use ($filtered) { |
|
163 | + $filteredKeys = array_filter( array_keys( $filtered ), function( $key ) use ( $filtered ) { |
|
164 | 164 | return !( |
165 | - empty($filtered[$key]) |
|
166 | - && !is_numeric($filtered[$key]) |
|
167 | - && !in_array($key, self::BOOLEAN_ATTRIBUTES) |
|
165 | + empty( $filtered[$key] ) |
|
166 | + && !is_numeric( $filtered[$key] ) |
|
167 | + && !in_array( $key, self::BOOLEAN_ATTRIBUTES ) |
|
168 | 168 | ); |
169 | 169 | }); |
170 | 170 | |
171 | - return array_intersect_key($filtered, array_flip($filteredKeys)); |
|
171 | + return array_intersect_key( $filtered, array_flip( $filteredKeys ) ); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
@@ -176,27 +176,27 @@ discard block |
||
176 | 176 | */ |
177 | 177 | protected function filterGlobalAttributes() |
178 | 178 | { |
179 | - $global = $this->filterAttributes(self::GLOBAL_ATTRIBUTES); |
|
179 | + $global = $this->filterAttributes( self::GLOBAL_ATTRIBUTES ); |
|
180 | 180 | |
181 | 181 | $wildcards = []; |
182 | 182 | |
183 | - foreach (self::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
184 | - foreach ($this->args as $key => $value) { |
|
185 | - $length = strlen($wildcard); |
|
186 | - $result = substr($key, 0, $length) === $wildcard; |
|
183 | + foreach( self::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard ) { |
|
184 | + foreach( $this->args as $key => $value ) { |
|
185 | + $length = strlen( $wildcard ); |
|
186 | + $result = substr( $key, 0, $length ) === $wildcard; |
|
187 | 187 | |
188 | - if ($result) { |
|
188 | + if( $result ) { |
|
189 | 189 | // only allow data attributes to have an empty value |
190 | - if ('data-' != $wildcard && empty($value)) { |
|
190 | + if( 'data-' != $wildcard && empty( $value ) ) { |
|
191 | 191 | continue; |
192 | 192 | } |
193 | 193 | |
194 | - if (is_array($value)) { |
|
195 | - if ('data-' != $wildcard) { |
|
194 | + if( is_array( $value ) ) { |
|
195 | + if( 'data-' != $wildcard ) { |
|
196 | 196 | continue; |
197 | 197 | } |
198 | 198 | |
199 | - $value = json_encode($value); |
|
199 | + $value = json_encode( $value ); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | $wildcards[$key] = $value; |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | } |
205 | 205 | } |
206 | 206 | |
207 | - return array_merge($global, $wildcards); |
|
207 | + return array_merge( $global, $wildcards ); |
|
208 | 208 | } |
209 | 209 | |
210 | 210 | /** |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | */ |
213 | 213 | protected function filterInputType() |
214 | 214 | { |
215 | - if (!isset($this->args['type']) || !in_array($this->args['type'], self::INPUT_TYPES)) { |
|
215 | + if( !isset( $this->args['type'] ) || !in_array( $this->args['type'], self::INPUT_TYPES ) ) { |
|
216 | 216 | $this->args['type'] = 'text'; |
217 | 217 | } |
218 | 218 | } |
@@ -220,15 +220,15 @@ discard block |
||
220 | 220 | /** |
221 | 221 | * @return array |
222 | 222 | */ |
223 | - protected function parseAttributes(array $attributes, array $args = []) |
|
223 | + protected function parseAttributes( array $attributes, array $args = [] ) |
|
224 | 224 | { |
225 | - if (!empty($args)) { |
|
226 | - $this->args = array_change_key_case($args); |
|
225 | + if( !empty( $args ) ) { |
|
226 | + $this->args = array_change_key_case( $args ); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | $global = $this->filterGlobalAttributes(); |
230 | - $local = $this->filterAttributes($attributes); |
|
230 | + $local = $this->filterAttributes( $attributes ); |
|
231 | 231 | |
232 | - return array_merge($global, $local); |
|
232 | + return array_merge( $global, $local ); |
|
233 | 233 | } |
234 | 234 | } |
@@ -2,8 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace GeminiLabs\Castor\Services; |
4 | 4 | |
5 | -class Normalizer |
|
6 | -{ |
|
5 | +class Normalizer { |
|
7 | 6 | const BOOLEAN_ATTRIBUTES = [ |
8 | 7 | 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
9 | 8 | 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
@@ -24,7 +24,8 @@ |
||
24 | 24 | |
25 | 25 | if (version_compare($wp_version, '5.3', '<')) { |
26 | 26 | require_once ABSPATH.'/'.WPINC.'/class-oembed.php'; |
27 | -} else { |
|
27 | +} |
|
28 | +else { |
|
28 | 29 | require_once ABSPATH.'/'.WPINC.'/class-wp-oembed.php'; |
29 | 30 | } |
30 | 31 |
@@ -5,36 +5,36 @@ |
||
5 | 5 | global $wp_version; |
6 | 6 | |
7 | 7 | if (!is_admin() && version_compare('7.1', phpversion(), '>')) { |
8 | - wp_die( |
|
9 | - __('You must be using PHP 7.1.0 or greater.', 'castor'), |
|
10 | - __('Unsupported PHP version', 'castor') |
|
11 | - ); |
|
8 | + wp_die( |
|
9 | + __('You must be using PHP 7.1.0 or greater.', 'castor'), |
|
10 | + __('Unsupported PHP version', 'castor') |
|
11 | + ); |
|
12 | 12 | } |
13 | 13 | if (!is_admin() && version_compare('5.2', $wp_version, '>')) { |
14 | - wp_die( |
|
15 | - __('You must be using WordPress 5.2.0 or greater.', 'castor'), |
|
16 | - __('Unsupported WordPress version', 'castor') |
|
17 | - ); |
|
14 | + wp_die( |
|
15 | + __('You must be using WordPress 5.2.0 or greater.', 'castor'), |
|
16 | + __('Unsupported WordPress version', 'castor') |
|
17 | + ); |
|
18 | 18 | } |
19 | 19 | if (is_customize_preview() && filter_input(INPUT_GET, 'theme')) { |
20 | - wp_die( |
|
21 | - __('Theme must be activated prior to using the customizer.', 'castor') |
|
22 | - ); |
|
20 | + wp_die( |
|
21 | + __('Theme must be activated prior to using the customizer.', 'castor') |
|
22 | + ); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | define('CASTOR_FRAMEWORK_VERSION', '1.4.3'); |
26 | 26 | |
27 | 27 | if (version_compare($wp_version, '5.3', '<')) { |
28 | - require_once ABSPATH.'/'.WPINC.'/class-oembed.php'; |
|
28 | + require_once ABSPATH.'/'.WPINC.'/class-oembed.php'; |
|
29 | 29 | } else { |
30 | - require_once ABSPATH.'/'.WPINC.'/class-wp-oembed.php'; |
|
30 | + require_once ABSPATH.'/'.WPINC.'/class-wp-oembed.php'; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | if (!function_exists('castor_app')) { |
34 | - function castor_app() |
|
35 | - { |
|
36 | - return \GeminiLabs\Castor\Application::getInstance(); |
|
37 | - } |
|
34 | + function castor_app() |
|
35 | + { |
|
36 | + return \GeminiLabs\Castor\Application::getInstance(); |
|
37 | + } |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | \GeminiLabs\Castor\Application::getInstance()->init(); |
@@ -1,36 +1,36 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -defined('WPINC') || die; |
|
3 | +defined( 'WPINC' ) || die; |
|
4 | 4 | |
5 | 5 | global $wp_version; |
6 | 6 | |
7 | -if (!is_admin() && version_compare('7.1', phpversion(), '>')) { |
|
7 | +if( !is_admin() && version_compare( '7.1', phpversion(), '>' ) ) { |
|
8 | 8 | wp_die( |
9 | - __('You must be using PHP 7.1.0 or greater.', 'castor'), |
|
10 | - __('Unsupported PHP version', 'castor') |
|
9 | + __( 'You must be using PHP 7.1.0 or greater.', 'castor' ), |
|
10 | + __( 'Unsupported PHP version', 'castor' ) |
|
11 | 11 | ); |
12 | 12 | } |
13 | -if (!is_admin() && version_compare('5.2', $wp_version, '>')) { |
|
13 | +if( !is_admin() && version_compare( '5.2', $wp_version, '>' ) ) { |
|
14 | 14 | wp_die( |
15 | - __('You must be using WordPress 5.2.0 or greater.', 'castor'), |
|
16 | - __('Unsupported WordPress version', 'castor') |
|
15 | + __( 'You must be using WordPress 5.2.0 or greater.', 'castor' ), |
|
16 | + __( 'Unsupported WordPress version', 'castor' ) |
|
17 | 17 | ); |
18 | 18 | } |
19 | -if (is_customize_preview() && filter_input(INPUT_GET, 'theme')) { |
|
19 | +if( is_customize_preview() && filter_input( INPUT_GET, 'theme' ) ) { |
|
20 | 20 | wp_die( |
21 | - __('Theme must be activated prior to using the customizer.', 'castor') |
|
21 | + __( 'Theme must be activated prior to using the customizer.', 'castor' ) |
|
22 | 22 | ); |
23 | 23 | } |
24 | 24 | |
25 | -define('CASTOR_FRAMEWORK_VERSION', '1.4.3'); |
|
25 | +define( 'CASTOR_FRAMEWORK_VERSION', '1.4.3' ); |
|
26 | 26 | |
27 | -if (version_compare($wp_version, '5.3', '<')) { |
|
27 | +if( version_compare( $wp_version, '5.3', '<' ) ) { |
|
28 | 28 | require_once ABSPATH.'/'.WPINC.'/class-oembed.php'; |
29 | 29 | } else { |
30 | 30 | require_once ABSPATH.'/'.WPINC.'/class-wp-oembed.php'; |
31 | 31 | } |
32 | 32 | |
33 | -if (!function_exists('castor_app')) { |
|
33 | +if( !function_exists( 'castor_app' ) ) { |
|
34 | 34 | function castor_app() |
35 | 35 | { |
36 | 36 | return \GeminiLabs\Castor\Application::getInstance(); |
@@ -9,225 +9,225 @@ |
||
9 | 9 | |
10 | 10 | abstract class Container |
11 | 11 | { |
12 | - /** |
|
13 | - * The current globally available container (if any). |
|
14 | - * |
|
15 | - * @var static |
|
16 | - */ |
|
17 | - protected static $instance; |
|
18 | - |
|
19 | - /** |
|
20 | - * The container's bound services. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $services = []; |
|
25 | - |
|
26 | - /** |
|
27 | - * The container's bucket items. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $bucket = []; |
|
32 | - |
|
33 | - /** |
|
34 | - * Set the globally available instance of the container. |
|
35 | - * |
|
36 | - * @return static |
|
37 | - */ |
|
38 | - public static function getInstance() |
|
39 | - { |
|
40 | - if (is_null(static::$instance)) { |
|
41 | - static::$instance = new static(); |
|
42 | - } |
|
43 | - |
|
44 | - return static::$instance; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Bind a service to the container. |
|
49 | - * |
|
50 | - * @param string $alias |
|
51 | - * @param mixed $concrete |
|
52 | - * |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public function bind($alias, $concrete) |
|
56 | - { |
|
57 | - $this->services[$alias] = $concrete; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Resolve the given type from the container. |
|
62 | - * Allow unbound aliases that omit the root namespace |
|
63 | - * i.e. 'Controller' translates to 'GeminiLabs\Castor\Controller'. |
|
64 | - * |
|
65 | - * @param mixed $abstract |
|
66 | - * |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function make($abstract) |
|
70 | - { |
|
71 | - $service = isset($this->services[$abstract]) |
|
72 | - ? $this->services[$abstract] |
|
73 | - : $this->addNamespace($abstract); |
|
74 | - |
|
75 | - if (is_callable($service)) { |
|
76 | - return call_user_func_array($service, [$this]); |
|
77 | - } |
|
78 | - if (is_object($service)) { |
|
79 | - return $service; |
|
80 | - } |
|
81 | - |
|
82 | - return $this->resolve($service); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Register a shared binding in the container. |
|
87 | - * |
|
88 | - * @param string $abstract |
|
89 | - * @param \Closure|string|null $concrete |
|
90 | - * |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function singleton($abstract, $concrete) |
|
94 | - { |
|
95 | - $this->bind($abstract, $this->make($concrete)); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Dynamically access container bucket items. |
|
100 | - * |
|
101 | - * @param string $item |
|
102 | - * |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function __get($item) |
|
106 | - { |
|
107 | - return isset($this->bucket[$item]) |
|
108 | - ? $this->bucket[$item] |
|
109 | - : null; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Dynamically set container bucket items. |
|
114 | - * |
|
115 | - * @param string $item |
|
116 | - * @param mixed $value |
|
117 | - * |
|
118 | - * @return void |
|
119 | - */ |
|
120 | - public function __set($item, $value) |
|
121 | - { |
|
122 | - $this->bucket[$item] = $value; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Prefix the current namespace to the abstract if absent. |
|
127 | - * |
|
128 | - * @param string $abstract |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - protected function addNamespace($abstract) |
|
133 | - { |
|
134 | - if (false === strpos($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
135 | - $abstract = __NAMESPACE__."\\$abstract"; |
|
136 | - } |
|
137 | - |
|
138 | - return $abstract; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param \ReflectionParameter $parameter |
|
143 | - * @return null|\ReflectionClass|\ReflectionNamedType|\ReflectionType |
|
144 | - */ |
|
145 | - protected function getClass($parameter) |
|
146 | - { |
|
147 | - if (version_compare(phpversion(), '8', '<')) { |
|
148 | - return $parameter->getClass(); // @compat PHP < 8 |
|
149 | - } |
|
150 | - return $parameter->getType(); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Throw an exception that the concrete is not instantiable. |
|
155 | - * |
|
156 | - * @param string $concrete |
|
157 | - * |
|
158 | - * @return void |
|
159 | - * @throws BindingResolutionException |
|
160 | - */ |
|
161 | - protected function notInstantiable($concrete) |
|
162 | - { |
|
163 | - $message = "Target [$concrete] is not instantiable."; |
|
164 | - |
|
165 | - throw new BindingResolutionException($message); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Resolve a class based dependency from the container. |
|
170 | - * |
|
171 | - * @param mixed $concrete |
|
172 | - * |
|
173 | - * @return mixed |
|
174 | - * @throws BindingResolutionException |
|
175 | - */ |
|
176 | - protected function resolve($concrete) |
|
177 | - { |
|
178 | - if ($concrete instanceof Closure) { |
|
179 | - return $concrete($this); |
|
180 | - } |
|
181 | - |
|
182 | - $reflector = new ReflectionClass($concrete); |
|
183 | - |
|
184 | - if (!$reflector->isInstantiable()) { |
|
185 | - return $this->notInstantiable($concrete); |
|
186 | - } |
|
187 | - |
|
188 | - if (is_null(($constructor = $reflector->getConstructor()))) { |
|
189 | - return new $concrete(); |
|
190 | - } |
|
191 | - |
|
192 | - return $reflector->newInstanceArgs( |
|
193 | - $this->resolveDependencies($constructor->getParameters()) |
|
194 | - ); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Resolve a class based dependency from the container. |
|
199 | - * |
|
200 | - * @return mixed |
|
201 | - * @throws BindingResolutionException |
|
202 | - */ |
|
203 | - protected function resolveClass(ReflectionParameter $parameter) |
|
204 | - { |
|
205 | - try { |
|
206 | - return $this->make($this->getClass($parameter)->getName()); |
|
207 | - } catch (BindingResolutionException $e) { |
|
208 | - if ($parameter->isOptional()) { |
|
209 | - return $parameter->getDefaultValue(); |
|
210 | - } |
|
211 | - throw $e; |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Resolve all of the dependencies from the ReflectionParameters. |
|
217 | - * |
|
218 | - * @return array |
|
219 | - */ |
|
220 | - protected function resolveDependencies(array $dependencies) |
|
221 | - { |
|
222 | - $results = []; |
|
223 | - |
|
224 | - foreach ($dependencies as $dependency) { |
|
225 | - // If the class is null, the dependency is a string or some other primitive type |
|
226 | - $results[] = !is_null($class = $this->getClass($dependency)) |
|
227 | - ? $this->resolveClass($dependency) |
|
228 | - : null; |
|
229 | - } |
|
230 | - |
|
231 | - return $results; |
|
232 | - } |
|
12 | + /** |
|
13 | + * The current globally available container (if any). |
|
14 | + * |
|
15 | + * @var static |
|
16 | + */ |
|
17 | + protected static $instance; |
|
18 | + |
|
19 | + /** |
|
20 | + * The container's bound services. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $services = []; |
|
25 | + |
|
26 | + /** |
|
27 | + * The container's bucket items. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $bucket = []; |
|
32 | + |
|
33 | + /** |
|
34 | + * Set the globally available instance of the container. |
|
35 | + * |
|
36 | + * @return static |
|
37 | + */ |
|
38 | + public static function getInstance() |
|
39 | + { |
|
40 | + if (is_null(static::$instance)) { |
|
41 | + static::$instance = new static(); |
|
42 | + } |
|
43 | + |
|
44 | + return static::$instance; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Bind a service to the container. |
|
49 | + * |
|
50 | + * @param string $alias |
|
51 | + * @param mixed $concrete |
|
52 | + * |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public function bind($alias, $concrete) |
|
56 | + { |
|
57 | + $this->services[$alias] = $concrete; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Resolve the given type from the container. |
|
62 | + * Allow unbound aliases that omit the root namespace |
|
63 | + * i.e. 'Controller' translates to 'GeminiLabs\Castor\Controller'. |
|
64 | + * |
|
65 | + * @param mixed $abstract |
|
66 | + * |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function make($abstract) |
|
70 | + { |
|
71 | + $service = isset($this->services[$abstract]) |
|
72 | + ? $this->services[$abstract] |
|
73 | + : $this->addNamespace($abstract); |
|
74 | + |
|
75 | + if (is_callable($service)) { |
|
76 | + return call_user_func_array($service, [$this]); |
|
77 | + } |
|
78 | + if (is_object($service)) { |
|
79 | + return $service; |
|
80 | + } |
|
81 | + |
|
82 | + return $this->resolve($service); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Register a shared binding in the container. |
|
87 | + * |
|
88 | + * @param string $abstract |
|
89 | + * @param \Closure|string|null $concrete |
|
90 | + * |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function singleton($abstract, $concrete) |
|
94 | + { |
|
95 | + $this->bind($abstract, $this->make($concrete)); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Dynamically access container bucket items. |
|
100 | + * |
|
101 | + * @param string $item |
|
102 | + * |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function __get($item) |
|
106 | + { |
|
107 | + return isset($this->bucket[$item]) |
|
108 | + ? $this->bucket[$item] |
|
109 | + : null; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Dynamically set container bucket items. |
|
114 | + * |
|
115 | + * @param string $item |
|
116 | + * @param mixed $value |
|
117 | + * |
|
118 | + * @return void |
|
119 | + */ |
|
120 | + public function __set($item, $value) |
|
121 | + { |
|
122 | + $this->bucket[$item] = $value; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Prefix the current namespace to the abstract if absent. |
|
127 | + * |
|
128 | + * @param string $abstract |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + protected function addNamespace($abstract) |
|
133 | + { |
|
134 | + if (false === strpos($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
135 | + $abstract = __NAMESPACE__."\\$abstract"; |
|
136 | + } |
|
137 | + |
|
138 | + return $abstract; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param \ReflectionParameter $parameter |
|
143 | + * @return null|\ReflectionClass|\ReflectionNamedType|\ReflectionType |
|
144 | + */ |
|
145 | + protected function getClass($parameter) |
|
146 | + { |
|
147 | + if (version_compare(phpversion(), '8', '<')) { |
|
148 | + return $parameter->getClass(); // @compat PHP < 8 |
|
149 | + } |
|
150 | + return $parameter->getType(); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Throw an exception that the concrete is not instantiable. |
|
155 | + * |
|
156 | + * @param string $concrete |
|
157 | + * |
|
158 | + * @return void |
|
159 | + * @throws BindingResolutionException |
|
160 | + */ |
|
161 | + protected function notInstantiable($concrete) |
|
162 | + { |
|
163 | + $message = "Target [$concrete] is not instantiable."; |
|
164 | + |
|
165 | + throw new BindingResolutionException($message); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Resolve a class based dependency from the container. |
|
170 | + * |
|
171 | + * @param mixed $concrete |
|
172 | + * |
|
173 | + * @return mixed |
|
174 | + * @throws BindingResolutionException |
|
175 | + */ |
|
176 | + protected function resolve($concrete) |
|
177 | + { |
|
178 | + if ($concrete instanceof Closure) { |
|
179 | + return $concrete($this); |
|
180 | + } |
|
181 | + |
|
182 | + $reflector = new ReflectionClass($concrete); |
|
183 | + |
|
184 | + if (!$reflector->isInstantiable()) { |
|
185 | + return $this->notInstantiable($concrete); |
|
186 | + } |
|
187 | + |
|
188 | + if (is_null(($constructor = $reflector->getConstructor()))) { |
|
189 | + return new $concrete(); |
|
190 | + } |
|
191 | + |
|
192 | + return $reflector->newInstanceArgs( |
|
193 | + $this->resolveDependencies($constructor->getParameters()) |
|
194 | + ); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Resolve a class based dependency from the container. |
|
199 | + * |
|
200 | + * @return mixed |
|
201 | + * @throws BindingResolutionException |
|
202 | + */ |
|
203 | + protected function resolveClass(ReflectionParameter $parameter) |
|
204 | + { |
|
205 | + try { |
|
206 | + return $this->make($this->getClass($parameter)->getName()); |
|
207 | + } catch (BindingResolutionException $e) { |
|
208 | + if ($parameter->isOptional()) { |
|
209 | + return $parameter->getDefaultValue(); |
|
210 | + } |
|
211 | + throw $e; |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Resolve all of the dependencies from the ReflectionParameters. |
|
217 | + * |
|
218 | + * @return array |
|
219 | + */ |
|
220 | + protected function resolveDependencies(array $dependencies) |
|
221 | + { |
|
222 | + $results = []; |
|
223 | + |
|
224 | + foreach ($dependencies as $dependency) { |
|
225 | + // If the class is null, the dependency is a string or some other primitive type |
|
226 | + $results[] = !is_null($class = $this->getClass($dependency)) |
|
227 | + ? $this->resolveClass($dependency) |
|
228 | + : null; |
|
229 | + } |
|
230 | + |
|
231 | + return $results; |
|
232 | + } |
|
233 | 233 | } |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public static function getInstance() |
39 | 39 | { |
40 | - if (is_null(static::$instance)) { |
|
40 | + if( is_null( static::$instance ) ) { |
|
41 | 41 | static::$instance = new static(); |
42 | 42 | } |
43 | 43 | |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return mixed |
54 | 54 | */ |
55 | - public function bind($alias, $concrete) |
|
55 | + public function bind( $alias, $concrete ) |
|
56 | 56 | { |
57 | 57 | $this->services[$alias] = $concrete; |
58 | 58 | } |
@@ -66,20 +66,20 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @return mixed |
68 | 68 | */ |
69 | - public function make($abstract) |
|
69 | + public function make( $abstract ) |
|
70 | 70 | { |
71 | - $service = isset($this->services[$abstract]) |
|
71 | + $service = isset( $this->services[$abstract] ) |
|
72 | 72 | ? $this->services[$abstract] |
73 | - : $this->addNamespace($abstract); |
|
73 | + : $this->addNamespace( $abstract ); |
|
74 | 74 | |
75 | - if (is_callable($service)) { |
|
76 | - return call_user_func_array($service, [$this]); |
|
75 | + if( is_callable( $service ) ) { |
|
76 | + return call_user_func_array( $service, [$this] ); |
|
77 | 77 | } |
78 | - if (is_object($service)) { |
|
78 | + if( is_object( $service ) ) { |
|
79 | 79 | return $service; |
80 | 80 | } |
81 | 81 | |
82 | - return $this->resolve($service); |
|
82 | + return $this->resolve( $service ); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -90,9 +90,9 @@ discard block |
||
90 | 90 | * |
91 | 91 | * @return void |
92 | 92 | */ |
93 | - public function singleton($abstract, $concrete) |
|
93 | + public function singleton( $abstract, $concrete ) |
|
94 | 94 | { |
95 | - $this->bind($abstract, $this->make($concrete)); |
|
95 | + $this->bind( $abstract, $this->make( $concrete ) ); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | * |
103 | 103 | * @return mixed |
104 | 104 | */ |
105 | - public function __get($item) |
|
105 | + public function __get( $item ) |
|
106 | 106 | { |
107 | - return isset($this->bucket[$item]) |
|
107 | + return isset( $this->bucket[$item] ) |
|
108 | 108 | ? $this->bucket[$item] |
109 | 109 | : null; |
110 | 110 | } |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @return void |
119 | 119 | */ |
120 | - public function __set($item, $value) |
|
120 | + public function __set( $item, $value ) |
|
121 | 121 | { |
122 | 122 | $this->bucket[$item] = $value; |
123 | 123 | } |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @return string |
131 | 131 | */ |
132 | - protected function addNamespace($abstract) |
|
132 | + protected function addNamespace( $abstract ) |
|
133 | 133 | { |
134 | - if (false === strpos($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
134 | + if( false === strpos( $abstract, __NAMESPACE__ ) && !class_exists( $abstract ) ) { |
|
135 | 135 | $abstract = __NAMESPACE__."\\$abstract"; |
136 | 136 | } |
137 | 137 | |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | * @param \ReflectionParameter $parameter |
143 | 143 | * @return null|\ReflectionClass|\ReflectionNamedType|\ReflectionType |
144 | 144 | */ |
145 | - protected function getClass($parameter) |
|
145 | + protected function getClass( $parameter ) |
|
146 | 146 | { |
147 | - if (version_compare(phpversion(), '8', '<')) { |
|
147 | + if( version_compare( phpversion(), '8', '<' ) ) { |
|
148 | 148 | return $parameter->getClass(); // @compat PHP < 8 |
149 | 149 | } |
150 | 150 | return $parameter->getType(); |
@@ -158,11 +158,11 @@ discard block |
||
158 | 158 | * @return void |
159 | 159 | * @throws BindingResolutionException |
160 | 160 | */ |
161 | - protected function notInstantiable($concrete) |
|
161 | + protected function notInstantiable( $concrete ) |
|
162 | 162 | { |
163 | 163 | $message = "Target [$concrete] is not instantiable."; |
164 | 164 | |
165 | - throw new BindingResolutionException($message); |
|
165 | + throw new BindingResolutionException( $message ); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -173,24 +173,24 @@ discard block |
||
173 | 173 | * @return mixed |
174 | 174 | * @throws BindingResolutionException |
175 | 175 | */ |
176 | - protected function resolve($concrete) |
|
176 | + protected function resolve( $concrete ) |
|
177 | 177 | { |
178 | - if ($concrete instanceof Closure) { |
|
179 | - return $concrete($this); |
|
178 | + if( $concrete instanceof Closure ) { |
|
179 | + return $concrete( $this ); |
|
180 | 180 | } |
181 | 181 | |
182 | - $reflector = new ReflectionClass($concrete); |
|
182 | + $reflector = new ReflectionClass( $concrete ); |
|
183 | 183 | |
184 | - if (!$reflector->isInstantiable()) { |
|
185 | - return $this->notInstantiable($concrete); |
|
184 | + if( !$reflector->isInstantiable() ) { |
|
185 | + return $this->notInstantiable( $concrete ); |
|
186 | 186 | } |
187 | 187 | |
188 | - if (is_null(($constructor = $reflector->getConstructor()))) { |
|
188 | + if( is_null( ( $constructor = $reflector->getConstructor() ) ) ) { |
|
189 | 189 | return new $concrete(); |
190 | 190 | } |
191 | 191 | |
192 | 192 | return $reflector->newInstanceArgs( |
193 | - $this->resolveDependencies($constructor->getParameters()) |
|
193 | + $this->resolveDependencies( $constructor->getParameters() ) |
|
194 | 194 | ); |
195 | 195 | } |
196 | 196 | |
@@ -200,12 +200,12 @@ discard block |
||
200 | 200 | * @return mixed |
201 | 201 | * @throws BindingResolutionException |
202 | 202 | */ |
203 | - protected function resolveClass(ReflectionParameter $parameter) |
|
203 | + protected function resolveClass( ReflectionParameter $parameter ) |
|
204 | 204 | { |
205 | 205 | try { |
206 | - return $this->make($this->getClass($parameter)->getName()); |
|
207 | - } catch (BindingResolutionException $e) { |
|
208 | - if ($parameter->isOptional()) { |
|
206 | + return $this->make( $this->getClass( $parameter )->getName() ); |
|
207 | + } catch( BindingResolutionException $e ) { |
|
208 | + if( $parameter->isOptional() ) { |
|
209 | 209 | return $parameter->getDefaultValue(); |
210 | 210 | } |
211 | 211 | throw $e; |
@@ -217,14 +217,14 @@ discard block |
||
217 | 217 | * |
218 | 218 | * @return array |
219 | 219 | */ |
220 | - protected function resolveDependencies(array $dependencies) |
|
220 | + protected function resolveDependencies( array $dependencies ) |
|
221 | 221 | { |
222 | 222 | $results = []; |
223 | 223 | |
224 | - foreach ($dependencies as $dependency) { |
|
224 | + foreach( $dependencies as $dependency ) { |
|
225 | 225 | // If the class is null, the dependency is a string or some other primitive type |
226 | - $results[] = !is_null($class = $this->getClass($dependency)) |
|
227 | - ? $this->resolveClass($dependency) |
|
226 | + $results[] = !is_null( $class = $this->getClass( $dependency ) ) |
|
227 | + ? $this->resolveClass( $dependency ) |
|
228 | 228 | : null; |
229 | 229 | } |
230 | 230 |
@@ -7,8 +7,7 @@ discard block |
||
7 | 7 | use ReflectionClass; |
8 | 8 | use ReflectionParameter; |
9 | 9 | |
10 | -abstract class Container |
|
11 | -{ |
|
10 | +abstract class Container { |
|
12 | 11 | /** |
13 | 12 | * The current globally available container (if any). |
14 | 13 | * |
@@ -204,7 +203,8 @@ discard block |
||
204 | 203 | { |
205 | 204 | try { |
206 | 205 | return $this->make($this->getClass($parameter)->getName()); |
207 | - } catch (BindingResolutionException $e) { |
|
206 | + } |
|
207 | + catch (BindingResolutionException $e) { |
|
208 | 208 | if ($parameter->isOptional()) { |
209 | 209 | return $parameter->getDefaultValue(); |
210 | 210 | } |
@@ -6,85 +6,85 @@ |
||
6 | 6 | |
7 | 7 | final class Application extends Container |
8 | 8 | { |
9 | - public $assets; |
|
10 | - public $cssDir; |
|
11 | - public $imgDir; |
|
12 | - public $jsDir; |
|
9 | + public $assets; |
|
10 | + public $cssDir; |
|
11 | + public $imgDir; |
|
12 | + public $jsDir; |
|
13 | 13 | |
14 | - public function __construct() |
|
15 | - { |
|
16 | - $this->assets = sprintf('%s/assets/', dirname(__DIR__)); |
|
17 | - Facade::clearResolvedInstances(); |
|
18 | - Facade::setFacadeApplication($this); |
|
19 | - $this->registerAliases(); |
|
20 | - $this->registerBindings(); |
|
21 | - } |
|
14 | + public function __construct() |
|
15 | + { |
|
16 | + $this->assets = sprintf('%s/assets/', dirname(__DIR__)); |
|
17 | + Facade::clearResolvedInstances(); |
|
18 | + Facade::setFacadeApplication($this); |
|
19 | + $this->registerAliases(); |
|
20 | + $this->registerBindings(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function init() |
|
24 | - { |
|
25 | - $controller = $this->make(Controller::class); |
|
23 | + public function init() |
|
24 | + { |
|
25 | + $controller = $this->make(Controller::class); |
|
26 | 26 | |
27 | - // Action hooks |
|
28 | - add_action('after_setup_theme', [$controller, 'afterSetupTheme'], 20); |
|
29 | - add_action('login_head', [$controller, 'loadAdminFavicon']); |
|
30 | - add_action('admin_head', [$controller, 'loadAdminFavicon']); |
|
31 | - add_action('login_head', [$controller, 'login']); |
|
32 | - add_action('admin_enqueue_scripts', [$controller, 'registerAdminAssets']); |
|
33 | - add_action('wp_enqueue_scripts', [$controller, 'registerAssets']); |
|
34 | - add_action('customize_register', [$controller, 'registerCustomizer']); |
|
35 | - add_action('customize_preview_init', [$controller, 'registerCustomizerAssets']); |
|
36 | - add_action('widgets_init', [$controller, 'registerSidebars']); |
|
27 | + // Action hooks |
|
28 | + add_action('after_setup_theme', [$controller, 'afterSetupTheme'], 20); |
|
29 | + add_action('login_head', [$controller, 'loadAdminFavicon']); |
|
30 | + add_action('admin_head', [$controller, 'loadAdminFavicon']); |
|
31 | + add_action('login_head', [$controller, 'login']); |
|
32 | + add_action('admin_enqueue_scripts', [$controller, 'registerAdminAssets']); |
|
33 | + add_action('wp_enqueue_scripts', [$controller, 'registerAssets']); |
|
34 | + add_action('customize_register', [$controller, 'registerCustomizer']); |
|
35 | + add_action('customize_preview_init', [$controller, 'registerCustomizerAssets']); |
|
36 | + add_action('widgets_init', [$controller, 'registerSidebars']); |
|
37 | 37 | |
38 | - // Filter hooks |
|
39 | - add_filter('body_class', [$controller, 'filterBodyClasses']); |
|
40 | - add_filter('template_include', [$controller, 'filterTemplate']); |
|
41 | - add_filter('login_headertext', [$controller, 'filterLoginTitle']); |
|
42 | - add_filter('login_headerurl', [$controller, 'filterLoginUrl']); |
|
38 | + // Filter hooks |
|
39 | + add_filter('body_class', [$controller, 'filterBodyClasses']); |
|
40 | + add_filter('template_include', [$controller, 'filterTemplate']); |
|
41 | + add_filter('login_headertext', [$controller, 'filterLoginTitle']); |
|
42 | + add_filter('login_headerurl', [$controller, 'filterLoginUrl']); |
|
43 | 43 | |
44 | - foreach ($this->getTemplateTypes() as $type) { |
|
45 | - add_filter("{$type}_template_hierarchy", [$controller, 'filterTemplateHierarchy']); |
|
46 | - } |
|
47 | - } |
|
44 | + foreach ($this->getTemplateTypes() as $type) { |
|
45 | + add_filter("{$type}_template_hierarchy", [$controller, 'filterTemplateHierarchy']); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function registerAliases() |
|
53 | - { |
|
54 | - $aliases = [ |
|
55 | - 'Development' => Facades\Development::class, |
|
56 | - 'Log' => Facades\Log::class, |
|
57 | - 'Media' => Facades\Media::class, |
|
58 | - 'PostMeta' => Facades\PostMeta::class, |
|
59 | - 'Render' => Facades\Render::class, |
|
60 | - 'SiteMeta' => Facades\SiteMeta::class, |
|
61 | - 'Template' => Facades\Template::class, |
|
62 | - 'Theme' => Facades\Theme::class, |
|
63 | - 'Utility' => Facades\Utility::class, |
|
64 | - ]; |
|
65 | - $aliases = apply_filters('castor/register/aliases', $aliases); |
|
66 | - AliasLoader::getInstance($aliases)->register(); |
|
67 | - } |
|
49 | + /** |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function registerAliases() |
|
53 | + { |
|
54 | + $aliases = [ |
|
55 | + 'Development' => Facades\Development::class, |
|
56 | + 'Log' => Facades\Log::class, |
|
57 | + 'Media' => Facades\Media::class, |
|
58 | + 'PostMeta' => Facades\PostMeta::class, |
|
59 | + 'Render' => Facades\Render::class, |
|
60 | + 'SiteMeta' => Facades\SiteMeta::class, |
|
61 | + 'Template' => Facades\Template::class, |
|
62 | + 'Theme' => Facades\Theme::class, |
|
63 | + 'Utility' => Facades\Utility::class, |
|
64 | + ]; |
|
65 | + $aliases = apply_filters('castor/register/aliases', $aliases); |
|
66 | + AliasLoader::getInstance($aliases)->register(); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * @return void |
|
71 | - */ |
|
72 | - public function registerBindings() |
|
73 | - { |
|
74 | - $this->bind(Helpers\Log::class, function () { |
|
75 | - return new Helpers\Log(trailingslashit(get_stylesheet_directory()).'castor-debug.log'); |
|
76 | - }); |
|
77 | - } |
|
69 | + /** |
|
70 | + * @return void |
|
71 | + */ |
|
72 | + public function registerBindings() |
|
73 | + { |
|
74 | + $this->bind(Helpers\Log::class, function () { |
|
75 | + return new Helpers\Log(trailingslashit(get_stylesheet_directory()).'castor-debug.log'); |
|
76 | + }); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - protected function getTemplateTypes() |
|
83 | - { |
|
84 | - return [ |
|
85 | - '404', 'archive', 'attachment', 'author', 'category', 'date', |
|
86 | - 'embed', 'frontpage', 'home', 'index', 'page', 'paged', |
|
87 | - 'search', 'single', 'singular', 'tag', 'taxonomy', |
|
88 | - ]; |
|
89 | - } |
|
79 | + /** |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + protected function getTemplateTypes() |
|
83 | + { |
|
84 | + return [ |
|
85 | + '404', 'archive', 'attachment', 'author', 'category', 'date', |
|
86 | + 'embed', 'frontpage', 'home', 'index', 'page', 'paged', |
|
87 | + 'search', 'single', 'singular', 'tag', 'taxonomy', |
|
88 | + ]; |
|
89 | + } |
|
90 | 90 | } |
@@ -13,36 +13,36 @@ discard block |
||
13 | 13 | |
14 | 14 | public function __construct() |
15 | 15 | { |
16 | - $this->assets = sprintf('%s/assets/', dirname(__DIR__)); |
|
16 | + $this->assets = sprintf( '%s/assets/', dirname( __DIR__ ) ); |
|
17 | 17 | Facade::clearResolvedInstances(); |
18 | - Facade::setFacadeApplication($this); |
|
18 | + Facade::setFacadeApplication( $this ); |
|
19 | 19 | $this->registerAliases(); |
20 | 20 | $this->registerBindings(); |
21 | 21 | } |
22 | 22 | |
23 | 23 | public function init() |
24 | 24 | { |
25 | - $controller = $this->make(Controller::class); |
|
25 | + $controller = $this->make( Controller::class ); |
|
26 | 26 | |
27 | 27 | // Action hooks |
28 | - add_action('after_setup_theme', [$controller, 'afterSetupTheme'], 20); |
|
29 | - add_action('login_head', [$controller, 'loadAdminFavicon']); |
|
30 | - add_action('admin_head', [$controller, 'loadAdminFavicon']); |
|
31 | - add_action('login_head', [$controller, 'login']); |
|
32 | - add_action('admin_enqueue_scripts', [$controller, 'registerAdminAssets']); |
|
33 | - add_action('wp_enqueue_scripts', [$controller, 'registerAssets']); |
|
34 | - add_action('customize_register', [$controller, 'registerCustomizer']); |
|
35 | - add_action('customize_preview_init', [$controller, 'registerCustomizerAssets']); |
|
36 | - add_action('widgets_init', [$controller, 'registerSidebars']); |
|
28 | + add_action( 'after_setup_theme', [$controller, 'afterSetupTheme'], 20 ); |
|
29 | + add_action( 'login_head', [$controller, 'loadAdminFavicon'] ); |
|
30 | + add_action( 'admin_head', [$controller, 'loadAdminFavicon'] ); |
|
31 | + add_action( 'login_head', [$controller, 'login'] ); |
|
32 | + add_action( 'admin_enqueue_scripts', [$controller, 'registerAdminAssets'] ); |
|
33 | + add_action( 'wp_enqueue_scripts', [$controller, 'registerAssets'] ); |
|
34 | + add_action( 'customize_register', [$controller, 'registerCustomizer'] ); |
|
35 | + add_action( 'customize_preview_init', [$controller, 'registerCustomizerAssets'] ); |
|
36 | + add_action( 'widgets_init', [$controller, 'registerSidebars'] ); |
|
37 | 37 | |
38 | 38 | // Filter hooks |
39 | - add_filter('body_class', [$controller, 'filterBodyClasses']); |
|
40 | - add_filter('template_include', [$controller, 'filterTemplate']); |
|
41 | - add_filter('login_headertext', [$controller, 'filterLoginTitle']); |
|
42 | - add_filter('login_headerurl', [$controller, 'filterLoginUrl']); |
|
39 | + add_filter( 'body_class', [$controller, 'filterBodyClasses'] ); |
|
40 | + add_filter( 'template_include', [$controller, 'filterTemplate'] ); |
|
41 | + add_filter( 'login_headertext', [$controller, 'filterLoginTitle'] ); |
|
42 | + add_filter( 'login_headerurl', [$controller, 'filterLoginUrl'] ); |
|
43 | 43 | |
44 | - foreach ($this->getTemplateTypes() as $type) { |
|
45 | - add_filter("{$type}_template_hierarchy", [$controller, 'filterTemplateHierarchy']); |
|
44 | + foreach( $this->getTemplateTypes() as $type ) { |
|
45 | + add_filter( "{$type}_template_hierarchy", [$controller, 'filterTemplateHierarchy'] ); |
|
46 | 46 | } |
47 | 47 | } |
48 | 48 | |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | 'Theme' => Facades\Theme::class, |
63 | 63 | 'Utility' => Facades\Utility::class, |
64 | 64 | ]; |
65 | - $aliases = apply_filters('castor/register/aliases', $aliases); |
|
66 | - AliasLoader::getInstance($aliases)->register(); |
|
65 | + $aliases = apply_filters( 'castor/register/aliases', $aliases ); |
|
66 | + AliasLoader::getInstance( $aliases )->register(); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -71,8 +71,8 @@ discard block |
||
71 | 71 | */ |
72 | 72 | public function registerBindings() |
73 | 73 | { |
74 | - $this->bind(Helpers\Log::class, function () { |
|
75 | - return new Helpers\Log(trailingslashit(get_stylesheet_directory()).'castor-debug.log'); |
|
74 | + $this->bind( Helpers\Log::class, function() { |
|
75 | + return new Helpers\Log( trailingslashit( get_stylesheet_directory() ).'castor-debug.log' ); |
|
76 | 76 | }); |
77 | 77 | } |
78 | 78 |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use GeminiLabs\Castor\Controller; |
6 | 6 | |
7 | -final class Application extends Container |
|
8 | -{ |
|
7 | +final class Application extends Container { |
|
9 | 8 | public $assets; |
10 | 9 | public $cssDir; |
11 | 10 | public $imgDir; |
@@ -6,266 +6,266 @@ |
||
6 | 6 | |
7 | 7 | class Email |
8 | 8 | { |
9 | - /** |
|
10 | - * @var Template |
|
11 | - */ |
|
12 | - public $template; |
|
13 | - |
|
14 | - /** |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $attachments; |
|
18 | - |
|
19 | - /** |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $headers; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $message; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $subject; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $to; |
|
38 | - |
|
39 | - public function __construct(Template $template) |
|
40 | - { |
|
41 | - $this->template = $template; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @return Email |
|
46 | - */ |
|
47 | - public function compose(array $email) |
|
48 | - { |
|
49 | - $email = $this->normalize($email); |
|
50 | - |
|
51 | - $this->attachments = $email['attachments']; |
|
52 | - $this->headers = $this->buildHeaders($email); |
|
53 | - $this->message = $this->buildHtmlMessage($email); |
|
54 | - $this->subject = $email['subject']; |
|
55 | - $this->to = $email['to']; |
|
56 | - |
|
57 | - add_action('phpmailer_init', function ($phpmailer) { |
|
58 | - if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
59 | - return; |
|
60 | - } |
|
61 | - $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
62 | - }); |
|
63 | - |
|
64 | - return $this; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param bool $plaintext |
|
69 | - * |
|
70 | - * @return string|null |
|
71 | - */ |
|
72 | - public function read($plaintext = false) |
|
73 | - { |
|
74 | - return $plaintext |
|
75 | - ? $this->buildPlainTextMessage($this->message) |
|
76 | - : $this->message; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return bool|null |
|
81 | - */ |
|
82 | - public function send() |
|
83 | - { |
|
84 | - if (!$this->message || !$this->subject || !$this->to) { |
|
85 | - return; |
|
86 | - } |
|
87 | - |
|
88 | - $sent = wp_mail( |
|
89 | - $this->to, |
|
90 | - $this->subject, |
|
91 | - $this->message, |
|
92 | - $this->headers, |
|
93 | - $this->attachments |
|
94 | - ); |
|
95 | - |
|
96 | - $this->reset(); |
|
97 | - |
|
98 | - return $sent; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - protected function buildHeaders(array $email) |
|
105 | - { |
|
106 | - $allowed = [ |
|
107 | - 'bcc', |
|
108 | - 'cc', |
|
109 | - 'from', |
|
110 | - 'reply-to', |
|
111 | - ]; |
|
112 | - |
|
113 | - $headers = array_intersect_key($email, array_flip($allowed)); |
|
114 | - $headers = array_filter($headers); |
|
115 | - |
|
116 | - foreach ($headers as $key => $value) { |
|
117 | - unset($headers[$key]); |
|
118 | - $headers[] = sprintf('%s: %s', $key, $value); |
|
119 | - } |
|
120 | - |
|
121 | - $headers[] = 'Content-Type: text/html'; |
|
122 | - |
|
123 | - return apply_filters('castor/email/headers', $headers, $this); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - protected function buildHtmlMessage(array $email) |
|
130 | - { |
|
131 | - $body = $this->renderTemplate('index'); |
|
132 | - $message = !empty($email['template']) |
|
133 | - ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
134 | - : $email['message']; |
|
135 | - |
|
136 | - $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
137 | - $message = str_replace('{message}', $message, $body); |
|
138 | - |
|
139 | - return apply_filters('castor/email/message', $message, 'html', $this); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param string $message |
|
144 | - * |
|
145 | - * @return string |
|
146 | - */ |
|
147 | - protected function buildPlainTextMessage($message) |
|
148 | - { |
|
149 | - return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param string $message |
|
154 | - * |
|
155 | - * @return string |
|
156 | - */ |
|
157 | - protected function filterHtml($message) |
|
158 | - { |
|
159 | - $message = strip_shortcodes($message); |
|
160 | - $message = wptexturize($message); |
|
161 | - $message = wpautop($message); |
|
162 | - $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
163 | - $message = stripslashes($message); |
|
164 | - return $message; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - protected function getDefaultFrom() |
|
171 | - { |
|
172 | - return sprintf('%s <%s>', |
|
173 | - wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
174 | - (string) get_option('admin_email') |
|
175 | - ); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - protected function normalize($email) |
|
182 | - { |
|
183 | - $defaults = array_fill_keys([ |
|
184 | - 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
|
185 | - 'template', 'template-tags', 'to', |
|
186 | - ], ''); |
|
187 | - $defaults['from'] = $this->getDefaultFrom(); |
|
188 | - |
|
189 | - $email = shortcode_atts($defaults, $email); |
|
190 | - |
|
191 | - foreach (['attachments', 'template-tags'] as $key) { |
|
192 | - $email[$key] = array_filter((array) $email[$key]); |
|
193 | - } |
|
194 | - if (empty($email['reply-to'])) { |
|
195 | - $email['reply-to'] = $email['from']; |
|
196 | - } |
|
197 | - |
|
198 | - return apply_filters('castor/email/compose', $email, $this); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Override by adding the custom template to "templates/castor/" in your theme. |
|
203 | - * |
|
204 | - * @param string $templatePath |
|
205 | - * |
|
206 | - * @return void|string |
|
207 | - */ |
|
208 | - protected function renderTemplate($templatePath, array $args = []) |
|
209 | - { |
|
210 | - $file = $this->template->get('castor/email/'.$templatePath); |
|
211 | - |
|
212 | - if (!file_exists($file)) { |
|
213 | - $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
214 | - } |
|
215 | - if (!file_exists($file)) { |
|
216 | - return; |
|
217 | - } |
|
218 | - |
|
219 | - ob_start(); |
|
220 | - include $file; |
|
221 | - $template = ob_get_clean(); |
|
222 | - |
|
223 | - return $this->renderTemplateString($template, $args); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @param string $template |
|
228 | - * |
|
229 | - * @return string |
|
230 | - */ |
|
231 | - protected function renderTemplateString($template, array $args = []) |
|
232 | - { |
|
233 | - foreach ($args as $key => $value) { |
|
234 | - $template = str_replace('{'.$key.'}', $value, $template); |
|
235 | - } |
|
236 | - return trim($template); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * @return void |
|
241 | - */ |
|
242 | - protected function reset() |
|
243 | - { |
|
244 | - $this->attachments = []; |
|
245 | - $this->headers = []; |
|
246 | - $this->message = null; |
|
247 | - $this->subject = null; |
|
248 | - $this->to = null; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * - remove invisible elements |
|
253 | - * - replace certain elements with a line-break |
|
254 | - * - replace certain table elements with a space |
|
255 | - * - add a placeholder for plain-text bullets to list elements |
|
256 | - * - strip all remaining HTML tags. |
|
257 | - * @return string |
|
258 | - */ |
|
259 | - protected function stripHtmlTags($string) |
|
260 | - { |
|
261 | - $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
262 | - $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
263 | - $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
264 | - $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
265 | - $string = wp_strip_all_tags($string); |
|
266 | - $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
267 | - $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
268 | - $string = str_replace('-o-^-o-', ' - ', $string); |
|
269 | - return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
270 | - } |
|
9 | + /** |
|
10 | + * @var Template |
|
11 | + */ |
|
12 | + public $template; |
|
13 | + |
|
14 | + /** |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $attachments; |
|
18 | + |
|
19 | + /** |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $headers; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $message; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $subject; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $to; |
|
38 | + |
|
39 | + public function __construct(Template $template) |
|
40 | + { |
|
41 | + $this->template = $template; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @return Email |
|
46 | + */ |
|
47 | + public function compose(array $email) |
|
48 | + { |
|
49 | + $email = $this->normalize($email); |
|
50 | + |
|
51 | + $this->attachments = $email['attachments']; |
|
52 | + $this->headers = $this->buildHeaders($email); |
|
53 | + $this->message = $this->buildHtmlMessage($email); |
|
54 | + $this->subject = $email['subject']; |
|
55 | + $this->to = $email['to']; |
|
56 | + |
|
57 | + add_action('phpmailer_init', function ($phpmailer) { |
|
58 | + if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
59 | + return; |
|
60 | + } |
|
61 | + $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
62 | + }); |
|
63 | + |
|
64 | + return $this; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param bool $plaintext |
|
69 | + * |
|
70 | + * @return string|null |
|
71 | + */ |
|
72 | + public function read($plaintext = false) |
|
73 | + { |
|
74 | + return $plaintext |
|
75 | + ? $this->buildPlainTextMessage($this->message) |
|
76 | + : $this->message; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return bool|null |
|
81 | + */ |
|
82 | + public function send() |
|
83 | + { |
|
84 | + if (!$this->message || !$this->subject || !$this->to) { |
|
85 | + return; |
|
86 | + } |
|
87 | + |
|
88 | + $sent = wp_mail( |
|
89 | + $this->to, |
|
90 | + $this->subject, |
|
91 | + $this->message, |
|
92 | + $this->headers, |
|
93 | + $this->attachments |
|
94 | + ); |
|
95 | + |
|
96 | + $this->reset(); |
|
97 | + |
|
98 | + return $sent; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + protected function buildHeaders(array $email) |
|
105 | + { |
|
106 | + $allowed = [ |
|
107 | + 'bcc', |
|
108 | + 'cc', |
|
109 | + 'from', |
|
110 | + 'reply-to', |
|
111 | + ]; |
|
112 | + |
|
113 | + $headers = array_intersect_key($email, array_flip($allowed)); |
|
114 | + $headers = array_filter($headers); |
|
115 | + |
|
116 | + foreach ($headers as $key => $value) { |
|
117 | + unset($headers[$key]); |
|
118 | + $headers[] = sprintf('%s: %s', $key, $value); |
|
119 | + } |
|
120 | + |
|
121 | + $headers[] = 'Content-Type: text/html'; |
|
122 | + |
|
123 | + return apply_filters('castor/email/headers', $headers, $this); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + protected function buildHtmlMessage(array $email) |
|
130 | + { |
|
131 | + $body = $this->renderTemplate('index'); |
|
132 | + $message = !empty($email['template']) |
|
133 | + ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
134 | + : $email['message']; |
|
135 | + |
|
136 | + $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
137 | + $message = str_replace('{message}', $message, $body); |
|
138 | + |
|
139 | + return apply_filters('castor/email/message', $message, 'html', $this); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param string $message |
|
144 | + * |
|
145 | + * @return string |
|
146 | + */ |
|
147 | + protected function buildPlainTextMessage($message) |
|
148 | + { |
|
149 | + return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param string $message |
|
154 | + * |
|
155 | + * @return string |
|
156 | + */ |
|
157 | + protected function filterHtml($message) |
|
158 | + { |
|
159 | + $message = strip_shortcodes($message); |
|
160 | + $message = wptexturize($message); |
|
161 | + $message = wpautop($message); |
|
162 | + $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
163 | + $message = stripslashes($message); |
|
164 | + return $message; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + protected function getDefaultFrom() |
|
171 | + { |
|
172 | + return sprintf('%s <%s>', |
|
173 | + wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
174 | + (string) get_option('admin_email') |
|
175 | + ); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + protected function normalize($email) |
|
182 | + { |
|
183 | + $defaults = array_fill_keys([ |
|
184 | + 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
|
185 | + 'template', 'template-tags', 'to', |
|
186 | + ], ''); |
|
187 | + $defaults['from'] = $this->getDefaultFrom(); |
|
188 | + |
|
189 | + $email = shortcode_atts($defaults, $email); |
|
190 | + |
|
191 | + foreach (['attachments', 'template-tags'] as $key) { |
|
192 | + $email[$key] = array_filter((array) $email[$key]); |
|
193 | + } |
|
194 | + if (empty($email['reply-to'])) { |
|
195 | + $email['reply-to'] = $email['from']; |
|
196 | + } |
|
197 | + |
|
198 | + return apply_filters('castor/email/compose', $email, $this); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Override by adding the custom template to "templates/castor/" in your theme. |
|
203 | + * |
|
204 | + * @param string $templatePath |
|
205 | + * |
|
206 | + * @return void|string |
|
207 | + */ |
|
208 | + protected function renderTemplate($templatePath, array $args = []) |
|
209 | + { |
|
210 | + $file = $this->template->get('castor/email/'.$templatePath); |
|
211 | + |
|
212 | + if (!file_exists($file)) { |
|
213 | + $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
214 | + } |
|
215 | + if (!file_exists($file)) { |
|
216 | + return; |
|
217 | + } |
|
218 | + |
|
219 | + ob_start(); |
|
220 | + include $file; |
|
221 | + $template = ob_get_clean(); |
|
222 | + |
|
223 | + return $this->renderTemplateString($template, $args); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @param string $template |
|
228 | + * |
|
229 | + * @return string |
|
230 | + */ |
|
231 | + protected function renderTemplateString($template, array $args = []) |
|
232 | + { |
|
233 | + foreach ($args as $key => $value) { |
|
234 | + $template = str_replace('{'.$key.'}', $value, $template); |
|
235 | + } |
|
236 | + return trim($template); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * @return void |
|
241 | + */ |
|
242 | + protected function reset() |
|
243 | + { |
|
244 | + $this->attachments = []; |
|
245 | + $this->headers = []; |
|
246 | + $this->message = null; |
|
247 | + $this->subject = null; |
|
248 | + $this->to = null; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * - remove invisible elements |
|
253 | + * - replace certain elements with a line-break |
|
254 | + * - replace certain table elements with a space |
|
255 | + * - add a placeholder for plain-text bullets to list elements |
|
256 | + * - strip all remaining HTML tags. |
|
257 | + * @return string |
|
258 | + */ |
|
259 | + protected function stripHtmlTags($string) |
|
260 | + { |
|
261 | + $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
262 | + $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
263 | + $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
264 | + $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
265 | + $string = wp_strip_all_tags($string); |
|
266 | + $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
267 | + $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
268 | + $string = str_replace('-o-^-o-', ' - ', $string); |
|
269 | + return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
270 | + } |
|
271 | 271 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | protected $to; |
38 | 38 | |
39 | - public function __construct(Template $template) |
|
39 | + public function __construct( Template $template ) |
|
40 | 40 | { |
41 | 41 | $this->template = $template; |
42 | 42 | } |
@@ -44,21 +44,21 @@ discard block |
||
44 | 44 | /** |
45 | 45 | * @return Email |
46 | 46 | */ |
47 | - public function compose(array $email) |
|
47 | + public function compose( array $email ) |
|
48 | 48 | { |
49 | - $email = $this->normalize($email); |
|
49 | + $email = $this->normalize( $email ); |
|
50 | 50 | |
51 | 51 | $this->attachments = $email['attachments']; |
52 | - $this->headers = $this->buildHeaders($email); |
|
53 | - $this->message = $this->buildHtmlMessage($email); |
|
52 | + $this->headers = $this->buildHeaders( $email ); |
|
53 | + $this->message = $this->buildHtmlMessage( $email ); |
|
54 | 54 | $this->subject = $email['subject']; |
55 | 55 | $this->to = $email['to']; |
56 | 56 | |
57 | - add_action('phpmailer_init', function ($phpmailer) { |
|
58 | - if ('text/plain' === $phpmailer->ContentType || !empty($phpmailer->AltBody)) { |
|
57 | + add_action( 'phpmailer_init', function( $phpmailer ) { |
|
58 | + if( 'text/plain' === $phpmailer->ContentType || !empty( $phpmailer->AltBody ) ) { |
|
59 | 59 | return; |
60 | 60 | } |
61 | - $phpmailer->AltBody = $this->buildPlainTextMessage($phpmailer->Body); |
|
61 | + $phpmailer->AltBody = $this->buildPlainTextMessage( $phpmailer->Body ); |
|
62 | 62 | }); |
63 | 63 | |
64 | 64 | return $this; |
@@ -69,10 +69,10 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return string|null |
71 | 71 | */ |
72 | - public function read($plaintext = false) |
|
72 | + public function read( $plaintext = false ) |
|
73 | 73 | { |
74 | 74 | return $plaintext |
75 | - ? $this->buildPlainTextMessage($this->message) |
|
75 | + ? $this->buildPlainTextMessage( $this->message ) |
|
76 | 76 | : $this->message; |
77 | 77 | } |
78 | 78 | |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | */ |
82 | 82 | public function send() |
83 | 83 | { |
84 | - if (!$this->message || !$this->subject || !$this->to) { |
|
84 | + if( !$this->message || !$this->subject || !$this->to ) { |
|
85 | 85 | return; |
86 | 86 | } |
87 | 87 | |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | /** |
102 | 102 | * @return array |
103 | 103 | */ |
104 | - protected function buildHeaders(array $email) |
|
104 | + protected function buildHeaders( array $email ) |
|
105 | 105 | { |
106 | 106 | $allowed = [ |
107 | 107 | 'bcc', |
@@ -110,33 +110,33 @@ discard block |
||
110 | 110 | 'reply-to', |
111 | 111 | ]; |
112 | 112 | |
113 | - $headers = array_intersect_key($email, array_flip($allowed)); |
|
114 | - $headers = array_filter($headers); |
|
113 | + $headers = array_intersect_key( $email, array_flip( $allowed ) ); |
|
114 | + $headers = array_filter( $headers ); |
|
115 | 115 | |
116 | - foreach ($headers as $key => $value) { |
|
117 | - unset($headers[$key]); |
|
118 | - $headers[] = sprintf('%s: %s', $key, $value); |
|
116 | + foreach( $headers as $key => $value ) { |
|
117 | + unset( $headers[$key] ); |
|
118 | + $headers[] = sprintf( '%s: %s', $key, $value ); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | $headers[] = 'Content-Type: text/html'; |
122 | 122 | |
123 | - return apply_filters('castor/email/headers', $headers, $this); |
|
123 | + return apply_filters( 'castor/email/headers', $headers, $this ); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
127 | 127 | * @return string |
128 | 128 | */ |
129 | - protected function buildHtmlMessage(array $email) |
|
129 | + protected function buildHtmlMessage( array $email ) |
|
130 | 130 | { |
131 | - $body = $this->renderTemplate('index'); |
|
132 | - $message = !empty($email['template']) |
|
133 | - ? $this->renderTemplate($email['template'], $email['template-tags']) |
|
131 | + $body = $this->renderTemplate( 'index' ); |
|
132 | + $message = !empty( $email['template'] ) |
|
133 | + ? $this->renderTemplate( $email['template'], $email['template-tags'] ) |
|
134 | 134 | : $email['message']; |
135 | 135 | |
136 | - $message = $this->filterHtml($email['before'].$message.$email['after']); |
|
137 | - $message = str_replace('{message}', $message, $body); |
|
136 | + $message = $this->filterHtml( $email['before'].$message.$email['after'] ); |
|
137 | + $message = str_replace( '{message}', $message, $body ); |
|
138 | 138 | |
139 | - return apply_filters('castor/email/message', $message, 'html', $this); |
|
139 | + return apply_filters( 'castor/email/message', $message, 'html', $this ); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | /** |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | * |
145 | 145 | * @return string |
146 | 146 | */ |
147 | - protected function buildPlainTextMessage($message) |
|
147 | + protected function buildPlainTextMessage( $message ) |
|
148 | 148 | { |
149 | - return apply_filters('castor/email/message', $this->stripHtmlTags($message), 'text', $this); |
|
149 | + return apply_filters( 'castor/email/message', $this->stripHtmlTags( $message ), 'text', $this ); |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | /** |
@@ -154,13 +154,13 @@ discard block |
||
154 | 154 | * |
155 | 155 | * @return string |
156 | 156 | */ |
157 | - protected function filterHtml($message) |
|
157 | + protected function filterHtml( $message ) |
|
158 | 158 | { |
159 | - $message = strip_shortcodes($message); |
|
160 | - $message = wptexturize($message); |
|
161 | - $message = wpautop($message); |
|
162 | - $message = str_replace(['<> ', ']]>'], ['', ']]>'], $message); |
|
163 | - $message = stripslashes($message); |
|
159 | + $message = strip_shortcodes( $message ); |
|
160 | + $message = wptexturize( $message ); |
|
161 | + $message = wpautop( $message ); |
|
162 | + $message = str_replace( ['<> ', ']]>'], ['', ']]>'], $message ); |
|
163 | + $message = stripslashes( $message ); |
|
164 | 164 | return $message; |
165 | 165 | } |
166 | 166 | |
@@ -169,33 +169,33 @@ discard block |
||
169 | 169 | */ |
170 | 170 | protected function getDefaultFrom() |
171 | 171 | { |
172 | - return sprintf('%s <%s>', |
|
173 | - wp_specialchars_decode((string) get_option('blogname'), ENT_QUOTES), |
|
174 | - (string) get_option('admin_email') |
|
172 | + return sprintf( '%s <%s>', |
|
173 | + wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ), |
|
174 | + (string) get_option( 'admin_email' ) |
|
175 | 175 | ); |
176 | 176 | } |
177 | 177 | |
178 | 178 | /** |
179 | 179 | * @return array |
180 | 180 | */ |
181 | - protected function normalize($email) |
|
181 | + protected function normalize( $email ) |
|
182 | 182 | { |
183 | - $defaults = array_fill_keys([ |
|
183 | + $defaults = array_fill_keys( [ |
|
184 | 184 | 'after', 'attachments', 'bcc', 'before', 'cc', 'from', 'message', 'reply-to', 'subject', |
185 | 185 | 'template', 'template-tags', 'to', |
186 | - ], ''); |
|
186 | + ], '' ); |
|
187 | 187 | $defaults['from'] = $this->getDefaultFrom(); |
188 | 188 | |
189 | - $email = shortcode_atts($defaults, $email); |
|
189 | + $email = shortcode_atts( $defaults, $email ); |
|
190 | 190 | |
191 | - foreach (['attachments', 'template-tags'] as $key) { |
|
192 | - $email[$key] = array_filter((array) $email[$key]); |
|
191 | + foreach( ['attachments', 'template-tags'] as $key ) { |
|
192 | + $email[$key] = array_filter( (array) $email[$key] ); |
|
193 | 193 | } |
194 | - if (empty($email['reply-to'])) { |
|
194 | + if( empty( $email['reply-to'] ) ) { |
|
195 | 195 | $email['reply-to'] = $email['from']; |
196 | 196 | } |
197 | 197 | |
198 | - return apply_filters('castor/email/compose', $email, $this); |
|
198 | + return apply_filters( 'castor/email/compose', $email, $this ); |
|
199 | 199 | } |
200 | 200 | |
201 | 201 | /** |
@@ -205,14 +205,14 @@ discard block |
||
205 | 205 | * |
206 | 206 | * @return void|string |
207 | 207 | */ |
208 | - protected function renderTemplate($templatePath, array $args = []) |
|
208 | + protected function renderTemplate( $templatePath, array $args = [] ) |
|
209 | 209 | { |
210 | - $file = $this->template->get('castor/email/'.$templatePath); |
|
210 | + $file = $this->template->get( 'castor/email/'.$templatePath ); |
|
211 | 211 | |
212 | - if (!file_exists($file)) { |
|
213 | - $file = sprintf('%s/templates/email/%s.php', dirname(dirname(__DIR__)), $templatePath); |
|
212 | + if( !file_exists( $file ) ) { |
|
213 | + $file = sprintf( '%s/templates/email/%s.php', dirname( dirname( __DIR__ ) ), $templatePath ); |
|
214 | 214 | } |
215 | - if (!file_exists($file)) { |
|
215 | + if( !file_exists( $file ) ) { |
|
216 | 216 | return; |
217 | 217 | } |
218 | 218 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | include $file; |
221 | 221 | $template = ob_get_clean(); |
222 | 222 | |
223 | - return $this->renderTemplateString($template, $args); |
|
223 | + return $this->renderTemplateString( $template, $args ); |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | /** |
@@ -228,12 +228,12 @@ discard block |
||
228 | 228 | * |
229 | 229 | * @return string |
230 | 230 | */ |
231 | - protected function renderTemplateString($template, array $args = []) |
|
231 | + protected function renderTemplateString( $template, array $args = [] ) |
|
232 | 232 | { |
233 | - foreach ($args as $key => $value) { |
|
234 | - $template = str_replace('{'.$key.'}', $value, $template); |
|
233 | + foreach( $args as $key => $value ) { |
|
234 | + $template = str_replace( '{'.$key.'}', $value, $template ); |
|
235 | 235 | } |
236 | - return trim($template); |
|
236 | + return trim( $template ); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
@@ -256,16 +256,16 @@ discard block |
||
256 | 256 | * - strip all remaining HTML tags. |
257 | 257 | * @return string |
258 | 258 | */ |
259 | - protected function stripHtmlTags($string) |
|
259 | + protected function stripHtmlTags( $string ) |
|
260 | 260 | { |
261 | - $string = preg_replace('@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string); |
|
262 | - $string = preg_replace('@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string); |
|
263 | - $string = preg_replace('@</(td|th)@iu', ' $0', $string); |
|
264 | - $string = preg_replace('@<(li)[^>]*?>@siu', '$0-o-^-o-', $string); |
|
265 | - $string = wp_strip_all_tags($string); |
|
266 | - $string = wp_specialchars_decode($string, ENT_QUOTES); |
|
267 | - $string = preg_replace('/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string); |
|
268 | - $string = str_replace('-o-^-o-', ' - ', $string); |
|
269 | - return html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
261 | + $string = preg_replace( '@<(embed|head|noembed|noscript|object|script|style)[^>]*?>.*?</\\1>@siu', '', $string ); |
|
262 | + $string = preg_replace( '@</(div|h[1-9]|p|pre|tr)@iu', "\r\n\$0", $string ); |
|
263 | + $string = preg_replace( '@</(td|th)@iu', ' $0', $string ); |
|
264 | + $string = preg_replace( '@<(li)[^>]*?>@siu', '$0-o-^-o-', $string ); |
|
265 | + $string = wp_strip_all_tags( $string ); |
|
266 | + $string = wp_specialchars_decode( $string, ENT_QUOTES ); |
|
267 | + $string = preg_replace( '/\v(?:[\v\h]+){2,}/', "\r\n\r\n", $string ); |
|
268 | + $string = str_replace( '-o-^-o-', ' - ', $string ); |
|
269 | + return html_entity_decode( $string, ENT_QUOTES, 'UTF-8' ); |
|
270 | 270 | } |
271 | 271 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use GeminiLabs\Castor\Helpers\Template; |
6 | 6 | |
7 | -class Email |
|
8 | -{ |
|
7 | +class Email { |
|
9 | 8 | /** |
10 | 9 | * @var Template |
11 | 10 | */ |