22
33namespace Awssat \ArrayHelper ;
44
5-
65/**
7- * @method \Awssat\ArrayHelper\ArrayHelper push(mixed $value1 [, mixed $... ])
6+ * @method \Awssat\ArrayHelper\ArrayHelper push(mixed $value1 [, mixed $... ])
87 * @method \Awssat\ArrayHelper\ArrayHelper prepend(mixed $value1 [, mixed $... ])
98 * @method \Awssat\ArrayHelper\ArrayHelper values()
109 * @method \Awssat\ArrayHelper\ArrayHelper keys([ mixed $search_value [, bool $strict]])
@@ -86,32 +85,31 @@ class ArrayHelper implements \Countable, \ArrayAccess, \IteratorAggregate, \Seri
8685 protected $ isElse = [];
8786 protected $ insideIfCondition = false ;
8887 protected $ dataHasChanged = false ;
89-
88+
9089 protected $ aliasMethods = [
91- 'exists ' => 'in_array ' ,
90+ 'exists ' => 'in_array ' ,
9291 'prepend ' => 'array_unshift ' ,
9392 ];
9493
95-
9694 /**
9795 * Initiate the class.
9896 *
9997 * @param array $value an array
10098 */
10199 public function __construct ()
102100 {
103- if (func_num_args () > 1 ) {
101+ if (func_num_args () > 1 ) {
104102 $ this ->data = func_get_args ();
105- } else if (func_num_args () == 1 ) {
103+ } elseif (func_num_args () == 1 ) {
106104 $ this ->set (func_get_arg (0 ));
107105 }
108106
109107 return $ this ;
110108 }
111109
112- static public function make ()
110+ public static function make ()
113111 {
114- return ( new static (...func_get_args () ));
112+ return new static (...func_get_args ());
115113 }
116114
117115 /**
@@ -142,28 +140,27 @@ function () use ($methodName, $arguments) {
142140
143141 $ snake_method_name = strtolower (preg_replace ('/([a-z]{1})([A-Z]{1})/ ' , '$1_$2 ' , $ methodName ));
144142
145- if (array_key_exists ($ snake_method_name , $ this ->aliasMethods )) {
143+ if (array_key_exists ($ snake_method_name , $ this ->aliasMethods )) {
146144 $ snake_method_name = $ this ->aliasMethods [$ snake_method_name ];
147145 }
148146
149- if (in_array ($ snake_method_name , ['combine_keys ' , 'combine_values ' , 'equal ' , 'contains ' , 'empty ' ])) {
150- return $ this ->do (function () use ($ snake_method_name , $ arguments ) {
151-
152- if ($ snake_method_name == 'combine_keys ' ) {
147+ if (in_array ($ snake_method_name , ['combine_keys ' , 'combine_values ' , 'equal ' , 'contains ' , 'empty ' ])) {
148+ return $ this ->do (function () use ($ snake_method_name , $ arguments ) {
149+ if ($ snake_method_name == 'combine_keys ' ) {
153150 return array_combine ($ arguments [0 ], $ this ->data );
154151 }
155152
156153 if ($ snake_method_name == 'combine_values ' ) {
157154 return array_combine ($ this ->data , $ arguments [0 ]);
158155 }
159-
156+
160157 if ($ snake_method_name == 'equal ' ) {
161158 return $ this ->equal ($ this ->data , $ arguments [0 ]);
162159 }
163160
164- if ($ snake_method_name == 'contains ' && sizeof ($ arguments )) {
165- foreach ($ arguments as $ argument ) {
166- if (! in_array ($ argument , $ this ->data )) {
161+ if ($ snake_method_name == 'contains ' && count ($ arguments )) {
162+ foreach ($ arguments as $ argument ) {
163+ if (! in_array ($ argument , $ this ->data )) {
167164 return false ;
168165 }
169166 }
@@ -174,14 +171,12 @@ function () use ($methodName, $arguments) {
174171 if ($ snake_method_name == 'empty ' ) {
175172 return count ($ this ->data ) === 0 ;
176173 }
177-
178- });
179-
180- } else if (function_exists ('array_ ' .$ snake_method_name )) {
174+ });
175+ } elseif (function_exists ('array_ ' .$ snake_method_name )) {
181176 return $ this ->do ('array_ ' .$ snake_method_name , ...$ arguments );
182- } else if (function_exists ($ snake_method_name )) {
183- return $ this ->do ($ snake_method_name , ...$ arguments );
184- } else {
177+ } elseif (function_exists ($ snake_method_name )) {
178+ return $ this ->do ($ snake_method_name , ...$ arguments );
179+ } else {
185180 // Couldn't find either?
186181 throw new \BadMethodCallException ('Method ( ' .$ methodName .') does not exists! ' );
187182 }
@@ -190,28 +185,29 @@ function () use ($methodName, $arguments) {
190185 }
191186
192187 /**
193- * Undocumented function
188+ * Undocumented function.
194189 *
195190 * @param int|null $index
196- * @param boolean $ignoreConditions
191+ * @param bool $ignoreConditions
192+ *
197193 * @return array|self
198194 */
199195 public function get ($ index = null , $ ignoreConditions = false )
200196 {
201- if (! is_null ($ index )) {
197+ if (! is_null ($ index )) {
202198 return $ this ->data [$ index ];
203199 }
204200
205- if ($ this ->skipIfTriggered () && ! $ ignoreConditions ) {
201+ if ($ this ->skipIfTriggered () && !$ ignoreConditions ) {
206202 return $ this ;
207203 }
208204
209205 return $ this ->data ;
210206 }
211207
212208 /**
213- * get all items ignoring the current conditions
214- *
209+ * get all items ignoring the current conditions.
210+ *
215211 * @return array
216212 */
217213 public function all ()
@@ -229,13 +225,13 @@ public function set($value)
229225 if ($ this ->skipIfTriggered ()) {
230226 return $ this ;
231227 }
232-
228+
233229 $ oldData = $ this ->data ;
234230
235231 if (is_array ($ value )) {
236232 $ this ->data = $ value ;
237233 } elseif ($ value instanceof self) {
238- $ this ->data = $ value ->get (null , true );
234+ $ this ->data = $ value ->get (null , true );
239235 } elseif ($ value instanceof JsonSerializable) {
240236 $ this ->data = $ value ->jsonSerialize ();
241237 } elseif ($ value instanceof Traversable) {
@@ -251,7 +247,7 @@ public function set($value)
251247 }
252248 }
253249
254- $ this ->dataHasChanged = ! $ this ->equal ($ oldData , $ this ->data );
250+ $ this ->dataHasChanged = !$ this ->equal ($ oldData , $ this ->data );
255251
256252 unset($ oldData );
257253
@@ -328,7 +324,7 @@ public function do($callable, ...$args)
328324
329325 $ exceptionIndices = [];
330326
331- if (! array_key_exists ($ callable , $ exceptionIndices )) {
327+ if (!array_key_exists ($ callable , $ exceptionIndices )) {
332328 foreach ($ functionInfo ->getParameters () as $ order => $ arg ) {
333329 //no: value
334330 // if ($callable === 'array_intersect') {
@@ -364,15 +360,14 @@ public function do($callable, ...$args)
364360 !(isset ($ this ->isIf [$ this ->conditionDepth ]) && $ this ->isIf [$ this ->conditionDepth ]) &&
365361 !(isset ($ this ->isElse [$ this ->conditionDepth ]) && $ this ->isElse [$ this ->conditionDepth ])
366362 ) {
367-
368- if (in_array ($ callable , [
363+ if (in_array ($ callable , [
369364 'array_multisort ' , 'array_push ' , 'array_unshift ' , 'array_walk ' , 'array_walk_recursive ' ,
370365 'arsort ' , 'asort ' , 'krsort ' , 'ksort ' , 'rsort ' , 'sort ' , 'uasort ' , 'uksort ' , 'usort ' , 'natcasesort ' ,
371366 'natsort ' , 'shuffle ' ,
372367 ])) {
373368 return $ this ;
374369 }
375-
370+
376371 return $ result ;
377372 }
378373
@@ -382,14 +377,14 @@ public function do($callable, ...$args)
382377
383378 $ oldData = $ this ->data ;
384379
385- if (! ($ callable instanceof \Closure) && in_array ($ callable , ['array_fill_keys ' , 'array_fill ' ]) && is_array ($ result )) {
380+ if (! ($ callable instanceof \Closure) && in_array ($ callable , ['array_fill_keys ' , 'array_fill ' ]) && is_array ($ result )) {
386381 $ this ->data = $ this ->data + $ result ;
387382 } else {
388383 $ this ->data = $ result ;
389384 }
390385
391- $ this ->dataHasChanged = ! $ this ->equal ($ oldData , $ this ->data );
392-
386+ $ this ->dataHasChanged = !$ this ->equal ($ oldData , $ this ->data );
387+
393388 unset($ oldData );
394389
395390 return $ this ;
@@ -418,7 +413,6 @@ public function if($callable, ...$args)
418413
419414 $ result = $ this ->do ($ callable , ...$ args );
420415
421-
422416 if ($ result instanceof self && $ this ->equal ($ lastData , $ this ->data )) {
423417 $ this ->falseIfTriggered [$ this ->conditionDepth ] = true ;
424418 } elseif (is_array ($ result ) && $ this ->equal ($ lastData , $ result )) {
@@ -434,15 +428,15 @@ public function if($callable, ...$args)
434428 return $ this ;
435429 }
436430
437- protected function equal ($ array1 , $ array2 )
431+ protected function equal ($ array1 , $ array2 )
438432 {
439433 return is_array ($ array1 ) && is_array ($ array2 )
440434 && count ($ array1 ) == count ($ array2 )
441435 && array_diff ($ array1 , $ array2 ) === array_diff ($ array2 , $ array1 );
442436 }
443437
444438 /**
445- * did last method changed the array value
439+ * did last method changed the array value.
446440 *
447441 * @return bool
448442 */
@@ -489,7 +483,6 @@ public function else()
489483 return $ this ;
490484 }
491485
492-
493486 /**
494487 * Return the length of the current array.
495488 *
@@ -516,7 +509,7 @@ public function __debugInfo()
516509 }
517510
518511 /**
519- * unique hash of the array
512+ * unique hash of the array.
520513 *
521514 * @return string
522515 */
@@ -530,7 +523,7 @@ public function offsetGet($offset)
530523 return $ this ->__get ($ offset );
531524 }
532525
533- public function offsetSet ($ offset , $ value )
526+ public function offsetSet ($ offset , $ value )
534527 {
535528 $ this ->__set ($ offset , $ value );
536529 }
@@ -547,16 +540,14 @@ public function offsetUnset($offset)
547540
548541 public function __get ($ index )
549542 {
550- if (! is_null ($ index )) {
543+ if (!is_null ($ index )) {
551544 return $ this ->get ($ index );
552545 }
553-
554- return null ;
555546 }
556547
557548 public function __set ($ index , $ value )
558549 {
559- if ($ this ->data [$ index ] !== $ value ) {
550+ if ($ this ->data [$ index ] !== $ value ) {
560551 $ this ->dataHasChanged = true ;
561552 }
562553
0 commit comments