From 0de355405690e1af1499e63a8f1fd26765420a96 Mon Sep 17 00:00:00 2001 From: Paul-Henri Leobon <25308170+paulhenri-l@users.noreply.github.com> Date: Wed, 2 Sep 2020 13:45:35 +0200 Subject: [PATCH] Rename and remove wildcard for performance reasons --- composer.json | 2 +- src/HasDynamicAttributes.php | 58 ++++++++-------------------- tests/Unit/DynamicAttributesTest.php | 32 --------------- 3 files changed, 18 insertions(+), 74 deletions(-) diff --git a/composer.json b/composer.json index 76c1ed4..a967a59 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "phl/laravel-dynamic-attributes", + "name": "paulhenri-l/laravel-dynamic-attributes", "description": "Dynamically add attributes to your eloquent models", "type": "library", "license": "MIT", diff --git a/src/HasDynamicAttributes.php b/src/HasDynamicAttributes.php index e1f4774..4dc63f1 100644 --- a/src/HasDynamicAttributes.php +++ b/src/HasDynamicAttributes.php @@ -13,14 +13,6 @@ trait HasDynamicAttributes */ protected $dynamicAttributes = []; - /** - * Keep a cached array of previously matched attributes. So that we don't - * run a preg match on every get and set call. - * - * @var ?string[] - */ - protected $previousMatches = []; - /** * Register a dynamic attribute. * @@ -53,13 +45,13 @@ public function registerDynamicAttributeClass(string $key, DynamicAttribute $dyn * @param string $key * @return mixed */ - public function __get($key) + public function getAttribute($key) { - if ($daKey = $this->getDynamicAttributeKey($key)) { - return $this->dynamicAttributes[$daKey]->get($key); + if ($da = $this->getDynamicAttributeClass($key)) { + return $da->get($key); } - return parent::__get($key); + return parent::getAttribute($key); } /** @@ -67,43 +59,27 @@ public function __get($key) * * @param string $key * @param mixed $value - * @return void + * @return mixed */ - public function __set($key, $value) + public function setAttribute($key, $value) { - if ($daKey = $this->getDynamicAttributeKey($key)) { - $this->dynamicAttributes[$daKey]->set($key, $value); - return; - } + if ($da = $this->getDynamicAttributeClass($key)) { + $da->set($key, $value); - parent::__set($key, $value); - } + return $this; + } - /** - * Flush the cache of previously matched dynamic attribute keys. - */ - public function flushDynamicAttributeKeyCache() - { - $this->previousMatches = []; + return parent::setAttribute($key, $value); } /** - * Check if the given key matches with a dynamic attribute. + * Return the dynamic attribute class. + * + * @param string $key + * @return DynamicAttribute|null */ - protected function getDynamicAttributeKey(string $key): ?string + protected function getDynamicAttributeClass(string $key): ?DynamicAttribute { - $cache = $this->previousMatches; - - if (isset($cache[$key])) { - return $cache[$key]; - } - - $daKey = array_filter(array_keys($this->dynamicAttributes), function ($da) use ($key) { - return preg_match('/^' . $da . '$/', $key); - }); - - $result = $cache[$key] = array_shift($daKey); - - return $result; + return $this->dynamicAttributes[$key] ?? null; } } diff --git a/tests/Unit/DynamicAttributesTest.php b/tests/Unit/DynamicAttributesTest.php index 35078b4..88d08f0 100644 --- a/tests/Unit/DynamicAttributesTest.php +++ b/tests/Unit/DynamicAttributesTest.php @@ -64,38 +64,6 @@ public function test_dynamic_attribute_class() $this->assertEquals('hello', $da->get); $this->assertEquals(['hello' => 'world'], $da->set); } - - public function test_wildcard_attribute() - { - $member = new Member(); - $wildcardAttribute = new WildcardAttribute(); - $member->registerDynamicAttributeClass('prefix_.*', $wildcardAttribute); - $member->prefix_hello = 'world'; - $member->prefix_foo = 'bar'; - $member->prefix_test = 'value'; - $member->not_prefix_sorry = 'not there'; - - $this->assertEquals([ - 'prefix_hello' => 'world', - 'prefix_foo' => 'bar', - 'prefix_test' => 'value', - ], $wildcardAttribute->data); - } -} - -class WildcardAttribute implements DynamicAttribute -{ - public $data = []; - - public function get(string $key) - { - return $data[$key] ?? null; - } - - public function set(string $key, $value): void - { - $this->data[$key] = $value; - } } class FakeDaClass implements DynamicAttribute