From 35fa843fccb2ecf9c120adfd95e7181ca3e575fa Mon Sep 17 00:00:00 2001 From: Basil Date: Wed, 27 Apr 2022 11:50:00 +0200 Subject: [PATCH] custom events for json behavior (#2123) --- core/CHANGELOG.md | 2 +- core/base/Boot.php | 2 +- core/base/DynamicModel.php | 4 ++-- core/behaviors/JsonBehavior.php | 24 +++++++++++++++++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 57160da9f..504a62eda 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md). -## 2.0.3 +## 2.1.0 + [#2118](https://github.com/luyadev/luya/pull/2118) Removed conflicting `$attributeLabels` property from DynamicModel (Yii provides this option since 2.0.35, therefore not required by LUYA anymore). diff --git a/core/base/Boot.php b/core/base/Boot.php index a48ceabeb..7dad9fcb0 100644 --- a/core/base/Boot.php +++ b/core/base/Boot.php @@ -24,7 +24,7 @@ abstract class Boot /** * @var string The current LUYA version (see: https://github.com/luyadev/luya/blob/master/core/CHANGELOG.md) */ - const VERSION = '2.0.2'; + const VERSION = '2.1.0'; /** * @var string The path to the config file, which returns an array containing you configuration. diff --git a/core/base/DynamicModel.php b/core/base/DynamicModel.php index 1165579f8..057e6bb5e 100644 --- a/core/base/DynamicModel.php +++ b/core/base/DynamicModel.php @@ -32,7 +32,7 @@ class DynamicModel extends \yii\base\DynamicModel * * @param array $hints Array of attribute hints * @return $this - * @since 2.0.3 + * @since 2.1.0 */ public function setAttributeHints(array $hints) { @@ -44,7 +44,7 @@ public function setAttributeHints(array $hints) * Get all hints for backwards compatibility. * * @return array - * @since 2.0.3 + * @since 2.1.0 */ public function getAttributeHints() { diff --git a/core/behaviors/JsonBehavior.php b/core/behaviors/JsonBehavior.php index 95a310f7f..830e04e1e 100644 --- a/core/behaviors/JsonBehavior.php +++ b/core/behaviors/JsonBehavior.php @@ -59,23 +59,37 @@ class JsonBehavior extends Behavior */ public $decodeAfterFind = true; + /** + * @var array An Array with all events which should be attached. Based on {{$encodeBeforeValidate}} and {{$decodeAfterFind}} the array will automatically receive certain events. The main goal for this property is + * to have the option to attach events by yourself. For example it could be useful to decode values on ngrest find. + * + * ```php + * [ + * 'class' => JsonBehavior::class, + * 'events' => [NgRestModel::EVENT_AFTER_NGREST_FIND => 'decodeAttributes'] + * 'attributes' => ['my_super_json_attribute'], + * ] + * ``` + * @since 2.1.0 + */ + public $events = []; + /** * @inheritdoc */ public function events() { - $events = []; if ($this->encodeBeforeValidate) { - $events[ActiveRecord::EVENT_BEFORE_VALIDATE] = 'encodeAttributes'; + $this->events[ActiveRecord::EVENT_BEFORE_VALIDATE] = 'encodeAttributes'; } else { - $events[ActiveRecord::EVENT_AFTER_VALIDATE] = 'encodeAttributes'; + $this->events[ActiveRecord::EVENT_AFTER_VALIDATE] = 'encodeAttributes'; } if ($this->decodeAfterFind) { - $events[ActiveRecord::EVENT_AFTER_FIND] = 'decodeAttributes'; + $this->events[ActiveRecord::EVENT_AFTER_FIND] = 'decodeAttributes'; } - return $events; + return $this->events; } /**