33namespace Selective \ArrayReader ;
44
55use Cake \Chronos \Chronos ;
6- use Exception ;
76use InvalidArgumentException ;
87
98/**
@@ -32,13 +31,15 @@ public function __construct(array $data = [])
3231 * @param string $key The key
3332 * @param int|null $default The default value
3433 *
34+ * @throws InvalidArgumentException
35+ *
3536 * @return int The value
3637 */
3738 public function getInt (string $ key , int $ default = null ): int
3839 {
3940 $ value = $ this ->find ($ key , $ default );
4041
41- if ($ value === null ) {
42+ if ($ this -> isNullOrBlank ( $ value) ) {
4243 throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
4344 }
4445
@@ -49,19 +50,19 @@ public function getInt(string $key, int $default = null): int
4950 * Get value as integer or null.
5051 *
5152 * @param string $key The key
52- * @param int $default The default value
53+ * @param int|null $default The default value
5354 *
5455 * @return int|null The value
5556 */
5657 public function findInt (string $ key , int $ default = null )
5758 {
58- $ result = $ this ->find ($ key , $ default );
59+ $ value = $ this ->find ($ key , $ default );
5960
60- if ($ result === null ) {
61+ if ($ this -> isNullOrBlank ( $ value ) ) {
6162 return null ;
6263 }
6364
64- return (int )$ result ;
65+ return (int )$ value ;
6566 }
6667
6768 /**
@@ -70,6 +71,8 @@ public function findInt(string $key, int $default = null)
7071 * @param string $key The key
7172 * @param string|null $default The default value
7273 *
74+ * @throws InvalidArgumentException
75+ *
7376 * @return string The value
7477 */
7578 public function getString (string $ key , string $ default = null ): string
@@ -108,13 +111,15 @@ public function findString(string $key, string $default = null)
108111 * @param string $key The key
109112 * @param array|null $default The default value
110113 *
114+ * @throws InvalidArgumentException
115+ *
111116 * @return array The value
112117 */
113118 public function getArray (string $ key , array $ default = null ): array
114119 {
115120 $ value = $ this ->find ($ key , $ default );
116121
117- if ($ value === null ) {
122+ if ($ this -> isNullOrBlank ( $ value) ) {
118123 throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
119124 }
120125
@@ -133,7 +138,7 @@ public function findArray(string $key, array $default = null)
133138 {
134139 $ value = $ this ->find ($ key , $ default );
135140
136- if ($ value === null ) {
141+ if ($ this -> isNullOrBlank ( $ value) ) {
137142 return null ;
138143 }
139144
@@ -146,13 +151,15 @@ public function findArray(string $key, array $default = null)
146151 * @param string $key The key
147152 * @param float|null $default The default value
148153 *
154+ * @throws InvalidArgumentException
155+ *
149156 * @return float The value
150157 */
151158 public function getFloat (string $ key , float $ default = null ): float
152159 {
153160 $ value = $ this ->find ($ key , $ default );
154161
155- if ($ value === null ) {
162+ if ($ this -> isNullOrBlank ( $ value) ) {
156163 throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
157164 }
158165
@@ -171,7 +178,7 @@ public function findFloat(string $key, float $default = null)
171178 {
172179 $ value = $ this ->find ($ key , $ default );
173180
174- if ($ value === null ) {
181+ if ($ this -> isNullOrBlank ( $ value) ) {
175182 return null ;
176183 }
177184
@@ -184,13 +191,15 @@ public function findFloat(string $key, float $default = null)
184191 * @param string $key The key
185192 * @param bool|null $default The default value
186193 *
194+ * @throws InvalidArgumentException
195+ *
187196 * @return bool The value
188197 */
189198 public function getBool (string $ key , bool $ default = null ): bool
190199 {
191200 $ value = $ this ->find ($ key , $ default );
192201
193- if ($ value === null ) {
202+ if ($ this -> isNullOrBlank ( $ value) ) {
194203 throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
195204 }
196205
@@ -209,7 +218,7 @@ public function findBool(string $key, bool $default = null)
209218 {
210219 $ value = $ this ->find ($ key , $ default );
211220
212- if ($ value === null ) {
221+ if ($ this -> isNullOrBlank ( $ value) ) {
213222 return null ;
214223 }
215224
@@ -222,13 +231,15 @@ public function findBool(string $key, bool $default = null)
222231 * @param string $key The key
223232 * @param Chronos|null $default The default value
224233 *
234+ * @throws InvalidArgumentException
235+ *
225236 * @return Chronos The value
226237 */
227238 public function getChronos (string $ key , Chronos $ default = null ): Chronos
228239 {
229240 $ value = $ this ->find ($ key , $ default );
230241
231- if ($ value === null ) {
242+ if ($ this -> isNullOrBlank ( $ value) ) {
232243 throw new InvalidArgumentException (sprintf ('No value found for key "%s" ' , $ key ));
233244 }
234245
@@ -245,15 +256,17 @@ public function getChronos(string $key, Chronos $default = null): Chronos
245256 * @param string $key The key
246257 * @param Chronos $default The default value
247258 *
248- * @throws Exception Chronos date time parsing error
249- *
250259 * @return Chronos|null The value
251260 */
252261 public function findChronos (string $ key , Chronos $ default = null )
253262 {
254263 $ value = $ this ->find ($ key , $ default );
255264
256- if ($ value === null || $ value instanceof Chronos) {
265+ if ($ this ->isNullOrBlank ($ value )) {
266+ return null ;
267+ }
268+
269+ if ($ value instanceof Chronos) {
257270 return $ value ;
258271 }
259272
@@ -332,4 +345,16 @@ public function isEmpty(string $path): bool
332345 {
333346 return empty ($ this ->find ($ path ));
334347 }
348+
349+ /**
350+ * Is null or blank.
351+ *
352+ * @param mixed $value The value
353+ *
354+ * @return bool The status
355+ */
356+ private function isNullOrBlank ($ value ): bool
357+ {
358+ return $ value === null || $ value === '' ;
359+ }
335360}
0 commit comments