Add helper function for checking if an object is an array-like object.

Added support for traversable objects in `BaseHtml` and `ArrayHelper`
This commit is contained in:
Sam Mousa
2016-02-24 11:59:01 +00:00
committed by SilverFire - Dmitry Naumenko
parent 80e9331fe0
commit 16a6af5fa8
5 changed files with 58 additions and 8 deletions

View File

@ -680,7 +680,7 @@ class BaseArrayHelper
* Check whether an array or [[\Traversable]] contains an element.
*
* This method does the same as the PHP function [in_array()](http://php.net/manual/en/function.in-array.php)
* but it does not only work for arrays but also objects that implement the [[\Traversable]] interface.
* but additionally works for objects that implement the [[\Traversable]] interface.
* @param mixed $needle The value to look for.
* @param array|\Traversable $haystack The set of values to search.
* @param boolean $strict Whether to enable strict (`===`) comparison.
@ -706,6 +706,21 @@ class BaseArrayHelper
return false;
}
/**
* Checks whether a variable is an array or [[\Traversable]].
*
* This method does the same as the PHP function [is_array()](http://php.net/manual/en/function.is-array.php)
* but additionally works objects that implement the [[\Traversable]] interface.
* @param mixed $var The variable being evaluated.
* @return boolean whether $var is array-like
* @see http://php.net/manual/en/function.is_array.php
* @since 2.0.8
*/
public static function isTraversable($var)
{
return is_array($var) || $var instanceof \Traversable;
}
/**
* Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]].
*