From 41e4cf4249c4f40cc4cb8c4048a832592c06834d Mon Sep 17 00:00:00 2001 From: lav45 Date: Thu, 19 Sep 2024 18:21:44 +0300 Subject: [PATCH] Updated the changedAttributes filling method; remove method array_diff_recursive($arr1, $arr2) --- src/SerializeBehavior.php | 45 +++++++++------------------ tests/units/SerializeBehaviorTest.php | 21 ++++++++++++- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/SerializeBehavior.php b/src/SerializeBehavior.php index 61e4de3..d758570 100644 --- a/src/SerializeBehavior.php +++ b/src/SerializeBehavior.php @@ -71,40 +71,23 @@ public function beforeSave() */ public function afterSave(AfterSaveEvent $event) { - $diff = $this->array_diff_recursive($this->data, $this->oldData); - foreach (array_keys($diff) as $key) { - $event->changedAttributes[$key] = isset($this->oldData[$key]) ? $this->oldData[$key] : null; + foreach ($this->data as $key => $_) { + if (isset($this->oldData[$key])) { + $old = $this->oldData[$key]; + } elseif ($this->attributes[$key] instanceof Closure || (is_array($this->attributes[$key]) && is_callable($this->attributes[$key]))) { + $old = call_user_func($this->attributes[$key]); + } else { + $old = $this->attributes[$key]; + } + if ($this->data[$key] !== $old) { + $event->changedAttributes[$key] = $old; + } } $this->oldData = $this->data; $this->changeStorageAttribute = false; } - /** - * @param array $arr1 - * @param array $arr2 - * @return array - */ - protected function array_diff_recursive($arr1, $arr2) - { - $result = []; - foreach ($arr1 as $key => $value) { - if (isset($arr2[$key]) || array_key_exists($key, $arr2)) { - if (is_array($value)) { - $recursiveDiff = $this->array_diff_recursive($value, $arr2[$key]); - if (count($recursiveDiff)) { - $result[$key] = $recursiveDiff; - } - } elseif (in_array($value, $arr2, true) === false) { - $result[$key] = $value; - } - } elseif (in_array($value, $arr2, true) === false) { - $result[$key] = $value; - } - } - return $result; - } - /** * @param array $data */ @@ -113,10 +96,10 @@ public function setAttributes(array $data) $this->attributes = []; foreach ($data as $key => $value) { if (is_int($key)) { - $this->attributes[$value] = null; - } else { - $this->attributes[$key] = $value; + $key = $value; + $value = null; } + $this->attributes[$key] = $value; } } diff --git a/tests/units/SerializeBehaviorTest.php b/tests/units/SerializeBehaviorTest.php index 2bc3158..c974fea 100644 --- a/tests/units/SerializeBehaviorTest.php +++ b/tests/units/SerializeBehaviorTest.php @@ -13,11 +13,11 @@ protected function getDefaultData() return [ 'description' => 'description', 'meta' => [ - 'title' => 'meta-title', 'description' => 'meta-description', 'keywords' => 'meta key words', ], 'is_active' => true, + 'defaultFunc' => 3 ]; } @@ -84,6 +84,25 @@ public function testUpdateChangedAttributes() $model->on(News::EVENT_AFTER_INSERT, function (AfterSaveEvent $event) { $this->assertArrayHasKey('title', $event->changedAttributes); $this->assertNull($event->changedAttributes['title']); + + $this->assertArrayHasKey('_data', $event->changedAttributes); + $this->assertNull($event->changedAttributes['_data']); + + $this->assertArrayHasKey('id', $event->changedAttributes); + $this->assertNull($event->changedAttributes['id']); + + $this->assertArrayHasKey('description', $event->changedAttributes); + $this->assertNull($event->changedAttributes['description']); + + $this->assertArrayHasKey('meta', $event->changedAttributes); + $this->assertEquals(['keywords' => null, 'description' => null], $event->changedAttributes['meta']); + + $this->assertArrayHasKey('meta_keywords', $event->changedAttributes); + $this->assertNull($event->changedAttributes['meta_keywords']); + + $this->assertArrayNotHasKey('is_active', $event->changedAttributes); + + $this->assertArrayNotHasKey('defaultFunc', $event->changedAttributes); }); $model->save(false);