Skip to content

Commit

Permalink
Improve BaseActiveRecord::getAttributes()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 12, 2024
1 parent 4f9a24a commit 002c356
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,14 @@ public function getAttribute(string $name): mixed

public function getAttributes(array $names = null, array $except = []): array
{
$values = [];

if ($names === null) {
$names = $this->attributes();
}
$names ??= $this->attributes();
$attributes = array_merge($this->attributes, get_object_vars($this));

if ($except !== []) {
$names = array_diff($names, $except);
}

foreach ($names as $name) {
$values[$name] = $this->$name;
}

return $values;
return array_intersect_key($attributes, array_flip($names));
}

public function getIsNewRecord(): bool
Expand Down Expand Up @@ -190,8 +183,7 @@ public function getOldAttribute(string $name): mixed
*/
public function getDirtyAttributes(array $names = null): array
{
$attributes = array_merge($this->attributes, get_object_vars($this));
$attributes = array_intersect_key($attributes, array_flip($names ?? $this->attributes()));
$attributes = $this->getAttributes($names);

if ($this->oldAttributes === null) {
return $attributes;
Expand Down

0 comments on commit 002c356

Please sign in to comment.