diff --git a/src/FlatArrayTrait.php b/src/FlatArrayTrait.php index d3218f7..e985190 100644 --- a/src/FlatArrayTrait.php +++ b/src/FlatArrayTrait.php @@ -148,9 +148,7 @@ trait FlatArrayTrait */ protected function flattenSearch(?string $name, $value = null, $unset = false) { - //normalize key names in $value, and also $name - $value = $this->normalizeValue($value); - $name = $name; + //check for home strings if ($name == '' || $name === null) { if ($unset) { @@ -201,20 +199,4 @@ trait FlatArrayTrait } return @$parent[$key]; } - - protected function normalizeValue($value) - { - if (!is_array($value)) { - return $value; - } - $norm = []; - foreach ($value as $key => $value) { - $nKey = preg_replace('/\./', '', $key); - if ($nKey == '') { - throw new \Exception("Key \"$key\" can't be used inside a FlatArray"); - } - $norm[$nKey] = $this->normalizeValue($value); - } - return $norm; - } } diff --git a/src/SelfReferencingFlatArray.php b/src/SelfReferencingFlatArray.php index 9dbd206..be12251 100644 --- a/src/SelfReferencingFlatArray.php +++ b/src/SelfReferencingFlatArray.php @@ -1,9 +1,12 @@ filter(parent::set($name,$value)); + $this->cache = []; + return $this->filter(parent::set($name, $value)); } public function push(?string $name, $value) { - return $this->filter(parent::push($name,$value)); + return $this->filter(parent::push($name, $value)); } public function pop(?string $name) @@ -34,7 +38,7 @@ class SelfReferencingFlatArray extends FlatArray public function unshift(?string $name, $value) { - return $this->filter(parent::unshift($name,$value)); + return $this->filter(parent::unshift($name, $value)); } public function shift(?string $name) @@ -96,14 +100,15 @@ class SelfReferencingFlatArray extends FlatArray //search/replace on string values if (is_string($value) && strpos($value, '${') !== false) { //search for valid replacements - return preg_replace_callback( - //search for things like ${var/name}, escape with \ before last brace - '/\$\{([^\}]*[^\.\\\])\}/S', - //replace match with value from $this if it exists - [$this, 'filter_regex'], - //applied to $value - $value - ); + return $this->cache[$value] ?? + ($this->cache[$value] = preg_replace_callback( + //search for things like ${var/name}, escape with \ before last brace + '/\$\{([^\}]*[^\.\\\])\}/', + //replace match with value from $this if it exists + [$this, 'filter_regex'], + //applied to $value + $value + )); } //fall back to just returning value, it's some other datatype return $value;