Skip to content

Commit

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

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 @@ -183,24 +181,40 @@ public function getOldAttribute(string $name): mixed
public function getDirtyAttributes(array $names = null): array
{
if ($names === null) {
$attributes = $this->attributes;
} else {
$attributes = array_intersect_key($this->attributes, array_flip($names));
}

if ($this->oldAttributes === null) {
return $attributes;
$names = $this->attributes();
}

$result = array_diff_key($attributes, $this->oldAttributes);
$names = array_flip($names);
$attributes = [];

foreach (array_diff_key($attributes, $result) as $name => $value) {
if ($value !== $this->oldAttributes[$name]) {
$result[$name] = $value;
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 $result;
return $attributes;
}

public function getOldAttributes(): array
Expand Down
52 changes: 0 additions & 52 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,56 +836,4 @@ 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', 'adam@example.com');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => 'adam@example.com', 'address' => null],
$customer->getDirtyAttributes()
);
$this->assertEquals(
['email' => 'adam@example.com', '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', 'adam@example.com');
$customer->setAttribute('address', null);

$this->assertEquals(
['name' => 'Adam', 'email' => 'adam@example.com', 'address' => null],
$customer->getDirtyAttributes(),
);
$this->assertEquals(
['email' => 'adam@example.com', 'address' => null],
$customer->getDirtyAttributes(['id', 'email', 'address', 'status']),
);
}
}

0 comments on commit 0e3a2a0

Please sign in to comment.