@@ -13,19 +13,19 @@ |
||
13 | 13 | */ |
14 | 14 | class AggregateOffer extends BaseType |
15 | 15 | { |
16 | - /** |
|
17 | - * @var array |
|
18 | - * @see http://schema.org/{property_name} |
|
19 | - */ |
|
20 | - public $allowed = [ |
|
21 | - 'highPrice', 'lowPrice', 'offerCount', 'offers', |
|
22 | - ]; |
|
16 | + /** |
|
17 | + * @var array |
|
18 | + * @see http://schema.org/{property_name} |
|
19 | + */ |
|
20 | + public $allowed = [ |
|
21 | + 'highPrice', 'lowPrice', 'offerCount', 'offers', |
|
22 | + ]; |
|
23 | 23 | |
24 | - /** |
|
25 | - * @var array |
|
26 | - * @see http://schema.org/{property_name} |
|
27 | - */ |
|
28 | - public $parents = [ |
|
29 | - 'Offer', |
|
30 | - ]; |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + * @see http://schema.org/{property_name} |
|
27 | + */ |
|
28 | + public $parents = [ |
|
29 | + 'Offer', |
|
30 | + ]; |
|
31 | 31 | } |
@@ -12,248 +12,248 @@ |
||
12 | 12 | |
13 | 13 | abstract class BaseType implements ArrayAccess, JsonSerializable, Type |
14 | 14 | { |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - public $allowed = []; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + public $allowed = []; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - public $parents = []; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + public $parents = []; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $properties = []; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $properties = []; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $type; |
|
30 | + /** |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $type; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string $method |
|
37 | - * @return static |
|
38 | - */ |
|
39 | - public function __call($method, array $arguments) |
|
40 | - { |
|
41 | - return $this->setProperty($method, glsr_get($arguments, 0)); |
|
42 | - } |
|
35 | + /** |
|
36 | + * @param string $method |
|
37 | + * @return static |
|
38 | + */ |
|
39 | + public function __call($method, array $arguments) |
|
40 | + { |
|
41 | + return $this->setProperty($method, glsr_get($arguments, 0)); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param string $type |
|
46 | - */ |
|
47 | - public function __construct($type = null) |
|
48 | - { |
|
49 | - $this->type = !is_string($type) |
|
50 | - ? (new ReflectionClass($this))->getShortName() |
|
51 | - : $type; |
|
52 | - $this->setAllowedProperties(); |
|
53 | - } |
|
44 | + /** |
|
45 | + * @param string $type |
|
46 | + */ |
|
47 | + public function __construct($type = null) |
|
48 | + { |
|
49 | + $this->type = !is_string($type) |
|
50 | + ? (new ReflectionClass($this))->getShortName() |
|
51 | + : $type; |
|
52 | + $this->setAllowedProperties(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function __toString() |
|
59 | - { |
|
60 | - return $this->toScript(); |
|
61 | - } |
|
55 | + /** |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function __toString() |
|
59 | + { |
|
60 | + return $this->toScript(); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * @return static |
|
65 | - */ |
|
66 | - public function addProperties(array $properties) |
|
67 | - { |
|
68 | - foreach ($properties as $property => $value) { |
|
69 | - $this->setProperty($property, $value); |
|
70 | - } |
|
71 | - return $this; |
|
72 | - } |
|
63 | + /** |
|
64 | + * @return static |
|
65 | + */ |
|
66 | + public function addProperties(array $properties) |
|
67 | + { |
|
68 | + foreach ($properties as $property => $value) { |
|
69 | + $this->setProperty($property, $value); |
|
70 | + } |
|
71 | + return $this; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function getContext() |
|
78 | - { |
|
79 | - return 'https://schema.org'; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function getContext() |
|
78 | + { |
|
79 | + return 'https://schema.org'; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return array |
|
84 | - */ |
|
85 | - public function getProperties() |
|
86 | - { |
|
87 | - return $this->properties; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return array |
|
84 | + */ |
|
85 | + public function getProperties() |
|
86 | + { |
|
87 | + return $this->properties; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @param string $property |
|
92 | - * @param mixed $default |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - public function getProperty($property, $default = null) |
|
96 | - { |
|
97 | - return glsr_get($this->properties, $property, $default); |
|
98 | - } |
|
90 | + /** |
|
91 | + * @param string $property |
|
92 | + * @param mixed $default |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + public function getProperty($property, $default = null) |
|
96 | + { |
|
97 | + return glsr_get($this->properties, $property, $default); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function getType() |
|
104 | - { |
|
105 | - return $this->type; |
|
106 | - } |
|
100 | + /** |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function getType() |
|
104 | + { |
|
105 | + return $this->type; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @param bool $condition |
|
110 | - * @param mixed $callback |
|
111 | - * @return static |
|
112 | - */ |
|
113 | - public function doIf($condition, $callback) |
|
114 | - { |
|
115 | - if ($condition) { |
|
116 | - $callback($this); |
|
117 | - } |
|
118 | - return $this; |
|
119 | - } |
|
108 | + /** |
|
109 | + * @param bool $condition |
|
110 | + * @param mixed $callback |
|
111 | + * @return static |
|
112 | + */ |
|
113 | + public function doIf($condition, $callback) |
|
114 | + { |
|
115 | + if ($condition) { |
|
116 | + $callback($this); |
|
117 | + } |
|
118 | + return $this; |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * @return array |
|
123 | - */ |
|
124 | - public function jsonSerialize() |
|
125 | - { |
|
126 | - return $this->toArray(); |
|
127 | - } |
|
121 | + /** |
|
122 | + * @return array |
|
123 | + */ |
|
124 | + public function jsonSerialize() |
|
125 | + { |
|
126 | + return $this->toArray(); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * @param mixed $offset |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function offsetExists($offset) |
|
134 | - { |
|
135 | - return array_key_exists($offset, $this->properties); |
|
136 | - } |
|
129 | + /** |
|
130 | + * @param mixed $offset |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function offsetExists($offset) |
|
134 | + { |
|
135 | + return array_key_exists($offset, $this->properties); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @param string $offset |
|
140 | - * @return mixed |
|
141 | - */ |
|
142 | - public function offsetGet($offset) |
|
143 | - { |
|
144 | - return $this->getProperty($offset); |
|
145 | - } |
|
138 | + /** |
|
139 | + * @param string $offset |
|
140 | + * @return mixed |
|
141 | + */ |
|
142 | + public function offsetGet($offset) |
|
143 | + { |
|
144 | + return $this->getProperty($offset); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * @param string $offset |
|
149 | - * @param mixed $value |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public function offsetSet($offset, $value) |
|
153 | - { |
|
154 | - $this->setProperty($offset, $value); |
|
155 | - } |
|
147 | + /** |
|
148 | + * @param string $offset |
|
149 | + * @param mixed $value |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public function offsetSet($offset, $value) |
|
153 | + { |
|
154 | + $this->setProperty($offset, $value); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * @param string $offset |
|
159 | - * @return void |
|
160 | - */ |
|
161 | - public function offsetUnset($offset) |
|
162 | - { |
|
163 | - unset($this->properties[$offset]); |
|
164 | - } |
|
157 | + /** |
|
158 | + * @param string $offset |
|
159 | + * @return void |
|
160 | + */ |
|
161 | + public function offsetUnset($offset) |
|
162 | + { |
|
163 | + unset($this->properties[$offset]); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @param string $property |
|
168 | - * @param mixed $value |
|
169 | - * @return static |
|
170 | - */ |
|
171 | - public function setProperty($property, $value) |
|
172 | - { |
|
173 | - if (!in_array($property, $this->allowed) |
|
174 | - && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
175 | - glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
176 | - return $this; |
|
177 | - } |
|
178 | - $this->properties[$property] = $value; |
|
179 | - return $this; |
|
180 | - } |
|
166 | + /** |
|
167 | + * @param string $property |
|
168 | + * @param mixed $value |
|
169 | + * @return static |
|
170 | + */ |
|
171 | + public function setProperty($property, $value) |
|
172 | + { |
|
173 | + if (!in_array($property, $this->allowed) |
|
174 | + && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
175 | + glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
176 | + return $this; |
|
177 | + } |
|
178 | + $this->properties[$property] = $value; |
|
179 | + return $this; |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public function toArray() |
|
186 | - { |
|
187 | - return [ |
|
188 | - '@context' => $this->getContext(), |
|
189 | - '@type' => $this->getType(), |
|
190 | - ] + $this->serializeProperty($this->getProperties()); |
|
191 | - } |
|
182 | + /** |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public function toArray() |
|
186 | + { |
|
187 | + return [ |
|
188 | + '@context' => $this->getContext(), |
|
189 | + '@type' => $this->getType(), |
|
190 | + ] + $this->serializeProperty($this->getProperties()); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * @return string |
|
195 | - */ |
|
196 | - public function toScript() |
|
197 | - { |
|
198 | - return sprintf('<script type="application/ld+json">%s</script>', |
|
199 | - json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
200 | - ); |
|
201 | - } |
|
193 | + /** |
|
194 | + * @return string |
|
195 | + */ |
|
196 | + public function toScript() |
|
197 | + { |
|
198 | + return sprintf('<script type="application/ld+json">%s</script>', |
|
199 | + json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
200 | + ); |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * @param array|null $parents |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - protected function getParents($parents = null) |
|
208 | - { |
|
209 | - if (!isset($parents)) { |
|
210 | - $parents = $this->parents; |
|
211 | - } |
|
212 | - $newParents = $parents; |
|
213 | - foreach ($parents as $parent) { |
|
214 | - $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
215 | - if (!class_exists($parentClass)) { |
|
216 | - continue; |
|
217 | - } |
|
218 | - $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
219 | - } |
|
220 | - return array_values(array_unique($newParents)); |
|
221 | - } |
|
203 | + /** |
|
204 | + * @param array|null $parents |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + protected function getParents($parents = null) |
|
208 | + { |
|
209 | + if (!isset($parents)) { |
|
210 | + $parents = $this->parents; |
|
211 | + } |
|
212 | + $newParents = $parents; |
|
213 | + foreach ($parents as $parent) { |
|
214 | + $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
215 | + if (!class_exists($parentClass)) { |
|
216 | + continue; |
|
217 | + } |
|
218 | + $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
219 | + } |
|
220 | + return array_values(array_unique($newParents)); |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @return void |
|
225 | - */ |
|
226 | - protected function setAllowedProperties() |
|
227 | - { |
|
228 | - $parents = $this->getParents(); |
|
229 | - foreach ($parents as $parent) { |
|
230 | - $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
231 | - if (!class_exists($parentClass)) { |
|
232 | - continue; |
|
233 | - } |
|
234 | - $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
235 | - } |
|
236 | - } |
|
223 | + /** |
|
224 | + * @return void |
|
225 | + */ |
|
226 | + protected function setAllowedProperties() |
|
227 | + { |
|
228 | + $parents = $this->getParents(); |
|
229 | + foreach ($parents as $parent) { |
|
230 | + $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
231 | + if (!class_exists($parentClass)) { |
|
232 | + continue; |
|
233 | + } |
|
234 | + $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
235 | + } |
|
236 | + } |
|
237 | 237 | |
238 | - /** |
|
239 | - * @param mixed $property |
|
240 | - * @return array|string |
|
241 | - */ |
|
242 | - protected function serializeProperty($property) |
|
243 | - { |
|
244 | - if (is_array($property)) { |
|
245 | - return array_map([$this, 'serializeProperty'], $property); |
|
246 | - } |
|
247 | - if ($property instanceof Type) { |
|
248 | - $property = $property->toArray(); |
|
249 | - unset($property['@context']); |
|
250 | - } |
|
251 | - if ($property instanceof DateTimeInterface) { |
|
252 | - $property = $property->format(DateTime::ATOM); |
|
253 | - } |
|
254 | - if (is_object($property)) { |
|
255 | - throw new InvalidProperty(); |
|
256 | - } |
|
257 | - return $property; |
|
258 | - } |
|
238 | + /** |
|
239 | + * @param mixed $property |
|
240 | + * @return array|string |
|
241 | + */ |
|
242 | + protected function serializeProperty($property) |
|
243 | + { |
|
244 | + if (is_array($property)) { |
|
245 | + return array_map([$this, 'serializeProperty'], $property); |
|
246 | + } |
|
247 | + if ($property instanceof Type) { |
|
248 | + $property = $property->toArray(); |
|
249 | + unset($property['@context']); |
|
250 | + } |
|
251 | + if ($property instanceof DateTimeInterface) { |
|
252 | + $property = $property->format(DateTime::ATOM); |
|
253 | + } |
|
254 | + if (is_object($property)) { |
|
255 | + throw new InvalidProperty(); |
|
256 | + } |
|
257 | + return $property; |
|
258 | + } |
|
259 | 259 | } |
@@ -36,18 +36,18 @@ discard block |
||
36 | 36 | * @param string $method |
37 | 37 | * @return static |
38 | 38 | */ |
39 | - public function __call($method, array $arguments) |
|
39 | + public function __call( $method, array $arguments ) |
|
40 | 40 | { |
41 | - return $this->setProperty($method, glsr_get($arguments, 0)); |
|
41 | + return $this->setProperty( $method, glsr_get( $arguments, 0 ) ); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * @param string $type |
46 | 46 | */ |
47 | - public function __construct($type = null) |
|
47 | + public function __construct( $type = null ) |
|
48 | 48 | { |
49 | - $this->type = !is_string($type) |
|
50 | - ? (new ReflectionClass($this))->getShortName() |
|
49 | + $this->type = !is_string( $type ) |
|
50 | + ? (new ReflectionClass( $this ))->getShortName() |
|
51 | 51 | : $type; |
52 | 52 | $this->setAllowedProperties(); |
53 | 53 | } |
@@ -63,10 +63,10 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * @return static |
65 | 65 | */ |
66 | - public function addProperties(array $properties) |
|
66 | + public function addProperties( array $properties ) |
|
67 | 67 | { |
68 | - foreach ($properties as $property => $value) { |
|
69 | - $this->setProperty($property, $value); |
|
68 | + foreach( $properties as $property => $value ) { |
|
69 | + $this->setProperty( $property, $value ); |
|
70 | 70 | } |
71 | 71 | return $this; |
72 | 72 | } |
@@ -92,9 +92,9 @@ discard block |
||
92 | 92 | * @param mixed $default |
93 | 93 | * @return mixed |
94 | 94 | */ |
95 | - public function getProperty($property, $default = null) |
|
95 | + public function getProperty( $property, $default = null ) |
|
96 | 96 | { |
97 | - return glsr_get($this->properties, $property, $default); |
|
97 | + return glsr_get( $this->properties, $property, $default ); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -110,10 +110,10 @@ discard block |
||
110 | 110 | * @param mixed $callback |
111 | 111 | * @return static |
112 | 112 | */ |
113 | - public function doIf($condition, $callback) |
|
113 | + public function doIf( $condition, $callback ) |
|
114 | 114 | { |
115 | - if ($condition) { |
|
116 | - $callback($this); |
|
115 | + if( $condition ) { |
|
116 | + $callback( $this ); |
|
117 | 117 | } |
118 | 118 | return $this; |
119 | 119 | } |
@@ -130,18 +130,18 @@ discard block |
||
130 | 130 | * @param mixed $offset |
131 | 131 | * @return bool |
132 | 132 | */ |
133 | - public function offsetExists($offset) |
|
133 | + public function offsetExists( $offset ) |
|
134 | 134 | { |
135 | - return array_key_exists($offset, $this->properties); |
|
135 | + return array_key_exists( $offset, $this->properties ); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | 139 | * @param string $offset |
140 | 140 | * @return mixed |
141 | 141 | */ |
142 | - public function offsetGet($offset) |
|
142 | + public function offsetGet( $offset ) |
|
143 | 143 | { |
144 | - return $this->getProperty($offset); |
|
144 | + return $this->getProperty( $offset ); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -149,16 +149,16 @@ discard block |
||
149 | 149 | * @param mixed $value |
150 | 150 | * @return void |
151 | 151 | */ |
152 | - public function offsetSet($offset, $value) |
|
152 | + public function offsetSet( $offset, $value ) |
|
153 | 153 | { |
154 | - $this->setProperty($offset, $value); |
|
154 | + $this->setProperty( $offset, $value ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
158 | 158 | * @param string $offset |
159 | 159 | * @return void |
160 | 160 | */ |
161 | - public function offsetUnset($offset) |
|
161 | + public function offsetUnset( $offset ) |
|
162 | 162 | { |
163 | 163 | unset($this->properties[$offset]); |
164 | 164 | } |
@@ -168,11 +168,11 @@ discard block |
||
168 | 168 | * @param mixed $value |
169 | 169 | * @return static |
170 | 170 | */ |
171 | - public function setProperty($property, $value) |
|
171 | + public function setProperty( $property, $value ) |
|
172 | 172 | { |
173 | - if (!in_array($property, $this->allowed) |
|
174 | - && 'UnknownType' != (new ReflectionClass($this))->getShortName()) { |
|
175 | - glsr_log()->warning($this->getType().' does not allow the "'.$property.'" property'); |
|
173 | + if( !in_array( $property, $this->allowed ) |
|
174 | + && 'UnknownType' != (new ReflectionClass( $this ))->getShortName() ) { |
|
175 | + glsr_log()->warning( $this->getType().' does not allow the "'.$property.'" property' ); |
|
176 | 176 | return $this; |
177 | 177 | } |
178 | 178 | $this->properties[$property] = $value; |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | return [ |
188 | 188 | '@context' => $this->getContext(), |
189 | 189 | '@type' => $this->getType(), |
190 | - ] + $this->serializeProperty($this->getProperties()); |
|
190 | + ] + $this->serializeProperty( $this->getProperties() ); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | /** |
@@ -195,8 +195,8 @@ discard block |
||
195 | 195 | */ |
196 | 196 | public function toScript() |
197 | 197 | { |
198 | - return sprintf('<script type="application/ld+json">%s</script>', |
|
199 | - json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
|
198 | + return sprintf( '<script type="application/ld+json">%s</script>', |
|
199 | + json_encode( $this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) |
|
200 | 200 | ); |
201 | 201 | } |
202 | 202 | |
@@ -204,20 +204,20 @@ discard block |
||
204 | 204 | * @param array|null $parents |
205 | 205 | * @return array |
206 | 206 | */ |
207 | - protected function getParents($parents = null) |
|
207 | + protected function getParents( $parents = null ) |
|
208 | 208 | { |
209 | - if (!isset($parents)) { |
|
209 | + if( !isset($parents) ) { |
|
210 | 210 | $parents = $this->parents; |
211 | 211 | } |
212 | 212 | $newParents = $parents; |
213 | - foreach ($parents as $parent) { |
|
214 | - $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
215 | - if (!class_exists($parentClass)) { |
|
213 | + foreach( $parents as $parent ) { |
|
214 | + $parentClass = glsr( Helper::class )->buildClassName( $parent, __NAMESPACE__ ); |
|
215 | + if( !class_exists( $parentClass ) ) { |
|
216 | 216 | continue; |
217 | 217 | } |
218 | - $newParents = array_merge($newParents, $this->getParents((new $parentClass())->parents)); |
|
218 | + $newParents = array_merge( $newParents, $this->getParents( (new $parentClass())->parents ) ); |
|
219 | 219 | } |
220 | - return array_values(array_unique($newParents)); |
|
220 | + return array_values( array_unique( $newParents ) ); |
|
221 | 221 | } |
222 | 222 | |
223 | 223 | /** |
@@ -226,12 +226,12 @@ discard block |
||
226 | 226 | protected function setAllowedProperties() |
227 | 227 | { |
228 | 228 | $parents = $this->getParents(); |
229 | - foreach ($parents as $parent) { |
|
230 | - $parentClass = glsr(Helper::class)->buildClassName($parent, __NAMESPACE__); |
|
231 | - if (!class_exists($parentClass)) { |
|
229 | + foreach( $parents as $parent ) { |
|
230 | + $parentClass = glsr( Helper::class )->buildClassName( $parent, __NAMESPACE__ ); |
|
231 | + if( !class_exists( $parentClass ) ) { |
|
232 | 232 | continue; |
233 | 233 | } |
234 | - $this->allowed = array_values(array_unique(array_merge((new $parentClass())->allowed, $this->allowed))); |
|
234 | + $this->allowed = array_values( array_unique( array_merge( (new $parentClass())->allowed, $this->allowed ) ) ); |
|
235 | 235 | } |
236 | 236 | } |
237 | 237 | |
@@ -239,19 +239,19 @@ discard block |
||
239 | 239 | * @param mixed $property |
240 | 240 | * @return array|string |
241 | 241 | */ |
242 | - protected function serializeProperty($property) |
|
242 | + protected function serializeProperty( $property ) |
|
243 | 243 | { |
244 | - if (is_array($property)) { |
|
245 | - return array_map([$this, 'serializeProperty'], $property); |
|
244 | + if( is_array( $property ) ) { |
|
245 | + return array_map( [$this, 'serializeProperty'], $property ); |
|
246 | 246 | } |
247 | - if ($property instanceof Type) { |
|
247 | + if( $property instanceof Type ) { |
|
248 | 248 | $property = $property->toArray(); |
249 | 249 | unset($property['@context']); |
250 | 250 | } |
251 | - if ($property instanceof DateTimeInterface) { |
|
252 | - $property = $property->format(DateTime::ATOM); |
|
251 | + if( $property instanceof DateTimeInterface ) { |
|
252 | + $property = $property->format( DateTime::ATOM ); |
|
253 | 253 | } |
254 | - if (is_object($property)) { |
|
254 | + if( is_object( $property ) ) { |
|
255 | 255 | throw new InvalidProperty(); |
256 | 256 | } |
257 | 257 | return $property; |
@@ -12,19 +12,19 @@ |
||
12 | 12 | */ |
13 | 13 | class Rating extends BaseType |
14 | 14 | { |
15 | - /** |
|
16 | - * @var array |
|
17 | - * @see http://schema.org/{property_name} |
|
18 | - */ |
|
19 | - public $allowed = [ |
|
20 | - 'author', 'bestRating', 'ratingValue', 'worstRating', |
|
21 | - ]; |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + * @see http://schema.org/{property_name} |
|
18 | + */ |
|
19 | + public $allowed = [ |
|
20 | + 'author', 'bestRating', 'ratingValue', 'worstRating', |
|
21 | + ]; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var array |
|
25 | - * @see http://schema.org/{property_name} |
|
26 | - */ |
|
27 | - public $parents = [ |
|
28 | - 'Intangible', |
|
29 | - ]; |
|
23 | + /** |
|
24 | + * @var array |
|
25 | + * @see http://schema.org/{property_name} |
|
26 | + */ |
|
27 | + public $parents = [ |
|
28 | + 'Intangible', |
|
29 | + ]; |
|
30 | 30 | } |
@@ -12,302 +12,302 @@ |
||
12 | 12 | */ |
13 | 13 | class Validator |
14 | 14 | { |
15 | - use ValidationRules; |
|
15 | + use ValidationRules; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - public $errors = []; |
|
17 | + /** |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + public $errors = []; |
|
21 | 21 | |
22 | - /** |
|
23 | - * The data under validation. |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $data = []; |
|
22 | + /** |
|
23 | + * The data under validation. |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $data = []; |
|
27 | 27 | |
28 | - /** |
|
29 | - * The failed validation rules. |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $failedRules = []; |
|
28 | + /** |
|
29 | + * The failed validation rules. |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $failedRules = []; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The rules to be applied to the data. |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $rules = []; |
|
34 | + /** |
|
35 | + * The rules to be applied to the data. |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $rules = []; |
|
39 | 39 | |
40 | - /** |
|
41 | - * The size related validation rules. |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected $sizeRules = [ |
|
45 | - 'Between', 'Max', 'Min', |
|
46 | - ]; |
|
40 | + /** |
|
41 | + * The size related validation rules. |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected $sizeRules = [ |
|
45 | + 'Between', 'Max', 'Min', |
|
46 | + ]; |
|
47 | 47 | |
48 | - /** |
|
49 | - * The validation rules that imply the field is required. |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $implicitRules = [ |
|
53 | - 'Required', |
|
54 | - ]; |
|
48 | + /** |
|
49 | + * The validation rules that imply the field is required. |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $implicitRules = [ |
|
53 | + 'Required', |
|
54 | + ]; |
|
55 | 55 | |
56 | - /** |
|
57 | - * The numeric related validation rules. |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $numericRules = [ |
|
61 | - 'Number', |
|
62 | - ]; |
|
56 | + /** |
|
57 | + * The numeric related validation rules. |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $numericRules = [ |
|
61 | + 'Number', |
|
62 | + ]; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Run the validator's rules against its data. |
|
66 | - * @param mixed $data |
|
67 | - * @return array |
|
68 | - */ |
|
69 | - public function validate($data, array $rules = []) |
|
70 | - { |
|
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
77 | - break; |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - return $this->errors; |
|
82 | - } |
|
64 | + /** |
|
65 | + * Run the validator's rules against its data. |
|
66 | + * @param mixed $data |
|
67 | + * @return array |
|
68 | + */ |
|
69 | + public function validate($data, array $rules = []) |
|
70 | + { |
|
71 | + $this->normalizeData($data); |
|
72 | + $this->setRules($rules); |
|
73 | + foreach ($this->rules as $attribute => $rules) { |
|
74 | + foreach ($rules as $rule) { |
|
75 | + $this->validateAttribute($attribute, $rule); |
|
76 | + if ($this->shouldStopValidating($attribute)) { |
|
77 | + break; |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + return $this->errors; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Validate a given attribute against a rule. |
|
86 | - * @param string $attribute |
|
87 | - * @param string $rule |
|
88 | - * @return void |
|
89 | - * @throws BadMethodCallException |
|
90 | - */ |
|
91 | - public function validateAttribute($attribute, $rule) |
|
92 | - { |
|
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
95 | - return; |
|
96 | - } |
|
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | - } |
|
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
103 | - } |
|
104 | - } |
|
84 | + /** |
|
85 | + * Validate a given attribute against a rule. |
|
86 | + * @param string $attribute |
|
87 | + * @param string $rule |
|
88 | + * @return void |
|
89 | + * @throws BadMethodCallException |
|
90 | + */ |
|
91 | + public function validateAttribute($attribute, $rule) |
|
92 | + { |
|
93 | + list($rule, $parameters) = $this->parseRule($rule); |
|
94 | + if ('' == $rule) { |
|
95 | + return; |
|
96 | + } |
|
97 | + $value = $this->getValue($attribute); |
|
98 | + if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | + throw new BadMethodCallException("Method [$method] does not exist."); |
|
100 | + } |
|
101 | + if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | + $this->addFailure($attribute, $rule, $parameters); |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Add an error message to the validator's collection of errors. |
|
108 | - * @param string $attribute |
|
109 | - * @param string $rule |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - protected function addError($attribute, $rule, array $parameters) |
|
113 | - { |
|
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | - $this->errors[$attribute][] = $message; |
|
116 | - } |
|
106 | + /** |
|
107 | + * Add an error message to the validator's collection of errors. |
|
108 | + * @param string $attribute |
|
109 | + * @param string $rule |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + protected function addError($attribute, $rule, array $parameters) |
|
113 | + { |
|
114 | + $message = $this->getMessage($attribute, $rule, $parameters); |
|
115 | + $this->errors[$attribute][] = $message; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Add a failed rule and error message to the collection. |
|
120 | - * @param string $attribute |
|
121 | - * @param string $rule |
|
122 | - * @return void |
|
123 | - */ |
|
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
125 | - { |
|
126 | - $this->addError($attribute, $rule, $parameters); |
|
127 | - $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | - } |
|
118 | + /** |
|
119 | + * Add a failed rule and error message to the collection. |
|
120 | + * @param string $attribute |
|
121 | + * @param string $rule |
|
122 | + * @return void |
|
123 | + */ |
|
124 | + protected function addFailure($attribute, $rule, array $parameters) |
|
125 | + { |
|
126 | + $this->addError($attribute, $rule, $parameters); |
|
127 | + $this->failedRules[$attribute][$rule] = $parameters; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Get the data type of the given attribute. |
|
132 | - * @param string $attribute |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function getAttributeType($attribute) |
|
136 | - { |
|
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
138 | - ? 'length' |
|
139 | - : ''; |
|
140 | - } |
|
130 | + /** |
|
131 | + * Get the data type of the given attribute. |
|
132 | + * @param string $attribute |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function getAttributeType($attribute) |
|
136 | + { |
|
137 | + return !$this->hasRule($attribute, $this->numericRules) |
|
138 | + ? 'length' |
|
139 | + : ''; |
|
140 | + } |
|
141 | 141 | |
142 | - /** |
|
143 | - * Get the validation message for an attribute and rule. |
|
144 | - * @param string $attribute |
|
145 | - * @param string $rule |
|
146 | - * @return string|null |
|
147 | - */ |
|
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
149 | - { |
|
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | - } |
|
153 | - $lowerRule = glsr(Helper::class)->snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
155 | - } |
|
142 | + /** |
|
143 | + * Get the validation message for an attribute and rule. |
|
144 | + * @param string $attribute |
|
145 | + * @param string $rule |
|
146 | + * @return string|null |
|
147 | + */ |
|
148 | + protected function getMessage($attribute, $rule, array $parameters) |
|
149 | + { |
|
150 | + if (in_array($rule, $this->sizeRules)) { |
|
151 | + return $this->getSizeMessage($attribute, $rule, $parameters); |
|
152 | + } |
|
153 | + $lowerRule = glsr(Helper::class)->snakeCase($rule); |
|
154 | + return $this->translator($lowerRule, $parameters); |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * Get a rule and its parameters for a given attribute. |
|
159 | - * @param string $attribute |
|
160 | - * @param string|array $rules |
|
161 | - * @return array|null |
|
162 | - */ |
|
163 | - protected function getRule($attribute, $rules) |
|
164 | - { |
|
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
166 | - return; |
|
167 | - } |
|
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
172 | - return [$rule, $parameters]; |
|
173 | - } |
|
174 | - } |
|
175 | - } |
|
157 | + /** |
|
158 | + * Get a rule and its parameters for a given attribute. |
|
159 | + * @param string $attribute |
|
160 | + * @param string|array $rules |
|
161 | + * @return array|null |
|
162 | + */ |
|
163 | + protected function getRule($attribute, $rules) |
|
164 | + { |
|
165 | + if (!array_key_exists($attribute, $this->rules)) { |
|
166 | + return; |
|
167 | + } |
|
168 | + $rules = (array) $rules; |
|
169 | + foreach ($this->rules[$attribute] as $rule) { |
|
170 | + list($rule, $parameters) = $this->parseRule($rule); |
|
171 | + if (in_array($rule, $rules)) { |
|
172 | + return [$rule, $parameters]; |
|
173 | + } |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get the size of an attribute. |
|
179 | - * @param string $attribute |
|
180 | - * @param mixed $value |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - protected function getSize($attribute, $value) |
|
184 | - { |
|
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
187 | - return $value; |
|
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
190 | - } |
|
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
194 | - } |
|
177 | + /** |
|
178 | + * Get the size of an attribute. |
|
179 | + * @param string $attribute |
|
180 | + * @param mixed $value |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + protected function getSize($attribute, $value) |
|
184 | + { |
|
185 | + $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | + if (is_numeric($value) && $hasNumeric) { |
|
187 | + return $value; |
|
188 | + } elseif (is_array($value)) { |
|
189 | + return count($value); |
|
190 | + } |
|
191 | + return function_exists('mb_strlen') |
|
192 | + ? mb_strlen($value) |
|
193 | + : strlen($value); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * Get the proper error message for an attribute and size rule. |
|
198 | - * @param string $attribute |
|
199 | - * @param string $rule |
|
200 | - * @return string|null |
|
201 | - */ |
|
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | - { |
|
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = glsr(Helper::class)->snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
207 | - } |
|
196 | + /** |
|
197 | + * Get the proper error message for an attribute and size rule. |
|
198 | + * @param string $attribute |
|
199 | + * @param string $rule |
|
200 | + * @return string|null |
|
201 | + */ |
|
202 | + protected function getSizeMessage($attribute, $rule, array $parameters) |
|
203 | + { |
|
204 | + $type = $this->getAttributeType($attribute); |
|
205 | + $lowerRule = glsr(Helper::class)->snakeCase($rule.$type); |
|
206 | + return $this->translator($lowerRule, $parameters); |
|
207 | + } |
|
208 | 208 | |
209 | - /** |
|
210 | - * Get the value of a given attribute. |
|
211 | - * @param string $attribute |
|
212 | - * @return mixed |
|
213 | - */ |
|
214 | - protected function getValue($attribute) |
|
215 | - { |
|
216 | - if (isset($this->data[$attribute])) { |
|
217 | - return $this->data[$attribute]; |
|
218 | - } |
|
219 | - } |
|
209 | + /** |
|
210 | + * Get the value of a given attribute. |
|
211 | + * @param string $attribute |
|
212 | + * @return mixed |
|
213 | + */ |
|
214 | + protected function getValue($attribute) |
|
215 | + { |
|
216 | + if (isset($this->data[$attribute])) { |
|
217 | + return $this->data[$attribute]; |
|
218 | + } |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Determine if the given attribute has a rule in the given set. |
|
223 | - * @param string $attribute |
|
224 | - * @param string|array $rules |
|
225 | - * @return bool |
|
226 | - */ |
|
227 | - protected function hasRule($attribute, $rules) |
|
228 | - { |
|
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
230 | - } |
|
221 | + /** |
|
222 | + * Determine if the given attribute has a rule in the given set. |
|
223 | + * @param string $attribute |
|
224 | + * @param string|array $rules |
|
225 | + * @return bool |
|
226 | + */ |
|
227 | + protected function hasRule($attribute, $rules) |
|
228 | + { |
|
229 | + return !is_null($this->getRule($attribute, $rules)); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Normalize the provided data to an array. |
|
234 | - * @param mixed $data |
|
235 | - * @return void |
|
236 | - */ |
|
237 | - protected function normalizeData($data) |
|
238 | - { |
|
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
241 | - : $data; |
|
242 | - } |
|
232 | + /** |
|
233 | + * Normalize the provided data to an array. |
|
234 | + * @param mixed $data |
|
235 | + * @return void |
|
236 | + */ |
|
237 | + protected function normalizeData($data) |
|
238 | + { |
|
239 | + $this->data = is_object($data) |
|
240 | + ? get_object_vars($data) |
|
241 | + : $data; |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Parse a parameter list. |
|
246 | - * @param string $rule |
|
247 | - * @param string $parameter |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - protected function parseParameters($rule, $parameter) |
|
251 | - { |
|
252 | - return 'regex' == strtolower($rule) |
|
253 | - ? [$parameter] |
|
254 | - : str_getcsv($parameter); |
|
255 | - } |
|
244 | + /** |
|
245 | + * Parse a parameter list. |
|
246 | + * @param string $rule |
|
247 | + * @param string $parameter |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + protected function parseParameters($rule, $parameter) |
|
251 | + { |
|
252 | + return 'regex' == strtolower($rule) |
|
253 | + ? [$parameter] |
|
254 | + : str_getcsv($parameter); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Extract the rule name and parameters from a rule. |
|
259 | - * @param string $rule |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - protected function parseRule($rule) |
|
263 | - { |
|
264 | - $parameters = []; |
|
265 | - if (false !== strpos($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
268 | - } |
|
269 | - $rule = glsr(Helper::class)->camelCase($rule); |
|
270 | - return [$rule, $parameters]; |
|
271 | - } |
|
257 | + /** |
|
258 | + * Extract the rule name and parameters from a rule. |
|
259 | + * @param string $rule |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + protected function parseRule($rule) |
|
263 | + { |
|
264 | + $parameters = []; |
|
265 | + if (false !== strpos($rule, ':')) { |
|
266 | + list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | + $parameters = $this->parseParameters($rule, $parameter); |
|
268 | + } |
|
269 | + $rule = glsr(Helper::class)->camelCase($rule); |
|
270 | + return [$rule, $parameters]; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * Set the validation rules. |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - protected function setRules(array $rules) |
|
278 | - { |
|
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
282 | - : $rule; |
|
283 | - } |
|
284 | - $this->rules = $rules; |
|
285 | - } |
|
273 | + /** |
|
274 | + * Set the validation rules. |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + protected function setRules(array $rules) |
|
278 | + { |
|
279 | + foreach ($rules as $key => $rule) { |
|
280 | + $rules[$key] = is_string($rule) |
|
281 | + ? explode('|', $rule) |
|
282 | + : $rule; |
|
283 | + } |
|
284 | + $this->rules = $rules; |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * Check if we should stop further validations on a given attribute. |
|
289 | - * @param string $attribute |
|
290 | - * @return bool |
|
291 | - */ |
|
292 | - protected function shouldStopValidating($attribute) |
|
293 | - { |
|
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
295 | - && isset($this->failedRules[$attribute]) |
|
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | - } |
|
287 | + /** |
|
288 | + * Check if we should stop further validations on a given attribute. |
|
289 | + * @param string $attribute |
|
290 | + * @return bool |
|
291 | + */ |
|
292 | + protected function shouldStopValidating($attribute) |
|
293 | + { |
|
294 | + return $this->hasRule($attribute, $this->implicitRules) |
|
295 | + && isset($this->failedRules[$attribute]) |
|
296 | + && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Returns a translated message for the attribute. |
|
301 | - * @param string $key |
|
302 | - * @param string $attribute |
|
303 | - * @return void|string |
|
304 | - */ |
|
305 | - protected function translator($key, array $parameters) |
|
306 | - { |
|
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
310 | - } |
|
311 | - return 'error'; |
|
312 | - } |
|
299 | + /** |
|
300 | + * Returns a translated message for the attribute. |
|
301 | + * @param string $key |
|
302 | + * @param string $attribute |
|
303 | + * @return void|string |
|
304 | + */ |
|
305 | + protected function translator($key, array $parameters) |
|
306 | + { |
|
307 | + $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | + if (isset($strings[$key])) { |
|
309 | + return $this->replace($strings[$key], $parameters); |
|
310 | + } |
|
311 | + return 'error'; |
|
312 | + } |
|
313 | 313 | } |
@@ -66,14 +66,14 @@ discard block |
||
66 | 66 | * @param mixed $data |
67 | 67 | * @return array |
68 | 68 | */ |
69 | - public function validate($data, array $rules = []) |
|
69 | + public function validate( $data, array $rules = [] ) |
|
70 | 70 | { |
71 | - $this->normalizeData($data); |
|
72 | - $this->setRules($rules); |
|
73 | - foreach ($this->rules as $attribute => $rules) { |
|
74 | - foreach ($rules as $rule) { |
|
75 | - $this->validateAttribute($attribute, $rule); |
|
76 | - if ($this->shouldStopValidating($attribute)) { |
|
71 | + $this->normalizeData( $data ); |
|
72 | + $this->setRules( $rules ); |
|
73 | + foreach( $this->rules as $attribute => $rules ) { |
|
74 | + foreach( $rules as $rule ) { |
|
75 | + $this->validateAttribute( $attribute, $rule ); |
|
76 | + if( $this->shouldStopValidating( $attribute ) ) { |
|
77 | 77 | break; |
78 | 78 | } |
79 | 79 | } |
@@ -88,18 +88,18 @@ discard block |
||
88 | 88 | * @return void |
89 | 89 | * @throws BadMethodCallException |
90 | 90 | */ |
91 | - public function validateAttribute($attribute, $rule) |
|
91 | + public function validateAttribute( $attribute, $rule ) |
|
92 | 92 | { |
93 | - list($rule, $parameters) = $this->parseRule($rule); |
|
94 | - if ('' == $rule) { |
|
93 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
94 | + if( '' == $rule ) { |
|
95 | 95 | return; |
96 | 96 | } |
97 | - $value = $this->getValue($attribute); |
|
98 | - if (!method_exists($this, $method = 'validate'.$rule)) { |
|
99 | - throw new BadMethodCallException("Method [$method] does not exist."); |
|
97 | + $value = $this->getValue( $attribute ); |
|
98 | + if( !method_exists( $this, $method = 'validate'.$rule ) ) { |
|
99 | + throw new BadMethodCallException( "Method [$method] does not exist." ); |
|
100 | 100 | } |
101 | - if (!$this->$method($value, $attribute, $parameters)) { |
|
102 | - $this->addFailure($attribute, $rule, $parameters); |
|
101 | + if( !$this->$method( $value, $attribute, $parameters ) ) { |
|
102 | + $this->addFailure( $attribute, $rule, $parameters ); |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | * @param string $rule |
110 | 110 | * @return void |
111 | 111 | */ |
112 | - protected function addError($attribute, $rule, array $parameters) |
|
112 | + protected function addError( $attribute, $rule, array $parameters ) |
|
113 | 113 | { |
114 | - $message = $this->getMessage($attribute, $rule, $parameters); |
|
114 | + $message = $this->getMessage( $attribute, $rule, $parameters ); |
|
115 | 115 | $this->errors[$attribute][] = $message; |
116 | 116 | } |
117 | 117 | |
@@ -121,9 +121,9 @@ discard block |
||
121 | 121 | * @param string $rule |
122 | 122 | * @return void |
123 | 123 | */ |
124 | - protected function addFailure($attribute, $rule, array $parameters) |
|
124 | + protected function addFailure( $attribute, $rule, array $parameters ) |
|
125 | 125 | { |
126 | - $this->addError($attribute, $rule, $parameters); |
|
126 | + $this->addError( $attribute, $rule, $parameters ); |
|
127 | 127 | $this->failedRules[$attribute][$rule] = $parameters; |
128 | 128 | } |
129 | 129 | |
@@ -132,9 +132,9 @@ discard block |
||
132 | 132 | * @param string $attribute |
133 | 133 | * @return string |
134 | 134 | */ |
135 | - protected function getAttributeType($attribute) |
|
135 | + protected function getAttributeType( $attribute ) |
|
136 | 136 | { |
137 | - return !$this->hasRule($attribute, $this->numericRules) |
|
137 | + return !$this->hasRule( $attribute, $this->numericRules ) |
|
138 | 138 | ? 'length' |
139 | 139 | : ''; |
140 | 140 | } |
@@ -145,13 +145,13 @@ discard block |
||
145 | 145 | * @param string $rule |
146 | 146 | * @return string|null |
147 | 147 | */ |
148 | - protected function getMessage($attribute, $rule, array $parameters) |
|
148 | + protected function getMessage( $attribute, $rule, array $parameters ) |
|
149 | 149 | { |
150 | - if (in_array($rule, $this->sizeRules)) { |
|
151 | - return $this->getSizeMessage($attribute, $rule, $parameters); |
|
150 | + if( in_array( $rule, $this->sizeRules ) ) { |
|
151 | + return $this->getSizeMessage( $attribute, $rule, $parameters ); |
|
152 | 152 | } |
153 | - $lowerRule = glsr(Helper::class)->snakeCase($rule); |
|
154 | - return $this->translator($lowerRule, $parameters); |
|
153 | + $lowerRule = glsr( Helper::class )->snakeCase( $rule ); |
|
154 | + return $this->translator( $lowerRule, $parameters ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
@@ -160,15 +160,15 @@ discard block |
||
160 | 160 | * @param string|array $rules |
161 | 161 | * @return array|null |
162 | 162 | */ |
163 | - protected function getRule($attribute, $rules) |
|
163 | + protected function getRule( $attribute, $rules ) |
|
164 | 164 | { |
165 | - if (!array_key_exists($attribute, $this->rules)) { |
|
165 | + if( !array_key_exists( $attribute, $this->rules ) ) { |
|
166 | 166 | return; |
167 | 167 | } |
168 | - $rules = (array) $rules; |
|
169 | - foreach ($this->rules[$attribute] as $rule) { |
|
170 | - list($rule, $parameters) = $this->parseRule($rule); |
|
171 | - if (in_array($rule, $rules)) { |
|
168 | + $rules = (array)$rules; |
|
169 | + foreach( $this->rules[$attribute] as $rule ) { |
|
170 | + list($rule, $parameters) = $this->parseRule( $rule ); |
|
171 | + if( in_array( $rule, $rules ) ) { |
|
172 | 172 | return [$rule, $parameters]; |
173 | 173 | } |
174 | 174 | } |
@@ -180,17 +180,17 @@ discard block |
||
180 | 180 | * @param mixed $value |
181 | 181 | * @return mixed |
182 | 182 | */ |
183 | - protected function getSize($attribute, $value) |
|
183 | + protected function getSize( $attribute, $value ) |
|
184 | 184 | { |
185 | - $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
|
186 | - if (is_numeric($value) && $hasNumeric) { |
|
185 | + $hasNumeric = $this->hasRule( $attribute, $this->numericRules ); |
|
186 | + if( is_numeric( $value ) && $hasNumeric ) { |
|
187 | 187 | return $value; |
188 | - } elseif (is_array($value)) { |
|
189 | - return count($value); |
|
188 | + } elseif( is_array( $value ) ) { |
|
189 | + return count( $value ); |
|
190 | 190 | } |
191 | - return function_exists('mb_strlen') |
|
192 | - ? mb_strlen($value) |
|
193 | - : strlen($value); |
|
191 | + return function_exists( 'mb_strlen' ) |
|
192 | + ? mb_strlen( $value ) |
|
193 | + : strlen( $value ); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -199,11 +199,11 @@ discard block |
||
199 | 199 | * @param string $rule |
200 | 200 | * @return string|null |
201 | 201 | */ |
202 | - protected function getSizeMessage($attribute, $rule, array $parameters) |
|
202 | + protected function getSizeMessage( $attribute, $rule, array $parameters ) |
|
203 | 203 | { |
204 | - $type = $this->getAttributeType($attribute); |
|
205 | - $lowerRule = glsr(Helper::class)->snakeCase($rule.$type); |
|
206 | - return $this->translator($lowerRule, $parameters); |
|
204 | + $type = $this->getAttributeType( $attribute ); |
|
205 | + $lowerRule = glsr( Helper::class )->snakeCase( $rule.$type ); |
|
206 | + return $this->translator( $lowerRule, $parameters ); |
|
207 | 207 | } |
208 | 208 | |
209 | 209 | /** |
@@ -211,9 +211,9 @@ discard block |
||
211 | 211 | * @param string $attribute |
212 | 212 | * @return mixed |
213 | 213 | */ |
214 | - protected function getValue($attribute) |
|
214 | + protected function getValue( $attribute ) |
|
215 | 215 | { |
216 | - if (isset($this->data[$attribute])) { |
|
216 | + if( isset($this->data[$attribute]) ) { |
|
217 | 217 | return $this->data[$attribute]; |
218 | 218 | } |
219 | 219 | } |
@@ -224,9 +224,9 @@ discard block |
||
224 | 224 | * @param string|array $rules |
225 | 225 | * @return bool |
226 | 226 | */ |
227 | - protected function hasRule($attribute, $rules) |
|
227 | + protected function hasRule( $attribute, $rules ) |
|
228 | 228 | { |
229 | - return !is_null($this->getRule($attribute, $rules)); |
|
229 | + return !is_null( $this->getRule( $attribute, $rules ) ); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
@@ -234,10 +234,10 @@ discard block |
||
234 | 234 | * @param mixed $data |
235 | 235 | * @return void |
236 | 236 | */ |
237 | - protected function normalizeData($data) |
|
237 | + protected function normalizeData( $data ) |
|
238 | 238 | { |
239 | - $this->data = is_object($data) |
|
240 | - ? get_object_vars($data) |
|
239 | + $this->data = is_object( $data ) |
|
240 | + ? get_object_vars( $data ) |
|
241 | 241 | : $data; |
242 | 242 | } |
243 | 243 | |
@@ -247,11 +247,11 @@ discard block |
||
247 | 247 | * @param string $parameter |
248 | 248 | * @return array |
249 | 249 | */ |
250 | - protected function parseParameters($rule, $parameter) |
|
250 | + protected function parseParameters( $rule, $parameter ) |
|
251 | 251 | { |
252 | - return 'regex' == strtolower($rule) |
|
252 | + return 'regex' == strtolower( $rule ) |
|
253 | 253 | ? [$parameter] |
254 | - : str_getcsv($parameter); |
|
254 | + : str_getcsv( $parameter ); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
@@ -259,14 +259,14 @@ discard block |
||
259 | 259 | * @param string $rule |
260 | 260 | * @return array |
261 | 261 | */ |
262 | - protected function parseRule($rule) |
|
262 | + protected function parseRule( $rule ) |
|
263 | 263 | { |
264 | 264 | $parameters = []; |
265 | - if (false !== strpos($rule, ':')) { |
|
266 | - list($rule, $parameter) = explode(':', $rule, 2); |
|
267 | - $parameters = $this->parseParameters($rule, $parameter); |
|
265 | + if( false !== strpos( $rule, ':' ) ) { |
|
266 | + list($rule, $parameter) = explode( ':', $rule, 2 ); |
|
267 | + $parameters = $this->parseParameters( $rule, $parameter ); |
|
268 | 268 | } |
269 | - $rule = glsr(Helper::class)->camelCase($rule); |
|
269 | + $rule = glsr( Helper::class )->camelCase( $rule ); |
|
270 | 270 | return [$rule, $parameters]; |
271 | 271 | } |
272 | 272 | |
@@ -274,11 +274,11 @@ discard block |
||
274 | 274 | * Set the validation rules. |
275 | 275 | * @return void |
276 | 276 | */ |
277 | - protected function setRules(array $rules) |
|
277 | + protected function setRules( array $rules ) |
|
278 | 278 | { |
279 | - foreach ($rules as $key => $rule) { |
|
280 | - $rules[$key] = is_string($rule) |
|
281 | - ? explode('|', $rule) |
|
279 | + foreach( $rules as $key => $rule ) { |
|
280 | + $rules[$key] = is_string( $rule ) |
|
281 | + ? explode( '|', $rule ) |
|
282 | 282 | : $rule; |
283 | 283 | } |
284 | 284 | $this->rules = $rules; |
@@ -289,11 +289,11 @@ discard block |
||
289 | 289 | * @param string $attribute |
290 | 290 | * @return bool |
291 | 291 | */ |
292 | - protected function shouldStopValidating($attribute) |
|
292 | + protected function shouldStopValidating( $attribute ) |
|
293 | 293 | { |
294 | - return $this->hasRule($attribute, $this->implicitRules) |
|
294 | + return $this->hasRule( $attribute, $this->implicitRules ) |
|
295 | 295 | && isset($this->failedRules[$attribute]) |
296 | - && array_intersect(array_keys($this->failedRules[$attribute]), $this->implicitRules); |
|
296 | + && array_intersect( array_keys( $this->failedRules[$attribute] ), $this->implicitRules ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -302,11 +302,11 @@ discard block |
||
302 | 302 | * @param string $attribute |
303 | 303 | * @return void|string |
304 | 304 | */ |
305 | - protected function translator($key, array $parameters) |
|
305 | + protected function translator( $key, array $parameters ) |
|
306 | 306 | { |
307 | - $strings = glsr(ValidationStringsDefaults::class)->defaults(); |
|
308 | - if (isset($strings[$key])) { |
|
309 | - return $this->replace($strings[$key], $parameters); |
|
307 | + $strings = glsr( ValidationStringsDefaults::class )->defaults(); |
|
308 | + if( isset($strings[$key]) ) { |
|
309 | + return $this->replace( $strings[$key], $parameters ); |
|
310 | 310 | } |
311 | 311 | return 'error'; |
312 | 312 | } |
@@ -185,7 +185,8 @@ |
||
185 | 185 | $hasNumeric = $this->hasRule($attribute, $this->numericRules); |
186 | 186 | if (is_numeric($value) && $hasNumeric) { |
187 | 187 | return $value; |
188 | - } elseif (is_array($value)) { |
|
188 | + } |
|
189 | + elseif (is_array($value)) { |
|
189 | 190 | return count($value); |
190 | 191 | } |
191 | 192 | return function_exists('mb_strlen') |
@@ -9,126 +9,126 @@ |
||
9 | 9 | */ |
10 | 10 | trait ValidationRules |
11 | 11 | { |
12 | - /** |
|
13 | - * Get the size of an attribute. |
|
14 | - * @param string $attribute |
|
15 | - * @param mixed $value |
|
16 | - * @return mixed |
|
17 | - */ |
|
18 | - abstract protected function getSize($attribute, $value); |
|
12 | + /** |
|
13 | + * Get the size of an attribute. |
|
14 | + * @param string $attribute |
|
15 | + * @param mixed $value |
|
16 | + * @return mixed |
|
17 | + */ |
|
18 | + abstract protected function getSize($attribute, $value); |
|
19 | 19 | |
20 | - /** |
|
21 | - * Replace all placeholders. |
|
22 | - * @param string $message |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - protected function replace($message, array $parameters) |
|
26 | - { |
|
27 | - if (false === strpos($message, '%s')) { |
|
28 | - return $message; |
|
29 | - } |
|
30 | - return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
31 | - foreach ($parameters as $key => $value) { |
|
32 | - return array_shift($parameters); |
|
33 | - } |
|
34 | - }, $message); |
|
35 | - } |
|
20 | + /** |
|
21 | + * Replace all placeholders. |
|
22 | + * @param string $message |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + protected function replace($message, array $parameters) |
|
26 | + { |
|
27 | + if (false === strpos($message, '%s')) { |
|
28 | + return $message; |
|
29 | + } |
|
30 | + return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
31 | + foreach ($parameters as $key => $value) { |
|
32 | + return array_shift($parameters); |
|
33 | + } |
|
34 | + }, $message); |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Validate that an attribute was "accepted". |
|
39 | - * This validation rule implies the attribute is "required". |
|
40 | - * @param string $attribute |
|
41 | - * @param mixed $value |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function validateAccepted($value) |
|
45 | - { |
|
46 | - $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
47 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
48 | - } |
|
37 | + /** |
|
38 | + * Validate that an attribute was "accepted". |
|
39 | + * This validation rule implies the attribute is "required". |
|
40 | + * @param string $attribute |
|
41 | + * @param mixed $value |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function validateAccepted($value) |
|
45 | + { |
|
46 | + $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
47 | + return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Validate the size of an attribute is between a set of values. |
|
52 | - * @param string $attribute |
|
53 | - * @param mixed $value |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - public function validateBetween($value, $attribute, array $parameters) |
|
57 | - { |
|
58 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
59 | - $size = $this->getSize($attribute, $value); |
|
60 | - return $size >= $parameters[0] && $size <= $parameters[1]; |
|
61 | - } |
|
50 | + /** |
|
51 | + * Validate the size of an attribute is between a set of values. |
|
52 | + * @param string $attribute |
|
53 | + * @param mixed $value |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + public function validateBetween($value, $attribute, array $parameters) |
|
57 | + { |
|
58 | + $this->requireParameterCount(2, $parameters, 'between'); |
|
59 | + $size = $this->getSize($attribute, $value); |
|
60 | + return $size >= $parameters[0] && $size <= $parameters[1]; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Validate that an attribute is a valid e-mail address. |
|
65 | - * @param mixed $value |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function validateEmail($value) |
|
69 | - { |
|
70 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
71 | - } |
|
63 | + /** |
|
64 | + * Validate that an attribute is a valid e-mail address. |
|
65 | + * @param mixed $value |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function validateEmail($value) |
|
69 | + { |
|
70 | + return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Validate the size of an attribute is less than a maximum value. |
|
75 | - * @param string $attribute |
|
76 | - * @param mixed $value |
|
77 | - * @return bool |
|
78 | - */ |
|
79 | - public function validateMax($value, $attribute, array $parameters) |
|
80 | - { |
|
81 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
82 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
83 | - } |
|
73 | + /** |
|
74 | + * Validate the size of an attribute is less than a maximum value. |
|
75 | + * @param string $attribute |
|
76 | + * @param mixed $value |
|
77 | + * @return bool |
|
78 | + */ |
|
79 | + public function validateMax($value, $attribute, array $parameters) |
|
80 | + { |
|
81 | + $this->requireParameterCount(1, $parameters, 'max'); |
|
82 | + return $this->getSize($attribute, $value) <= $parameters[0]; |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Validate the size of an attribute is greater than a minimum value. |
|
87 | - * @param string $attribute |
|
88 | - * @param mixed $value |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function validateMin($value, $attribute, array $parameters) |
|
92 | - { |
|
93 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
94 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
95 | - } |
|
85 | + /** |
|
86 | + * Validate the size of an attribute is greater than a minimum value. |
|
87 | + * @param string $attribute |
|
88 | + * @param mixed $value |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function validateMin($value, $attribute, array $parameters) |
|
92 | + { |
|
93 | + $this->requireParameterCount(1, $parameters, 'min'); |
|
94 | + return $this->getSize($attribute, $value) >= $parameters[0]; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Validate that an attribute is numeric. |
|
99 | - * @param mixed $value |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public function validateNumber($value) |
|
103 | - { |
|
104 | - return is_numeric($value); |
|
105 | - } |
|
97 | + /** |
|
98 | + * Validate that an attribute is numeric. |
|
99 | + * @param mixed $value |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public function validateNumber($value) |
|
103 | + { |
|
104 | + return is_numeric($value); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Validate that a required attribute exists. |
|
109 | - * @param mixed $value |
|
110 | - * @return bool |
|
111 | - */ |
|
112 | - public function validateRequired($value) |
|
113 | - { |
|
114 | - return is_null($value) |
|
115 | - || (is_string($value) && '' === trim($value)) |
|
116 | - || (is_array($value) && count($value) < 1) |
|
117 | - ? false |
|
118 | - : true; |
|
119 | - } |
|
107 | + /** |
|
108 | + * Validate that a required attribute exists. |
|
109 | + * @param mixed $value |
|
110 | + * @return bool |
|
111 | + */ |
|
112 | + public function validateRequired($value) |
|
113 | + { |
|
114 | + return is_null($value) |
|
115 | + || (is_string($value) && '' === trim($value)) |
|
116 | + || (is_array($value) && count($value) < 1) |
|
117 | + ? false |
|
118 | + : true; |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Require a certain number of parameters to be present. |
|
123 | - * @param int $count |
|
124 | - * @param string $rule |
|
125 | - * @return void |
|
126 | - * @throws InvalidArgumentException |
|
127 | - */ |
|
128 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
129 | - { |
|
130 | - if (count($parameters) < $count) { |
|
131 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
132 | - } |
|
133 | - } |
|
121 | + /** |
|
122 | + * Require a certain number of parameters to be present. |
|
123 | + * @param int $count |
|
124 | + * @param string $rule |
|
125 | + * @return void |
|
126 | + * @throws InvalidArgumentException |
|
127 | + */ |
|
128 | + protected function requireParameterCount($count, array $parameters, $rule) |
|
129 | + { |
|
130 | + if (count($parameters) < $count) { |
|
131 | + throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
132 | + } |
|
133 | + } |
|
134 | 134 | } |
@@ -15,23 +15,23 @@ discard block |
||
15 | 15 | * @param mixed $value |
16 | 16 | * @return mixed |
17 | 17 | */ |
18 | - abstract protected function getSize($attribute, $value); |
|
18 | + abstract protected function getSize( $attribute, $value ); |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Replace all placeholders. |
22 | 22 | * @param string $message |
23 | 23 | * @return string |
24 | 24 | */ |
25 | - protected function replace($message, array $parameters) |
|
25 | + protected function replace( $message, array $parameters ) |
|
26 | 26 | { |
27 | - if (false === strpos($message, '%s')) { |
|
27 | + if( false === strpos( $message, '%s' ) ) { |
|
28 | 28 | return $message; |
29 | 29 | } |
30 | - return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
31 | - foreach ($parameters as $key => $value) { |
|
32 | - return array_shift($parameters); |
|
30 | + return preg_replace_callback( '/(%s)/', function() use (&$parameters) { |
|
31 | + foreach( $parameters as $key => $value ) { |
|
32 | + return array_shift( $parameters ); |
|
33 | 33 | } |
34 | - }, $message); |
|
34 | + }, $message ); |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -41,10 +41,10 @@ discard block |
||
41 | 41 | * @param mixed $value |
42 | 42 | * @return bool |
43 | 43 | */ |
44 | - public function validateAccepted($value) |
|
44 | + public function validateAccepted( $value ) |
|
45 | 45 | { |
46 | 46 | $acceptable = ['yes', 'on', '1', 1, true, 'true']; |
47 | - return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
47 | + return $this->validateRequired( $value ) && in_array( $value, $acceptable, true ); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -53,10 +53,10 @@ discard block |
||
53 | 53 | * @param mixed $value |
54 | 54 | * @return bool |
55 | 55 | */ |
56 | - public function validateBetween($value, $attribute, array $parameters) |
|
56 | + public function validateBetween( $value, $attribute, array $parameters ) |
|
57 | 57 | { |
58 | - $this->requireParameterCount(2, $parameters, 'between'); |
|
59 | - $size = $this->getSize($attribute, $value); |
|
58 | + $this->requireParameterCount( 2, $parameters, 'between' ); |
|
59 | + $size = $this->getSize( $attribute, $value ); |
|
60 | 60 | return $size >= $parameters[0] && $size <= $parameters[1]; |
61 | 61 | } |
62 | 62 | |
@@ -65,9 +65,9 @@ discard block |
||
65 | 65 | * @param mixed $value |
66 | 66 | * @return bool |
67 | 67 | */ |
68 | - public function validateEmail($value) |
|
68 | + public function validateEmail( $value ) |
|
69 | 69 | { |
70 | - return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
70 | + return false !== filter_var( $value, FILTER_VALIDATE_EMAIL ); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -76,10 +76,10 @@ discard block |
||
76 | 76 | * @param mixed $value |
77 | 77 | * @return bool |
78 | 78 | */ |
79 | - public function validateMax($value, $attribute, array $parameters) |
|
79 | + public function validateMax( $value, $attribute, array $parameters ) |
|
80 | 80 | { |
81 | - $this->requireParameterCount(1, $parameters, 'max'); |
|
82 | - return $this->getSize($attribute, $value) <= $parameters[0]; |
|
81 | + $this->requireParameterCount( 1, $parameters, 'max' ); |
|
82 | + return $this->getSize( $attribute, $value ) <= $parameters[0]; |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -88,10 +88,10 @@ discard block |
||
88 | 88 | * @param mixed $value |
89 | 89 | * @return bool |
90 | 90 | */ |
91 | - public function validateMin($value, $attribute, array $parameters) |
|
91 | + public function validateMin( $value, $attribute, array $parameters ) |
|
92 | 92 | { |
93 | - $this->requireParameterCount(1, $parameters, 'min'); |
|
94 | - return $this->getSize($attribute, $value) >= $parameters[0]; |
|
93 | + $this->requireParameterCount( 1, $parameters, 'min' ); |
|
94 | + return $this->getSize( $attribute, $value ) >= $parameters[0]; |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | * @param mixed $value |
100 | 100 | * @return bool |
101 | 101 | */ |
102 | - public function validateNumber($value) |
|
102 | + public function validateNumber( $value ) |
|
103 | 103 | { |
104 | - return is_numeric($value); |
|
104 | + return is_numeric( $value ); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
@@ -109,11 +109,11 @@ discard block |
||
109 | 109 | * @param mixed $value |
110 | 110 | * @return bool |
111 | 111 | */ |
112 | - public function validateRequired($value) |
|
112 | + public function validateRequired( $value ) |
|
113 | 113 | { |
114 | - return is_null($value) |
|
115 | - || (is_string($value) && '' === trim($value)) |
|
116 | - || (is_array($value) && count($value) < 1) |
|
114 | + return is_null( $value ) |
|
115 | + || (is_string( $value ) && '' === trim( $value )) |
|
116 | + || (is_array( $value ) && count( $value ) < 1) |
|
117 | 117 | ? false |
118 | 118 | : true; |
119 | 119 | } |
@@ -125,10 +125,10 @@ discard block |
||
125 | 125 | * @return void |
126 | 126 | * @throws InvalidArgumentException |
127 | 127 | */ |
128 | - protected function requireParameterCount($count, array $parameters, $rule) |
|
128 | + protected function requireParameterCount( $count, array $parameters, $rule ) |
|
129 | 129 | { |
130 | - if (count($parameters) < $count) { |
|
131 | - throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
130 | + if( count( $parameters ) < $count ) { |
|
131 | + throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
|
132 | 132 | } |
133 | 133 | } |
134 | 134 | } |
@@ -4,206 +4,206 @@ |
||
4 | 4 | |
5 | 5 | class Rating |
6 | 6 | { |
7 | - /** |
|
8 | - * The more sure we are of the confidence interval (the higher the confidence level), the less |
|
9 | - * precise the estimation will be as the margin for error will be higher. |
|
10 | - * @see http://homepages.math.uic.edu/~bpower6/stat101/Confidence%20Intervals.pdf |
|
11 | - * @see https://www.thecalculator.co/math/Confidence-Interval-Calculator-210.html |
|
12 | - * @see https://www.youtube.com/watch?v=grodoLzThy4 |
|
13 | - * @see https://en.wikipedia.org/wiki/Standard_score |
|
14 | - * @var array |
|
15 | - */ |
|
16 | - const CONFIDENCE_LEVEL_Z_SCORES = [ |
|
17 | - 50 => 0.67449, |
|
18 | - 70 => 1.04, |
|
19 | - 75 => 1.15035, |
|
20 | - 80 => 1.282, |
|
21 | - 85 => 1.44, |
|
22 | - 90 => 1.64485, |
|
23 | - 92 => 1.75, |
|
24 | - 95 => 1.95996, |
|
25 | - 96 => 2.05, |
|
26 | - 97 => 2.17009, |
|
27 | - 98 => 2.326, |
|
28 | - 99 => 2.57583, |
|
29 | - '99.5' => 2.81, |
|
30 | - '99.8' => 3.08, |
|
31 | - '99.9' => 3.29053, |
|
32 | - ]; |
|
7 | + /** |
|
8 | + * The more sure we are of the confidence interval (the higher the confidence level), the less |
|
9 | + * precise the estimation will be as the margin for error will be higher. |
|
10 | + * @see http://homepages.math.uic.edu/~bpower6/stat101/Confidence%20Intervals.pdf |
|
11 | + * @see https://www.thecalculator.co/math/Confidence-Interval-Calculator-210.html |
|
12 | + * @see https://www.youtube.com/watch?v=grodoLzThy4 |
|
13 | + * @see https://en.wikipedia.org/wiki/Standard_score |
|
14 | + * @var array |
|
15 | + */ |
|
16 | + const CONFIDENCE_LEVEL_Z_SCORES = [ |
|
17 | + 50 => 0.67449, |
|
18 | + 70 => 1.04, |
|
19 | + 75 => 1.15035, |
|
20 | + 80 => 1.282, |
|
21 | + 85 => 1.44, |
|
22 | + 90 => 1.64485, |
|
23 | + 92 => 1.75, |
|
24 | + 95 => 1.95996, |
|
25 | + 96 => 2.05, |
|
26 | + 97 => 2.17009, |
|
27 | + 98 => 2.326, |
|
28 | + 99 => 2.57583, |
|
29 | + '99.5' => 2.81, |
|
30 | + '99.8' => 3.08, |
|
31 | + '99.9' => 3.29053, |
|
32 | + ]; |
|
33 | 33 | |
34 | - /** |
|
35 | - * @var int |
|
36 | - */ |
|
37 | - const MAX_RATING = 5; |
|
34 | + /** |
|
35 | + * @var int |
|
36 | + */ |
|
37 | + const MAX_RATING = 5; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @var int |
|
41 | - */ |
|
42 | - const MIN_RATING = 1; |
|
39 | + /** |
|
40 | + * @var int |
|
41 | + */ |
|
42 | + const MIN_RATING = 1; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param int $roundBy |
|
46 | - * @return float |
|
47 | - */ |
|
48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
49 | - { |
|
50 | - $average = array_sum($ratingCounts); |
|
51 | - if ($average > 0) { |
|
52 | - $average = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
53 | - } |
|
54 | - return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
55 | - } |
|
44 | + /** |
|
45 | + * @param int $roundBy |
|
46 | + * @return float |
|
47 | + */ |
|
48 | + public function getAverage(array $ratingCounts, $roundBy = 1) |
|
49 | + { |
|
50 | + $average = array_sum($ratingCounts); |
|
51 | + if ($average > 0) { |
|
52 | + $average = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
53 | + } |
|
54 | + return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Get the lower bound for up/down ratings |
|
59 | - * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
60 | - * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
61 | - * @see https://news.ycombinator.com/item?id=10481507 |
|
62 | - * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
63 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
64 | - * @param int $confidencePercentage |
|
65 | - * @return int|float |
|
66 | - */ |
|
67 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
68 | - { |
|
69 | - $numRatings = array_sum($upDownCounts); |
|
70 | - if ($numRatings < 1) { |
|
71 | - return 0; |
|
72 | - } |
|
73 | - $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
74 | - $phat = 1 * $upDownCounts[1] / $numRatings; |
|
75 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
76 | - } |
|
57 | + /** |
|
58 | + * Get the lower bound for up/down ratings |
|
59 | + * Method receives an up/down ratings array: [1, -1, -1, 1, 1, -1]. |
|
60 | + * @see http://www.evanmiller.org/how-not-to-sort-by-average-rating.html |
|
61 | + * @see https://news.ycombinator.com/item?id=10481507 |
|
62 | + * @see https://dataorigami.net/blogs/napkin-folding/79030467-an-algorithm-to-sort-top-comments |
|
63 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
64 | + * @param int $confidencePercentage |
|
65 | + * @return int|float |
|
66 | + */ |
|
67 | + public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
68 | + { |
|
69 | + $numRatings = array_sum($upDownCounts); |
|
70 | + if ($numRatings < 1) { |
|
71 | + return 0; |
|
72 | + } |
|
73 | + $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
74 | + $phat = 1 * $upDownCounts[1] / $numRatings; |
|
75 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @return int|float |
|
80 | - */ |
|
81 | - public function getOverallPercentage(array $ratingCounts) |
|
82 | - { |
|
83 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING'), 2); |
|
84 | - } |
|
78 | + /** |
|
79 | + * @return int|float |
|
80 | + */ |
|
81 | + public function getOverallPercentage(array $ratingCounts) |
|
82 | + { |
|
83 | + return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING'), 2); |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @return array |
|
88 | - */ |
|
89 | - public function getPercentages(array $ratingCounts) |
|
90 | - { |
|
91 | - $total = array_sum($ratingCounts); |
|
92 | - foreach ($ratingCounts as $index => $count) { |
|
93 | - if (empty($count)) { |
|
94 | - continue; |
|
95 | - } |
|
96 | - $ratingCounts[$index] = $count / $total * 100; |
|
97 | - } |
|
98 | - return $this->getRoundedPercentages($ratingCounts); |
|
99 | - } |
|
86 | + /** |
|
87 | + * @return array |
|
88 | + */ |
|
89 | + public function getPercentages(array $ratingCounts) |
|
90 | + { |
|
91 | + $total = array_sum($ratingCounts); |
|
92 | + foreach ($ratingCounts as $index => $count) { |
|
93 | + if (empty($count)) { |
|
94 | + continue; |
|
95 | + } |
|
96 | + $ratingCounts[$index] = $count / $total * 100; |
|
97 | + } |
|
98 | + return $this->getRoundedPercentages($ratingCounts); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return float |
|
103 | - */ |
|
104 | - public function getRanking(array $ratingCounts) |
|
105 | - { |
|
106 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | - $this->getRankingUsingImdb($ratingCounts), |
|
108 | - $ratingCounts, |
|
109 | - $this |
|
110 | - )); |
|
111 | - } |
|
101 | + /** |
|
102 | + * @return float |
|
103 | + */ |
|
104 | + public function getRanking(array $ratingCounts) |
|
105 | + { |
|
106 | + return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | + $this->getRankingUsingImdb($ratingCounts), |
|
108 | + $ratingCounts, |
|
109 | + $this |
|
110 | + )); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get the bayesian ranking for an array of reviews |
|
115 | - * This formula is the same one used by IMDB to rank their top 250 films. |
|
116 | - * @see https://www.xkcd.com/937/ |
|
117 | - * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
118 | - * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
119 | - * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
120 | - * @param int $confidencePercentage |
|
121 | - * @return int|float |
|
122 | - */ |
|
123 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
124 | - { |
|
125 | - $avgRating = $this->getAverage($ratingCounts); |
|
126 | - // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
|
127 | - // This could also be the average score of all items instead of a fixed value. |
|
128 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING'); // prior, 70% = 3.5 |
|
129 | - // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
130 | - $bayesMinimal = 10; // confidence |
|
131 | - $numOfReviews = array_sum($ratingCounts); |
|
132 | - return $avgRating > 0 |
|
133 | - ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
134 | - : 0; |
|
135 | - } |
|
113 | + /** |
|
114 | + * Get the bayesian ranking for an array of reviews |
|
115 | + * This formula is the same one used by IMDB to rank their top 250 films. |
|
116 | + * @see https://www.xkcd.com/937/ |
|
117 | + * @see https://districtdatalabs.silvrback.com/computing-a-bayesian-estimate-of-star-rating-means |
|
118 | + * @see http://fulmicoton.com/posts/bayesian_rating/ |
|
119 | + * @see https://stats.stackexchange.com/questions/93974/is-there-an-equivalent-to-lower-bound-of-wilson-score-confidence-interval-for-va |
|
120 | + * @param int $confidencePercentage |
|
121 | + * @return int|float |
|
122 | + */ |
|
123 | + public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
124 | + { |
|
125 | + $avgRating = $this->getAverage($ratingCounts); |
|
126 | + // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
|
127 | + // This could also be the average score of all items instead of a fixed value. |
|
128 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING'); // prior, 70% = 3.5 |
|
129 | + // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
|
130 | + $bayesMinimal = 10; // confidence |
|
131 | + $numOfReviews = array_sum($ratingCounts); |
|
132 | + return $avgRating > 0 |
|
133 | + ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
|
134 | + : 0; |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
139 | - * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
140 | - * of reviews and their rating. |
|
141 | - * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
142 | - * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
143 | - * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
144 | - * @param int $confidencePercentage |
|
145 | - * @return float |
|
146 | - */ |
|
147 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
148 | - { |
|
149 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING'); |
|
150 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
152 | - $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
153 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
154 | - } |
|
137 | + /** |
|
138 | + * The quality of a 5 star rating depends not only on the average number of stars but also on |
|
139 | + * the number of reviews. This method calculates the bayesian ranking of a page by its number |
|
140 | + * of reviews and their rating. |
|
141 | + * @see http://www.evanmiller.org/ranking-items-with-star-ratings.html |
|
142 | + * @see https://stackoverflow.com/questions/1411199/what-is-a-better-way-to-sort-by-a-5-star-rating/1411268 |
|
143 | + * @see http://julesjacobs.github.io/2015/08/17/bayesian-scoring-of-ratings.html |
|
144 | + * @param int $confidencePercentage |
|
145 | + * @return float |
|
146 | + */ |
|
147 | + public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
148 | + { |
|
149 | + $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING'); |
|
150 | + $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | + $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
152 | + $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
|
153 | + return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * @param int $target |
|
158 | - * @return array |
|
159 | - */ |
|
160 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
161 | - { |
|
162 | - array_walk($percentages, function (&$percent, $index) { |
|
163 | - $percent = [ |
|
164 | - 'index' => $index, |
|
165 | - 'percent' => floor($percent), |
|
166 | - 'remainder' => fmod($percent, 1), |
|
167 | - ]; |
|
168 | - }); |
|
169 | - $indexes = glsr_array_column($percentages, 'index'); |
|
170 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
171 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
172 | - $i = 0; |
|
173 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
175 | - ++$percentages[$i]['percent']; |
|
176 | - ++$i; |
|
177 | - } |
|
178 | - } |
|
179 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
181 | - } |
|
156 | + /** |
|
157 | + * @param int $target |
|
158 | + * @return array |
|
159 | + */ |
|
160 | + protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
161 | + { |
|
162 | + array_walk($percentages, function (&$percent, $index) { |
|
163 | + $percent = [ |
|
164 | + 'index' => $index, |
|
165 | + 'percent' => floor($percent), |
|
166 | + 'remainder' => fmod($percent, 1), |
|
167 | + ]; |
|
168 | + }); |
|
169 | + $indexes = glsr_array_column($percentages, 'index'); |
|
170 | + $remainders = glsr_array_column($percentages, 'remainder'); |
|
171 | + array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
172 | + $i = 0; |
|
173 | + if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | + while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
175 | + ++$percentages[$i]['percent']; |
|
176 | + ++$i; |
|
177 | + } |
|
178 | + } |
|
179 | + array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | + return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
181 | + } |
|
182 | 182 | |
183 | - /** |
|
184 | - * @return int |
|
185 | - */ |
|
186 | - protected function getTotalSum(array $ratingCounts) |
|
187 | - { |
|
188 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
189 | - return $carry + ($index * $ratingCounts[$index]); |
|
190 | - }); |
|
191 | - } |
|
183 | + /** |
|
184 | + * @return int |
|
185 | + */ |
|
186 | + protected function getTotalSum(array $ratingCounts) |
|
187 | + { |
|
188 | + return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
189 | + return $carry + ($index * $ratingCounts[$index]); |
|
190 | + }); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * @param int|float $ratingCountsSum |
|
195 | - * @param bool $powerOf2 |
|
196 | - * @return float |
|
197 | - */ |
|
198 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
199 | - { |
|
200 | - return array_reduce(array_keys($ratingCounts), |
|
201 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | - $ratingLevel = $powerOf2 |
|
203 | - ? pow($rating, 2) |
|
204 | - : $rating; |
|
205 | - return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
206 | - } |
|
207 | - ); |
|
208 | - } |
|
193 | + /** |
|
194 | + * @param int|float $ratingCountsSum |
|
195 | + * @param bool $powerOf2 |
|
196 | + * @return float |
|
197 | + */ |
|
198 | + protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
199 | + { |
|
200 | + return array_reduce(array_keys($ratingCounts), |
|
201 | + function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | + $ratingLevel = $powerOf2 |
|
203 | + ? pow($rating, 2) |
|
204 | + : $rating; |
|
205 | + return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
|
206 | + } |
|
207 | + ); |
|
208 | + } |
|
209 | 209 | } |
@@ -45,13 +45,13 @@ discard block |
||
45 | 45 | * @param int $roundBy |
46 | 46 | * @return float |
47 | 47 | */ |
48 | - public function getAverage(array $ratingCounts, $roundBy = 1) |
|
48 | + public function getAverage( array $ratingCounts, $roundBy = 1 ) |
|
49 | 49 | { |
50 | - $average = array_sum($ratingCounts); |
|
51 | - if ($average > 0) { |
|
52 | - $average = round($this->getTotalSum($ratingCounts) / $average, intval($roundBy)); |
|
50 | + $average = array_sum( $ratingCounts ); |
|
51 | + if( $average > 0 ) { |
|
52 | + $average = round( $this->getTotalSum( $ratingCounts ) / $average, intval( $roundBy ) ); |
|
53 | 53 | } |
54 | - return floatval(apply_filters('site-reviews/rating/average', $average, $ratingCounts)); |
|
54 | + return floatval( apply_filters( 'site-reviews/rating/average', $average, $ratingCounts ) ); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -64,50 +64,50 @@ discard block |
||
64 | 64 | * @param int $confidencePercentage |
65 | 65 | * @return int|float |
66 | 66 | */ |
67 | - public function getLowerBound(array $upDownCounts = [0, 0], $confidencePercentage = 95) |
|
67 | + public function getLowerBound( array $upDownCounts = [0, 0], $confidencePercentage = 95 ) |
|
68 | 68 | { |
69 | - $numRatings = array_sum($upDownCounts); |
|
70 | - if ($numRatings < 1) { |
|
69 | + $numRatings = array_sum( $upDownCounts ); |
|
70 | + if( $numRatings < 1 ) { |
|
71 | 71 | return 0; |
72 | 72 | } |
73 | 73 | $z = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
74 | 74 | $phat = 1 * $upDownCounts[1] / $numRatings; |
75 | - return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt(($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings)) / (1 + $z * $z / $numRatings); |
|
75 | + return ($phat + $z * $z / (2 * $numRatings) - $z * sqrt( ($phat * (1 - $phat) + $z * $z / (4 * $numRatings)) / $numRatings )) / (1 + $z * $z / $numRatings); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @return int|float |
80 | 80 | */ |
81 | - public function getOverallPercentage(array $ratingCounts) |
|
81 | + public function getOverallPercentage( array $ratingCounts ) |
|
82 | 82 | { |
83 | - return round($this->getAverage($ratingCounts) * 100 / glsr()->constant('MAX_RATING'), 2); |
|
83 | + return round( $this->getAverage( $ratingCounts ) * 100 / glsr()->constant( 'MAX_RATING' ), 2 ); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | /** |
87 | 87 | * @return array |
88 | 88 | */ |
89 | - public function getPercentages(array $ratingCounts) |
|
89 | + public function getPercentages( array $ratingCounts ) |
|
90 | 90 | { |
91 | - $total = array_sum($ratingCounts); |
|
92 | - foreach ($ratingCounts as $index => $count) { |
|
93 | - if (empty($count)) { |
|
91 | + $total = array_sum( $ratingCounts ); |
|
92 | + foreach( $ratingCounts as $index => $count ) { |
|
93 | + if( empty($count) ) { |
|
94 | 94 | continue; |
95 | 95 | } |
96 | 96 | $ratingCounts[$index] = $count / $total * 100; |
97 | 97 | } |
98 | - return $this->getRoundedPercentages($ratingCounts); |
|
98 | + return $this->getRoundedPercentages( $ratingCounts ); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
102 | 102 | * @return float |
103 | 103 | */ |
104 | - public function getRanking(array $ratingCounts) |
|
104 | + public function getRanking( array $ratingCounts ) |
|
105 | 105 | { |
106 | - return floatval(apply_filters('site-reviews/rating/ranking', |
|
107 | - $this->getRankingUsingImdb($ratingCounts), |
|
106 | + return floatval( apply_filters( 'site-reviews/rating/ranking', |
|
107 | + $this->getRankingUsingImdb( $ratingCounts ), |
|
108 | 108 | $ratingCounts, |
109 | 109 | $this |
110 | - )); |
|
110 | + ) ); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -120,15 +120,15 @@ discard block |
||
120 | 120 | * @param int $confidencePercentage |
121 | 121 | * @return int|float |
122 | 122 | */ |
123 | - public function getRankingUsingImdb(array $ratingCounts, $confidencePercentage = 70) |
|
123 | + public function getRankingUsingImdb( array $ratingCounts, $confidencePercentage = 70 ) |
|
124 | 124 | { |
125 | - $avgRating = $this->getAverage($ratingCounts); |
|
125 | + $avgRating = $this->getAverage( $ratingCounts ); |
|
126 | 126 | // Represents a prior (your prior opinion without data) for the average star rating. A higher prior also means a higher margin for error. |
127 | 127 | // This could also be the average score of all items instead of a fixed value. |
128 | - $bayesMean = ($confidencePercentage / 100) * glsr()->constant('MAX_RATING'); // prior, 70% = 3.5 |
|
128 | + $bayesMean = ($confidencePercentage / 100) * glsr()->constant( 'MAX_RATING' ); // prior, 70% = 3.5 |
|
129 | 129 | // Represents the number of ratings expected to begin observing a pattern that would put confidence in the prior. |
130 | 130 | $bayesMinimal = 10; // confidence |
131 | - $numOfReviews = array_sum($ratingCounts); |
|
131 | + $numOfReviews = array_sum( $ratingCounts ); |
|
132 | 132 | return $avgRating > 0 |
133 | 133 | ? (($bayesMinimal * $bayesMean) + ($avgRating * $numOfReviews)) / ($bayesMinimal + $numOfReviews) |
134 | 134 | : 0; |
@@ -144,48 +144,48 @@ discard block |
||
144 | 144 | * @param int $confidencePercentage |
145 | 145 | * @return float |
146 | 146 | */ |
147 | - public function getRankingUsingZScores(array $ratingCounts, $confidencePercentage = 90) |
|
147 | + public function getRankingUsingZScores( array $ratingCounts, $confidencePercentage = 90 ) |
|
148 | 148 | { |
149 | - $ratingCountsSum = array_sum($ratingCounts) + glsr()->constant('MAX_RATING'); |
|
150 | - $weight = $this->getWeight($ratingCounts, $ratingCountsSum); |
|
151 | - $weightPow2 = $this->getWeight($ratingCounts, $ratingCountsSum, true); |
|
149 | + $ratingCountsSum = array_sum( $ratingCounts ) + glsr()->constant( 'MAX_RATING' ); |
|
150 | + $weight = $this->getWeight( $ratingCounts, $ratingCountsSum ); |
|
151 | + $weightPow2 = $this->getWeight( $ratingCounts, $ratingCountsSum, true ); |
|
152 | 152 | $zScore = static::CONFIDENCE_LEVEL_Z_SCORES[$confidencePercentage]; |
153 | - return $weight - $zScore * sqrt(($weightPow2 - pow($weight, 2)) / ($ratingCountsSum + 1)); |
|
153 | + return $weight - $zScore * sqrt( ($weightPow2 - pow( $weight, 2 )) / ($ratingCountsSum + 1) ); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | 157 | * @param int $target |
158 | 158 | * @return array |
159 | 159 | */ |
160 | - protected function getRoundedPercentages(array $percentages, $totalPercent = 100) |
|
160 | + protected function getRoundedPercentages( array $percentages, $totalPercent = 100 ) |
|
161 | 161 | { |
162 | - array_walk($percentages, function (&$percent, $index) { |
|
162 | + array_walk( $percentages, function( &$percent, $index ) { |
|
163 | 163 | $percent = [ |
164 | 164 | 'index' => $index, |
165 | - 'percent' => floor($percent), |
|
166 | - 'remainder' => fmod($percent, 1), |
|
165 | + 'percent' => floor( $percent ), |
|
166 | + 'remainder' => fmod( $percent, 1 ), |
|
167 | 167 | ]; |
168 | 168 | }); |
169 | - $indexes = glsr_array_column($percentages, 'index'); |
|
170 | - $remainders = glsr_array_column($percentages, 'remainder'); |
|
171 | - array_multisort($remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages); |
|
169 | + $indexes = glsr_array_column( $percentages, 'index' ); |
|
170 | + $remainders = glsr_array_column( $percentages, 'remainder' ); |
|
171 | + array_multisort( $remainders, SORT_DESC, SORT_STRING, $indexes, SORT_DESC, $percentages ); |
|
172 | 172 | $i = 0; |
173 | - if (array_sum(glsr_array_column($percentages, 'percent')) > 0) { |
|
174 | - while (array_sum(glsr_array_column($percentages, 'percent')) < $totalPercent) { |
|
173 | + if( array_sum( glsr_array_column( $percentages, 'percent' ) ) > 0 ) { |
|
174 | + while( array_sum( glsr_array_column( $percentages, 'percent' ) ) < $totalPercent ) { |
|
175 | 175 | ++$percentages[$i]['percent']; |
176 | 176 | ++$i; |
177 | 177 | } |
178 | 178 | } |
179 | - array_multisort($indexes, SORT_DESC, $percentages); |
|
180 | - return array_combine($indexes, glsr_array_column($percentages, 'percent')); |
|
179 | + array_multisort( $indexes, SORT_DESC, $percentages ); |
|
180 | + return array_combine( $indexes, glsr_array_column( $percentages, 'percent' ) ); |
|
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
184 | 184 | * @return int |
185 | 185 | */ |
186 | - protected function getTotalSum(array $ratingCounts) |
|
186 | + protected function getTotalSum( array $ratingCounts ) |
|
187 | 187 | { |
188 | - return array_reduce(array_keys($ratingCounts), function ($carry, $index) use ($ratingCounts) { |
|
188 | + return array_reduce( array_keys( $ratingCounts ), function( $carry, $index ) use ($ratingCounts) { |
|
189 | 189 | return $carry + ($index * $ratingCounts[$index]); |
190 | 190 | }); |
191 | 191 | } |
@@ -195,12 +195,12 @@ discard block |
||
195 | 195 | * @param bool $powerOf2 |
196 | 196 | * @return float |
197 | 197 | */ |
198 | - protected function getWeight(array $ratingCounts, $ratingCountsSum, $powerOf2 = false) |
|
198 | + protected function getWeight( array $ratingCounts, $ratingCountsSum, $powerOf2 = false ) |
|
199 | 199 | { |
200 | - return array_reduce(array_keys($ratingCounts), |
|
201 | - function ($count, $rating) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
200 | + return array_reduce( array_keys( $ratingCounts ), |
|
201 | + function( $count, $rating ) use ($ratingCounts, $ratingCountsSum, $powerOf2) { |
|
202 | 202 | $ratingLevel = $powerOf2 |
203 | - ? pow($rating, 2) |
|
203 | + ? pow( $rating, 2 ) |
|
204 | 204 | : $rating; |
205 | 205 | return $count + ($ratingLevel * ($ratingCounts[$rating] + 1)) / $ratingCountsSum; |
206 | 206 | } |
@@ -4,9 +4,9 @@ |
||
4 | 4 | |
5 | 5 | interface ShortcodeContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @params string|array $atts |
|
9 | - * @return string |
|
10 | - */ |
|
11 | - public function buildShortcode($atts = []); |
|
7 | + /** |
|
8 | + * @params string|array $atts |
|
9 | + * @return string |
|
10 | + */ |
|
11 | + public function buildShortcode($atts = []); |
|
12 | 12 | } |
@@ -8,5 +8,5 @@ |
||
8 | 8 | * @params string|array $atts |
9 | 9 | * @return string |
10 | 10 | */ |
11 | - public function buildShortcode($atts = []); |
|
11 | + public function buildShortcode( $atts = [] ); |
|
12 | 12 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | interface HooksContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @return void |
|
9 | - */ |
|
10 | - public function run(); |
|
7 | + /** |
|
8 | + * @return void |
|
9 | + */ |
|
10 | + public function run(); |
|
11 | 11 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | interface PartialContract |
6 | 6 | { |
7 | - /** |
|
8 | - * @return void|string |
|
9 | - */ |
|
10 | - public function build(array $args = []); |
|
7 | + /** |
|
8 | + * @return void|string |
|
9 | + */ |
|
10 | + public function build(array $args = []); |
|
11 | 11 | } |
@@ -7,5 +7,5 @@ |
||
7 | 7 | /** |
8 | 8 | * @return void|string |
9 | 9 | */ |
10 | - public function build(array $args = []); |
|
10 | + public function build( array $args = [] ); |
|
11 | 11 | } |