1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-22 06:13:57 +02:00

Update WireArray::import() to support duplicate items when duplicate checking is turned off per processwire/processwire-issues#767

This commit is contained in:
Ryan Cramer
2018-12-14 13:12:52 -05:00
parent b773c81ae9
commit a89543944d

View File

@@ -225,7 +225,7 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
* Import the given item(s) into this WireArray.
*
* - Adds imported items to the end of the WireArray.
* - Skips over any items already present in the WireArray.
* - Skips over any items already present in the WireArray (when duplicateChecking is enabled)
*
* #pw-group-manipulation
*
@@ -236,13 +236,18 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
*/
public function import($items) {
if(!is_array($items) && !self::iterable($items))
throw new WireException('WireArray cannot import non arrays or non-iterable objects');
if(!is_array($items) && !self::iterable($items)) {
throw new WireException('WireArray cannot import non arrays or non-iterable objects');
}
foreach($items as $key => $value) {
if(($k = $this->getItemKey($value)) !== null) $key = $k;
if(isset($this->data[$key])) continue; // won't overwrite existing keys
$this->set($key, $value);
if($this->duplicateChecking) {
if(($k = $this->getItemKey($value)) !== null) $key = $k;
if(isset($this->data[$key])) continue; // won't overwrite existing keys
$this->set($key, $value);
} else {
$this->add($value);
}
}
return $this;