From a89543944db517e53cc4cdf8df942218f3fddf91 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 14 Dec 2018 13:12:52 -0500 Subject: [PATCH] Update WireArray::import() to support duplicate items when duplicate checking is turned off per processwire/processwire-issues#767 --- wire/core/WireArray.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/wire/core/WireArray.php b/wire/core/WireArray.php index dea220c1..4a7b3d34 100644 --- a/wire/core/WireArray.php +++ b/wire/core/WireArray.php @@ -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;