Skip to content

Commit

Permalink
Merge branch 'master' into refactor-getDirtyAttributes
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/BaseActiveRecord.php
  • Loading branch information
Tigrov committed Dec 29, 2023
2 parents ade14f6 + eb86518 commit 5964f2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use function array_combine;
use function array_diff_key;
use function array_diff;
use function array_flip;
use function array_intersect;
use function array_intersect_key;
Expand Down Expand Up @@ -142,15 +143,12 @@ public function getAttributes(array $names = null, array $except = []): array
$names = $this->attributes();
}

/** @psalm-var list<string> $names */
foreach ($names as $name) {
/** @psalm-var mixed */
$values[$name] = $this->$name;
if ($except !== []) {
$names = array_diff($names, $except);
}

/** @psalm-var list<string> $except */
foreach ($except as $name) {
unset($values[$name]);
foreach ($names as $name) {
$values[$name] = $this->$name;
}

return $values;
Expand Down
11 changes: 11 additions & 0 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,17 @@ public function testGetAttributes(): void
$this->assertEquals($attributes, $attributesExpected);
}

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

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

$attributes = $customer->findOne(1)->getAttributes(['id', 'email', 'name']);

$this->assertEquals(['id' => 1, 'email' => '[email protected]', 'name' => 'user1'], $attributes);
}

public function testGetAttributesExcept(): void
{
$this->checkFixture($this->db, 'customer');
Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/ActiveRecord/Cat.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getException(): void
*/
public function getThrowable(): float|int
{
return 5/0;
return 5 / 0;
}

public function setNonExistingProperty(string $value): void
Expand Down

0 comments on commit 5964f2a

Please sign in to comment.