"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "framework/helpers/BaseArrayHelper.php" between
yii2-2.0.35.tar.gz and yii2-2.0.36.tar.gz

About: Yii 2 is a high-performance component-based PHP framework for developing large-scale Web applications (source).

BaseArrayHelper.php  (yii2-2.0.35):BaseArrayHelper.php  (yii2-2.0.36)
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\helpers; namespace yii\helpers;
use ArrayAccess;
use Traversable;
use Yii; use Yii;
use yii\base\Arrayable; use yii\base\Arrayable;
use yii\base\InvalidArgumentException; use yii\base\InvalidArgumentException;
/** /**
* BaseArrayHelper provides concrete implementation for [[ArrayHelper]]. * BaseArrayHelper provides concrete implementation for [[ArrayHelper]].
* *
* Do not use BaseArrayHelper. Use [[ArrayHelper]] instead. * Do not use BaseArrayHelper. Use [[ArrayHelper]] instead.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
skipping to change at line 197 skipping to change at line 199
} }
if (is_array($key)) { if (is_array($key)) {
$lastKey = array_pop($key); $lastKey = array_pop($key);
foreach ($key as $keyPart) { foreach ($key as $keyPart) {
$array = static::getValue($array, $keyPart); $array = static::getValue($array, $keyPart);
} }
$key = $lastKey; $key = $lastKey;
} }
if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $ array))) { if (static::keyExists($key, $array)) {
return $array[$key]; return $array[$key];
} }
if (($pos = strrpos($key, '.')) !== false) { if (($pos = strrpos($key, '.')) !== false) {
$array = static::getValue($array, substr($key, 0, $pos), $default); $array = static::getValue($array, substr($key, 0, $pos), $default);
$key = substr($key, $pos + 1); $key = substr($key, $pos + 1);
} }
if (static::keyExists($key, $array)) {
return $array[$key];
}
if (is_object($array)) { if (is_object($array)) {
// this is expected to fail if the property does not exist, or __get () is not implemented // this is expected to fail if the property does not exist, or __get () is not implemented
// it is not reliably possible to check whether a property is access ible beforehand // it is not reliably possible to check whether a property is access ible beforehand
return $array->$key; try {
} elseif (is_array($array)) { return $array->$key;
return (isset($array[$key]) || array_key_exists($key, $array)) ? $ar } catch (\Exception $e) {
ray[$key] : $default; if ($array instanceof ArrayAccess) {
return $default;
}
throw $e;
}
} }
return $default; return $default;
} }
/** /**
* Writes a value into an associative array at the key path specified. * Writes a value into an associative array at the key path specified.
* If there is no such key path yet, it will be created recursively. * If there is no such key path yet, it will be created recursively.
* If the key exists, it will be overwritten. * If the key exists, it will be overwritten.
* *
skipping to change at line 596 skipping to change at line 606
} }
return $result; return $result;
} }
/** /**
* Checks if the given array contains the specified key. * Checks if the given array contains the specified key.
* This method enhances the `array_key_exists()` function by supporting case -insensitive * This method enhances the `array_key_exists()` function by supporting case -insensitive
* key comparison. * key comparison.
* @param string $key the key to check * @param string $key the key to check
* @param array $array the array with keys to check * @param array|ArrayAccess $array the array with keys to check
* @param bool $caseSensitive whether the key comparison should be case-sens itive * @param bool $caseSensitive whether the key comparison should be case-sens itive
* @return bool whether the array contains the specified key * @return bool whether the array contains the specified key
*/ */
public static function keyExists($key, $array, $caseSensitive = true) public static function keyExists($key, $array, $caseSensitive = true)
{ {
if ($caseSensitive) { if ($caseSensitive) {
// Function `isset` checks key faster but skips `null`, `array_key_e xists` handles this case // Function `isset` checks key faster but skips `null`, `array_key_e xists` handles this case
// https://secure.php.net/manual/en/function.array-key-exists.php#10 7786 // https://secure.php.net/manual/en/function.array-key-exists.php#10 7786
return isset($array[$key]) || array_key_exists($key, $array); if (is_array($array) && (isset($array[$key]) || array_key_exists($ke
y, $array))) {
return true;
}
// Cannot use `array_has_key` on Objects for PHP 7.4+, therefore we
need to check using [[ArrayAccess::offsetExists()]]
return $array instanceof ArrayAccess && $array->offsetExists($key);
}
if ($array instanceof ArrayAccess) {
throw new InvalidArgumentException('Second parameter($array) cannot
be ArrayAccess in case insensitive mode');
} }
foreach (array_keys($array) as $k) { foreach (array_keys($array) as $k) {
if (strcasecmp($key, $k) === 0) { if (strcasecmp($key, $k) === 0) {
return true; return true;
} }
} }
return false; return false;
} }
skipping to change at line 808 skipping to change at line 826
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
if (!is_int($key)) { if (!is_int($key)) {
return false; return false;
} }
} }
return true; return true;
} }
/** /**
* Check whether an array or [[\Traversable]] contains an element. * Check whether an array or [[Traversable]] contains an element.
* *
* This method does the same as the PHP function [in_array()](https://secure .php.net/manual/en/function.in-array.php) * This method does the same as the PHP function [in_array()](https://secure .php.net/manual/en/function.in-array.php)
* but additionally works for objects that implement the [[\Traversable]] in terface. * but additionally works for objects that implement the [[Traversable]] int erface.
* @param mixed $needle The value to look for. * @param mixed $needle The value to look for.
* @param array|\Traversable $haystack The set of values to search. * @param array|Traversable $haystack The set of values to search.
* @param bool $strict Whether to enable strict (`===`) comparison. * @param bool $strict Whether to enable strict (`===`) comparison.
* @return bool `true` if `$needle` was found in `$haystack`, `false` otherw ise. * @return bool `true` if `$needle` was found in `$haystack`, `false` otherw ise.
* @throws InvalidArgumentException if `$haystack` is neither traversable no r an array. * @throws InvalidArgumentException if `$haystack` is neither traversable no r an array.
* @see https://secure.php.net/manual/en/function.in-array.php * @see https://secure.php.net/manual/en/function.in-array.php
* @since 2.0.7 * @since 2.0.7
*/ */
public static function isIn($needle, $haystack, $strict = false) public static function isIn($needle, $haystack, $strict = false)
{ {
if ($haystack instanceof \Traversable) { if ($haystack instanceof Traversable) {
foreach ($haystack as $value) { foreach ($haystack as $value) {
if ($needle == $value && (!$strict || $needle === $value)) { if ($needle == $value && (!$strict || $needle === $value)) {
return true; return true;
} }
} }
} elseif (is_array($haystack)) { } elseif (is_array($haystack)) {
return in_array($needle, $haystack, $strict); return in_array($needle, $haystack, $strict);
} else { } else {
throw new InvalidArgumentException('Argument $haystack must be an ar ray or implement Traversable'); throw new InvalidArgumentException('Argument $haystack must be an ar ray or implement Traversable');
} }
return false; return false;
} }
/** /**
* Checks whether a variable is an array or [[\Traversable]]. * Checks whether a variable is an array or [[Traversable]].
* *
* This method does the same as the PHP function [is_array()](https://secure .php.net/manual/en/function.is-array.php) * This method does the same as the PHP function [is_array()](https://secure .php.net/manual/en/function.is-array.php)
* but additionally works on objects that implement the [[\Traversable]] int erface. * but additionally works on objects that implement the [[Traversable]] inte rface.
* @param mixed $var The variable being evaluated. * @param mixed $var The variable being evaluated.
* @return bool whether $var is array-like * @return bool whether $var can be traversed via foreach
* @see https://secure.php.net/manual/en/function.is-array.php * @see https://secure.php.net/manual/en/function.is-array.php
* @since 2.0.8 * @since 2.0.8
*/ */
public static function isTraversable($var) public static function isTraversable($var)
{ {
return is_array($var) || $var instanceof \Traversable; return is_array($var) || $var instanceof Traversable;
} }
/** /**
* Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]]. * Checks whether an array or [[Traversable]] is a subset of another array o r [[Traversable]].
* *
* This method will return `true`, if all elements of `$needles` are contain ed in * This method will return `true`, if all elements of `$needles` are contain ed in
* `$haystack`. If at least one element is missing, `false` will be returned . * `$haystack`. If at least one element is missing, `false` will be returned .
* @param array|\Traversable $needles The values that must **all** be in `$h * @param array|Traversable $needles The values that must **all** be in `$ha
aystack`. ystack`.
* @param array|\Traversable $haystack The set of value to search. * @param array|Traversable $haystack The set of value to search.
* @param bool $strict Whether to enable strict (`===`) comparison. * @param bool $strict Whether to enable strict (`===`) comparison.
* @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array. * @throws InvalidArgumentException if `$haystack` or `$needles` is neither traversable nor an array.
* @return bool `true` if `$needles` is a subset of `$haystack`, `false` oth erwise. * @return bool `true` if `$needles` is a subset of `$haystack`, `false` oth erwise.
* @since 2.0.7 * @since 2.0.7
*/ */
public static function isSubset($needles, $haystack, $strict = false) public static function isSubset($needles, $haystack, $strict = false)
{ {
if (is_array($needles) || $needles instanceof \Traversable) { if (is_array($needles) || $needles instanceof Traversable) {
foreach ($needles as $needle) { foreach ($needles as $needle) {
if (!static::isIn($needle, $haystack, $strict)) { if (!static::isIn($needle, $haystack, $strict)) {
return false; return false;
} }
} }
return true; return true;
} }
throw new InvalidArgumentException('Argument $needles must be an array o r implement Traversable'); throw new InvalidArgumentException('Argument $needles must be an array o r implement Traversable');
 End of changes. 17 change blocks. 
20 lines changed or deleted 40 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)