1
|
|
|
<?php |
2
|
|
|
namespace paulzi\jsonBehavior; |
3
|
|
|
|
4
|
|
|
use yii\base\Arrayable; |
5
|
|
|
use yii\base\InvalidParamException; |
6
|
|
|
use yii\helpers\Json; |
7
|
|
|
|
8
|
|
|
class JsonField implements \ArrayAccess, Arrayable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $value; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string|array $value |
18
|
|
|
*/ |
19
|
29 |
|
public function __construct($value = []) |
20
|
|
|
{ |
21
|
29 |
|
$this->set($value); |
22
|
22 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
13 |
|
public function __toString() |
28
|
|
|
{ |
29
|
13 |
|
return $this->value ? Json::encode($this->value) : ''; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|array $value |
34
|
|
|
*/ |
35
|
29 |
|
public function set($value) |
36
|
|
|
{ |
37
|
29 |
|
if ($value === null || $value === '') { |
38
|
4 |
|
$value = []; |
39
|
29 |
|
} elseif (is_string($value)) { |
40
|
24 |
|
$value = Json::decode($value, true); |
41
|
22 |
|
if (!is_array($value)) { |
42
|
9 |
|
throw new InvalidParamException('Value is scalar'); |
43
|
|
|
} |
44
|
14 |
|
} |
45
|
24 |
|
if (!is_array($value)) { |
46
|
2 |
|
throw new InvalidParamException('Value is not array'); |
47
|
|
|
} else { |
48
|
22 |
|
$this->value = $value; |
49
|
|
|
} |
50
|
22 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritdoc |
54
|
|
|
*/ |
55
|
1 |
|
public function fields() |
56
|
|
|
{ |
57
|
1 |
|
$fields = array_keys($this->value); |
58
|
1 |
|
return array_combine($fields, $fields); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function extraFields() |
65
|
|
|
{ |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
4 |
|
public function toArray(array $fields = [], array $expand = [], $recursive = true) |
73
|
|
|
{ |
74
|
4 |
|
return empty($fields) ? $this->value : array_intersect_key($this->value, array_flip($fields)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
1 |
|
public function isEmpty() |
81
|
|
|
{ |
82
|
1 |
|
return !$this->value; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
|
|
public function offsetExists($offset) |
89
|
|
|
{ |
90
|
|
|
return isset($this->value[$offset]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
3 |
View Code Duplication |
public function &offsetGet($offset) |
|
|
|
|
97
|
|
|
{ |
98
|
3 |
|
$null = null; |
99
|
3 |
|
if (isset($this->value[$offset])) { |
100
|
3 |
|
return $this->value[$offset]; |
101
|
|
|
} else { |
102
|
1 |
|
return $null; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
7 |
View Code Duplication |
public function offsetSet($offset, $value) |
|
|
|
|
110
|
|
|
{ |
111
|
7 |
|
if ($offset === null) { |
112
|
|
|
$this->value[] = $value; |
113
|
|
|
} else { |
114
|
7 |
|
$this->value[$offset] = $value; |
115
|
|
|
} |
116
|
7 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function offsetUnset($offset) |
122
|
|
|
{ |
123
|
|
|
unset($this->value[$offset]); |
124
|
|
|
} |
125
|
|
|
} |
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.