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,6 +22,7 @@ class SelfReferencingFlatArray extends FlatArray
public function set(?string $name, $value) public function set(?string $name, $value)
{ {
$this->cache = [];
return $this->filter(parent::set($name, $value)); return $this->filter(parent::set($name, $value));
} }
@ -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] ??
($this->cache[$value] = preg_replace_callback(
//search for things like ${var/name}, escape with \ before last brace //search for things like ${var/name}, escape with \ before last brace
'/\$\{([^\}]*[^\.\\\])\}/S', '/\$\{([^\}]*[^\.\\\])\}/',
//replace match with value from $this if it exists //replace match with value from $this if it exists
[$this, 'filter_regex'], [$this, 'filter_regex'],
//applied to $value //applied to $value
$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;