From 21fde080c09da877338a58effbf34449464dd216 Mon Sep 17 00:00:00 2001 From: Oleg Fedorov Date: Tue, 11 Oct 2016 13:45:13 +0300 Subject: [PATCH] Implement `ActiveRecord::populateRecord()` method. Add exception throwing during model population, that occurs when there was non-existent attribute found, declared in child class `attributes()` method. --- src/ActiveRecord.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 1c660b3..4220f98 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -14,6 +14,7 @@ use GuzzleHttp\Exception\ClientException; use yii\base\InvalidConfigException; use yii\base\NotSupportedException; +use yii\base\UnknownPropertyException; use yii\db\BaseActiveRecord; use yii\helpers\Inflector; use yii\helpers\StringHelper; @@ -235,4 +236,19 @@ public function unlinkAll($name, $delete = false) { throw new NotSupportedException('unlinkAll() is not supported by RestClient, use unlink() instead.'); } + + /** + * @inheritdoc + * @throws \yii\base\UnknownPropertyException + */ + public static function populateRecord($record, $row) + { + $attributes = array_flip($record->attributes()); + foreach ($attributes as $attributeName => $attributeValue) { + if (!array_key_exists($attributeName, $row)) { + throw new UnknownPropertyException("Attribute `{$attributeName}` not found in API response. Available fields: " . implode(', ', array_keys($row)) . '.'); + } + } + parent::populateRecord($record, $row); + } }