1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-22 14:23:05 +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. * Import the given item(s) into this WireArray.
* *
* - Adds imported items to the end of the 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 * #pw-group-manipulation
* *
@@ -236,13 +236,18 @@ class WireArray extends Wire implements \IteratorAggregate, \ArrayAccess, \Count
*/ */
public function import($items) { public function import($items) {
if(!is_array($items) && !self::iterable($items)) if(!is_array($items) && !self::iterable($items)) {
throw new WireException('WireArray cannot import non arrays or non-iterable objects'); throw new WireException('WireArray cannot import non arrays or non-iterable objects');
}
foreach($items as $key => $value) { foreach($items as $key => $value) {
if(($k = $this->getItemKey($value)) !== null) $key = $k; if($this->duplicateChecking) {
if(isset($this->data[$key])) continue; // won't overwrite existing keys if(($k = $this->getItemKey($value)) !== null) $key = $k;
$this->set($key, $value); if(isset($this->data[$key])) continue; // won't overwrite existing keys
$this->set($key, $value);
} else {
$this->add($value);
}
} }
return $this; return $this;