1111use Symfony \Component \PropertyAccess \PropertyPathInterface ;
1212use Traversable ;
1313
14+ /**
15+ * @author Jan Pech <[email protected] > 16+ */
1417class Arrays
1518{
1619 /** @var string */
@@ -305,14 +308,14 @@ public static function itemsWithMin($subject, $propertyPath = null)
305308 {
306309 throw new InvalidArgumentException ('Parameter $subject has to be type of array or Traversable. ' );
307310 }
308- if (Arrays::isEmpty ($ subject ))
309- {
310- throw new InvalidArgumentException ('Parameter $subject is empty. ' );
311- }
312311 if ($ propertyPath !== null && !is_callable ($ propertyPath ) && !is_string ($ propertyPath ) && !$ propertyPath instanceof PropertyPathInterface)
313312 {
314313 throw new InvalidArgumentException ('Parameter $propertyPath has to be NULL, callable, string or instance of ' . PropertyPathInterface::class . '. ' );
315314 }
315+ if (Arrays::isEmpty ($ subject ))
316+ {
317+ return [];
318+ }
316319 $ minValue = null ;
317320 $ items = [];
318321 foreach ($ subject as $ item )
@@ -373,14 +376,14 @@ public static function itemsWithMax($subject, $propertyPath = null)
373376 {
374377 throw new InvalidArgumentException ('Parameter $subject has to be type of array or Traversable. ' );
375378 }
376- if (Arrays::isEmpty ($ subject ))
377- {
378- throw new InvalidArgumentException ('Parameter $subject is empty. ' );
379- }
380379 if ($ propertyPath !== null && !is_callable ($ propertyPath ) && !is_string ($ propertyPath ) && !$ propertyPath instanceof PropertyPathInterface)
381380 {
382381 throw new InvalidArgumentException ('Parameter $propertyPath has to be NULL, callable, string or instance of ' . PropertyPathInterface::class . '. ' );
383382 }
383+ if (Arrays::isEmpty ($ subject ))
384+ {
385+ return [];
386+ }
384387 $ maxValue = null ;
385388 $ items = [];
386389 foreach ($ subject as $ item )
@@ -518,10 +521,6 @@ public static function groupBy($subject, $propertyPath)
518521 */
519522 public static function binarySearch (array $ subject , $ value )
520523 {
521- if (!is_array ($ subject ))
522- {
523- throw new InvalidArgumentException ('Parameter $subject has to be type of array. ' );
524- }
525524 $ left = 0 ;
526525 $ right = count ($ subject ) - 1 ;
527526 while ($ left <= $ right )
@@ -638,12 +637,8 @@ public static function contains($subject, $value)
638637 * @param $value
639638 * @return string|int|null
640639 */
641- public static function keyOf ($ subject , $ value )
640+ public static function keyOf (array $ subject , $ value )
642641 {
643- if (!is_array ($ subject ))
644- {
645- throw new InvalidArgumentException ('Parameter $subject has to be type of array. ' );
646- }
647642 $ key = array_search ($ value , $ subject , true );
648643 return $ key === false ? null : $ key ;
649644 }
@@ -725,12 +720,8 @@ public static function reverse($subject)
725720 * @param array $subject
726721 * @return array
727722 */
728- public static function flip ($ subject )
723+ public static function flip (array $ subject )
729724 {
730- if (!is_array ($ subject ))
731- {
732- throw new InvalidArgumentException ('Parameter $subject has to be type of array. ' );
733- }
734725 return array_flip ($ subject );
735726 }
736727
@@ -813,12 +804,8 @@ public static function mapByProperty($subject, $keyPropertyPath)
813804 * @param mixed $value
814805 * @return boolean
815806 */
816- public static function extract ($ subject , $ keyPath , &$ value = null )
807+ public static function extract (array $ subject , $ keyPath , &$ value = null )
817808 {
818- if (!is_array ($ subject ))
819- {
820- throw new InvalidArgumentException ('Parameter $subject has to be type of array. ' );
821- }
822809 if (!is_string ($ keyPath ) || Strings::isNullOrWhiteSpace ($ keyPath ))
823810 {
824811 throw new InvalidArgumentException ('Parameter $keyPath has to be non empty ("") and non-whitespace string. ' );
@@ -840,4 +827,4 @@ public static function extract($subject, $keyPath, &$value = null)
840827 }
841828 return false ;
842829 }
843- }
830+ }
0 commit comments