Skip to content

Commit

Permalink
invert json behavior (#2008)
Browse files Browse the repository at this point in the history
* invert json behavior

* change and test update

* version
  • Loading branch information
nadar authored Apr 7, 2020
1 parent c1ef0f1 commit 9983e6e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
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).

## 1.2.1 (7. April 2020)

+ [#2008](https://github.com/luyadev/luya/pull/2008) Default values for JsonBehavior was wrong declared, expected is the opposite way 1. array as validation rule, 2. auto decode data after find.

## 1.2.0 (7. April 2020)

+ [#2003](https://github.com/luyadev/luya/pull/2003) Add new `StringHelper::isNumeric()` which checks whether a value is nummeric (with regex instead of `is_numeric`) or not.
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 = '1.2.0';
const VERSION = '1.2.1';

/**
* @var string The path to the config file, which returns an array containing you configuration.
Expand Down
20 changes: 12 additions & 8 deletions core/behaviors/JsonBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use luya\helpers\Json;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use yii\db\BaseActiveRecord;

/**
* Json Behavior.
*
* Provides auto encoding for array values after validation in order to store in the database.
* Therefore the JsonBehavior default confiugration let you seamless work with expecting array values, array input
* and array output. Provides auto encoding for array values after validation in order to store in the database.
*
* In order to work only with arrayable json values use:
* An example of how to declared, validate and assign json values (as array):
*
* ```php
* public function behaviors()
Expand All @@ -21,8 +21,6 @@
* 'json' => [
* 'class' => JsonBehavior:class,
* 'attributes' => ['json_field'],
* 'encodeBeforeValidate' => true,
* 'decodeAfterFind' => true,
* ]
* ];
* }
Expand All @@ -34,6 +32,12 @@
* ]
* }
* ```
*
* The above mode assumes an array is passed to the model:
*
* ```php
* $model->json_field = ['foo' => 'bar'];
* ```
*
* @author Basil Suter <[email protected]>
* @since 1.0.9
Expand All @@ -43,17 +47,17 @@ class JsonBehavior extends Behavior
public $attributes = [];

/**
* @var boolean If enabled, the data will be encoded before validating, this means the validation rule should be `array` otherwise the validation must be `string`.
* @var boolean If enabled, the data will be encoded before validating, this means the validation rule should be `string` otherwise the validation must be `array`.
* This might also differ based on how the data is passed to the model. Data passed to the model will be encoded from string to array (if not already).
* @since 1.2.0
*/
public $encodeBeforeValidate = true;
public $encodeBeforeValidate = false;

/**
* @var boolean If enabled the data will be encoded from json to array after populating the active record data.
* @since 1.2.0
*/
public $decodeAfterFind = false;
public $decodeAfterFind = true;

/**
* @inheritdoc
Expand Down
19 changes: 18 additions & 1 deletion tests/core/behaviors/JsonBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JsonBehaviorTest extends LuyaWebTestCase
public function testEncodingDecodingOfAttributes()
{
$model = new DynamicModel(['json' => null]);
$model->attachBehavior('jsonBehavior', ['class' => JsonBehavior::class, 'attributes' => ['json'], 'decodeAfterFind' => true]);
$model->attachBehavior('jsonBehavior', ['class' => JsonBehavior::class, 'attributes' => ['json'], 'decodeAfterFind' => true, 'encodeBeforeValidate' => true]);

$model->addRule('json', 'string');

Expand Down Expand Up @@ -69,4 +69,21 @@ public function testOfAttributesAlreadyArray()
$this->assertSame('[1,2]', $model->jsonEncode('[1,2]'));

}

public function testExpectedDefaultBehavior()
{
$model = new DynamicModel(['json' => null]);
$model->attachBehavior('jsonBehavior', ['class' => JsonBehavior::class, 'attributes' => ['json']]);
$model->addRule('json', 'each', ['rule' => ['safe']]);

$model->json = [1,2,3];

$this->assertTrue($model->validate());

$this->assertSame('[1,2,3]', $model->json);

$model->trigger(ActiveRecord::EVENT_AFTER_FIND);

$this->assertSame([1,2,3], $model->json);
}
}

0 comments on commit 9983e6e

Please sign in to comment.