Skip to content

Commit

Permalink
Refactor BaseActiveRecord::getDirtyAttributes()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Dec 17, 2023
1 parent 37c551b commit a0c69ef
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

use function array_combine;
use function array_diff;
use function array_diff_key;
use function array_flip;
use function array_intersect;
use function array_intersect_key;
use function array_key_exists;
use function array_keys;
use function array_map;
Expand Down Expand Up @@ -181,40 +183,24 @@ public function getOldAttribute(string $name): mixed
public function getDirtyAttributes(array $names = null): array
{
if ($names === null) {
$names = $this->attributes();
$attributes = $this->attributes;
} else {
$attributes = array_intersect_key($this->attributes, array_flip($names));
}

$names = array_flip($names);
$attributes = [];

if ($this->oldAttributes === null) {
/**
* @var string $name
* @var mixed $value
*/
foreach ($this->attributes as $name => $value) {
if (isset($names[$name])) {
/** @psalm-var mixed */
$attributes[$name] = $value;
}
}
} else {
/**
* @var string $name
* @var mixed $value
*/
foreach ($this->attributes as $name => $value) {
if (
isset($names[$name])
&& (!array_key_exists($name, $this->oldAttributes) || $value !== $this->oldAttributes[$name])
) {
/** @psalm-var mixed */
$attributes[$name] = $value;
}
return $attributes;
}

$result = array_diff_key($attributes, $this->oldAttributes);

foreach (array_diff_key($attributes, $result) as $name => $value) {
if ($value !== $this->oldAttributes[$name]) {
$result[$name] = $value;
}
}

return $attributes;
return $result;
}

public function getOldAttributes(): array
Expand Down
52 changes: 52 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,56 @@ public function testToArrayForArrayable(): void
]),
);
}

public function testGetDirtyAttributesOnNewRecord(): void
{
$this->checkFixture($this->db, 'customer');

$customer = new Customer($this->db);

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('name', 'Adam');
$customer->setAttribute('email', '[email protected]');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes()
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status']),
);

$this->assertTrue($customer->save());
$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('address', '');

$this->assertSame(['address' => ''], $customer->getDirtyAttributes());
}

public function testGetDirtyAttributesAfterFind(): void
{
$this->checkFixture($this->db, 'customer');

$customerQuery = new ActiveQuery(Customer::class, $this->db);
$customer = $customerQuery->findOne(1);

$this->assertSame([], $customer->getDirtyAttributes());

$customer->setAttribute('name', 'Adam');
$customer->setAttribute('email', '[email protected]');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(),
);
$this->assertEquals(
['email' => '[email protected]', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status']),
);
}
}

0 comments on commit a0c69ef

Please sign in to comment.