Skip to content

Commit

Permalink
custom events for json behavior (#2123)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar authored Apr 27, 2022
1 parent 55569ff commit 35fa843
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
2 changes: 1 addition & 1 deletion core/base/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/base/DynamicModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
{
Expand Down
24 changes: 19 additions & 5 deletions core/behaviors/JsonBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 35fa843

Please sign in to comment.