Skip to content

Commit

Permalink
Updated the changedAttributes filling method; remove method array_dif…
Browse files Browse the repository at this point in the history
…f_recursive($arr1, $arr2)
  • Loading branch information
lav45 committed Sep 19, 2024
1 parent 422ca28 commit 41e4cf4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
45 changes: 14 additions & 31 deletions src/SerializeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
}

Expand Down
21 changes: 20 additions & 1 deletion tests/units/SerializeBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 41e4cf4

Please sign in to comment.