From 002c3563f99974d4d327cab86a9070b205d76f5a Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 12 Jan 2024 09:32:20 +0700 Subject: [PATCH] Improve `BaseActiveRecord::getAttributes()` --- src/BaseActiveRecord.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 485bc7ccf..64e3406f5 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -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 @@ -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;