Commit 409c508f by Qiang Xue

...

parent 7a234327
......@@ -9,6 +9,8 @@
namespace yii\base;
use yii\util\ArrayHelper;
/**
* Dictionary implements a collection that stores key-value pairs.
*
......@@ -203,17 +205,15 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
*
* Existing elements in the dictionary will be overwritten if their keys are the same as those in the source.
* If the merge is recursive, the following algorithm is performed:
* <ul>
* <li>the dictionary data is saved as $a, and the source data is saved as $b;</li>
* <li>if $a and $b both have an array indxed at the same string key, the arrays will be merged using this algorithm;</li>
* <li>any integer-indexed elements in $b will be appended to $a and reindxed accordingly;</li>
* <li>any string-indexed elements in $b will overwrite elements in $a with the same index;</li>
* </ul>
*
* @param mixed $data the data to be merged with, must be an array or object implementing Traversable
* @param boolean $recursive whether the merging should be recursive.
* - the dictionary data is saved as $a, and the source data is saved as $b;
* - if $a and $b both have an array indexed at the same string key, the arrays will be merged using this algorithm;
* - any integer-indexed elements in $b will be appended to $a;
* - any string-indexed elements in $b will overwrite elements in $a with the same index;
*
* @throws Exception If data is neither an array nor an iterator.
* @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable
* @param boolean $recursive whether the merging should be recursive.
* @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/
public function mergeWith($data, $recursive = true)
{
......@@ -227,9 +227,9 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
foreach ($data as $key => $value) {
$d[$key] = $value;
}
$this->_d = self::mergeArray($this->_d, $d);
$this->_d = ArrayHelper::merge($this->_d, $d);
} else {
$this->_d = self::mergeArray($this->_d, $data);
$this->_d = ArrayHelper::merge($this->_d, $data);
}
} else {
foreach ($data as $key => $value) {
......@@ -237,7 +237,7 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
}
}
} else {
throw new Exception('Dictionary data must be an array or an object implementing Traversable.');
throw new Exception('The data to be merged with must be an array or an object implementing Traversable.');
}
}
......@@ -293,31 +293,4 @@ class Dictionary extends Object implements \IteratorAggregate, \ArrayAccess, \Co
{
$this->remove($offset);
}
/**
* Merges two arrays into one recursively.
* If each array has an element with the same string key value, the latter
* will overwrite the former (different from array_merge_recursive).
* Recursive merging will be conducted if both arrays have an element of array
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* @param array $a array to be merged to
* @param array $b array to be merged from
* @return array the merged array (the original arrays are not changed.)
* @see mergeWith
*/
public static function mergeArray($a, $b)
{
foreach ($b as $k => $v) {
if (is_integer($k)) {
isset($a[$k]) ? $a[] = $v : $a[$k] = $v;
} elseif (is_array($v) && isset($a[$k]) && is_array($a[$k])) {
$a[$k] = self::mergeArray($a[$k], $v);
} else {
$a[$k] = $v;
}
}
return $a;
}
}
......@@ -68,7 +68,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
* Returns an iterator for traversing the items in the vector.
* This method is required by the SPL interface `IteratorAggregate`.
* It will be implicitly called when you use `foreach` to traverse the vector.
* @return Iterator an iterator for traversing the items in the vector.
* @return VectorIterator an iterator for traversing the items in the vector.
*/
public function getIterator()
{
......@@ -262,7 +262,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
/**
* Merges iterable data into the vector.
* New items will be appended to the end of the existing items.
* @param mixed $data the data to be merged with, must be an array or an object implementing `Traversable`
* @param array|\Traversable $data the data to be merged with. It must be an array or object implementing Traversable
* @throws Exception if data is neither an array nor an object implementing `Traversable`.
*/
public function mergeWith($data)
......@@ -275,7 +275,7 @@ class Vector extends Object implements \IteratorAggregate, \ArrayAccess, \Counta
$this->add($item);
}
} else {
throw new Exception('Data must be either an array or an object implementing Traversable.');
throw new Exception('The data to be merged with must be an array or an object implementing Traversable.');
}
}
......
......@@ -2,7 +2,6 @@
/**
* Exception class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* BaseQuery class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* ColumnSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Command class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Connection class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* DataReader class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Driver class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Expression class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Query class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
<?php
/**
* This file contains the Command class.
* QueryBuilder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* TableSchema class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Transaction class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Driver class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* QueryBuilder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Driver class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* QueryBuilder class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* DbTarget class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* EmailTarget class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* FileTarget class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Logger class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* CProfileLogRoute class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Router class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* Target class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
......@@ -2,7 +2,6 @@
/**
* CWebLogRoute class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
<?php
/**
* ArrayHelper class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\util;
/**
* ArrayHelper is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ArrayHelper extends \yii\base\Component
{
/**
* Merges two arrays into one recursively.
* If each array has an element with the same string key value, the latter
* will overwrite the former (different from array_merge_recursive).
* Recursive merging will be conducted if both arrays have an element of array
* type and are having the same key.
* For integer-keyed elements, the elements from the latter array will
* be appended to the former array.
* @param array $a array to be merged to
* @param array $b array to be merged from
* @return array the merged array (the original arrays are not changed.)
* @see mergeWith
*/
public static function merge($a, $b)
{
foreach ($b as $k => $v) {
if (is_integer($k)) {
isset($a[$k]) ? $a[] = $v : $a[$k] = $v;
} elseif (is_array($v) && isset($a[$k]) && is_array($a[$k])) {
$a[$k] = static::merge($a[$k], $v);
} else {
$a[$k] = $v;
}
}
return $a;
}
/**
* Retrieves the value of an array element with the specified key.
*
* If the key does not exist in the array, the default value will be returned instead.
* For example,
*
* ~~~
* $username = \yii\util\ArrayHelper::get($_POST, 'username');
* ~~~
*
* @param array $array array to extract value from
* @param string $key key name of the array element
* @param mixed $default the default value to be returned if the specified key does not exist
* @return mixed
*/
public static function get($array, $key, $default = null)
{
return isset($array[$key]) || array_key_exists($key, $array) ? $array[$key] : $default;
}
}
\ No newline at end of file
......@@ -2,8 +2,6 @@
/**
* Filesystem helper class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......@@ -14,6 +12,8 @@ namespace yii\util;
/**
* Filesystem helper
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class File
......
......@@ -2,8 +2,6 @@
/**
* Text helper class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alex Makarov <sam@rmcreative.ru>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment