performance improvements

This commit is contained in:
Joby Elliott 2021-09-13 10:52:13 -06:00
parent 188dffe8d5
commit 61d24eb3e8
2 changed files with 17 additions and 30 deletions

View file

@ -148,9 +148,7 @@ trait FlatArrayTrait
*/ */
protected function flattenSearch(?string $name, $value = null, $unset = false) 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 //check for home strings
if ($name == '' || $name === null) { if ($name == '' || $name === null) {
if ($unset) { if ($unset) {
@ -201,20 +199,4 @@ trait FlatArrayTrait
} }
return @$parent[$key]; 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;
}
} }

View file

@ -1,9 +1,12 @@
<?php <?php
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr; namespace Flatrr;
class SelfReferencingFlatArray extends FlatArray class SelfReferencingFlatArray extends FlatArray
{ {
protected $cache = [];
public function get(string $name = null, bool $raw = false, $unescape = true) public function get(string $name = null, bool $raw = false, $unescape = true)
{ {
$out = parent::get($name); $out = parent::get($name);
@ -19,12 +22,13 @@ class SelfReferencingFlatArray extends FlatArray
public function set(?string $name, $value) public function set(?string $name, $value)
{ {
return $this->filter(parent::set($name,$value)); $this->cache = [];
return $this->filter(parent::set($name, $value));
} }
public function push(?string $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) public function pop(?string $name)
@ -34,7 +38,7 @@ class SelfReferencingFlatArray extends FlatArray
public function unshift(?string $name, $value) 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) public function shift(?string $name)
@ -96,14 +100,15 @@ class SelfReferencingFlatArray extends FlatArray
//search/replace on string values //search/replace on string values
if (is_string($value) && strpos($value, '${') !== false) { if (is_string($value) && strpos($value, '${') !== false) {
//search for valid replacements //search for valid replacements
return preg_replace_callback( return $this->cache[$value] ??
//search for things like ${var/name}, escape with \ before last brace ($this->cache[$value] = preg_replace_callback(
'/\$\{([^\}]*[^\.\\\])\}/S', //search for things like ${var/name}, escape with \ before last brace
//replace match with value from $this if it exists '/\$\{([^\}]*[^\.\\\])\}/',
[$this, 'filter_regex'], //replace match with value from $this if it exists
//applied to $value [$this, 'filter_regex'],
$value //applied to $value
); $value
));
} }
//fall back to just returning value, it's some other datatype //fall back to just returning value, it's some other datatype
return $value; return $value;