1
|
|
|
<?php |
2
|
|
|
namespace paulzi\jsonBehavior; |
3
|
|
|
|
4
|
|
|
use yii\base\Behavior; |
5
|
|
|
use yii\db\ActiveRecord; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property ActiveRecord $owner |
9
|
|
|
*/ |
10
|
|
|
class JsonBehavior extends Behavior |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
public $attributes = []; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
|
public function events() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
ActiveRecord::EVENT_INIT => function () { $this->initialization(); }, |
25
|
|
|
ActiveRecord::EVENT_AFTER_FIND => function () { $this->decode(); }, |
26
|
|
|
ActiveRecord::EVENT_BEFORE_VALIDATE => function () { $this->encodeValidate(); }, |
27
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => function () { $this->encode(); }, |
28
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => function () { $this->encode(); }, |
29
|
|
|
ActiveRecord::EVENT_AFTER_VALIDATE => function () { $this->decode(); }, |
30
|
5 |
|
ActiveRecord::EVENT_AFTER_INSERT => function () { $this->decode(); }, |
31
|
|
|
ActiveRecord::EVENT_AFTER_UPDATE => function () { $this->decode(); }, |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
5 |
|
/** |
36
|
|
|
*/ |
37
|
5 |
|
protected function initialization() |
38
|
5 |
|
{ |
39
|
5 |
|
foreach ($this->attributes as $attribute) { |
40
|
5 |
|
$this->owner->setAttribute($attribute, new JsonField()); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
/** |
45
|
|
|
*/ |
46
|
4 |
View Code Duplication |
protected function decode() |
|
|
|
|
47
|
4 |
|
{ |
48
|
4 |
|
foreach ($this->attributes as $attribute) { |
49
|
4 |
|
$value = $this->owner->getAttribute($attribute); |
50
|
4 |
|
if (!$value instanceof JsonField) { |
51
|
4 |
|
$value = new JsonField($value); |
52
|
4 |
|
} |
53
|
4 |
|
$this->owner->setAttribute($attribute, $value); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
/** |
58
|
|
|
*/ |
59
|
4 |
View Code Duplication |
protected function encode() |
|
|
|
|
60
|
4 |
|
{ |
61
|
4 |
|
foreach ($this->attributes as $attribute) { |
62
|
|
|
$field = $this->owner->getAttribute($attribute); |
63
|
|
|
if (!$field instanceof JsonField) { |
64
|
4 |
|
$field = new JsonField($field); |
65
|
4 |
|
} |
66
|
4 |
|
$this->owner->setAttribute($attribute, (string)$field ?: null); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
protected function encodeValidate() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
foreach ($this->attributes as $attribute) { |
75
|
|
|
$field = $this->owner->getAttribute($attribute); |
76
|
|
|
if ($field instanceof JsonField) { |
77
|
|
|
$this->owner->setAttribute($attribute, (string)$field ?: null); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.