|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\GridField; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Simple set of data, similar to stdClass, but without the notice-level |
|
7
|
|
|
* errors. |
|
8
|
|
|
* |
|
9
|
|
|
* @see GridState |
|
10
|
|
|
*/ |
|
11
|
|
|
class GridState_Data |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $data; |
|
18
|
|
|
|
|
19
|
|
|
protected $defaults = []; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($data = []) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->data = $data; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function __get($name) |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->getData($name, new self()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function __call($name, $arguments) |
|
32
|
|
|
{ |
|
33
|
|
|
// Assume first parameter is default value |
|
34
|
|
|
if (empty($arguments)) { |
|
35
|
|
|
$default = new self(); |
|
36
|
|
|
} else { |
|
37
|
|
|
$default = $arguments[0]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->getData($name, $default); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Initialise the defaults values for the grid field state |
|
45
|
|
|
* These values won't be included in getChangesArray() |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $defaults |
|
48
|
|
|
*/ |
|
49
|
|
|
public function initDefaults(array $defaults): void |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($defaults as $key => $value) { |
|
52
|
|
|
$this->defaults[$key] = $value; |
|
53
|
|
|
$this->getData($key, $value); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Retrieve the value for the given key |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name The name of the value to retrieve |
|
61
|
|
|
* @param mixed $default Default value to assign if not set. Note that this *will* be included in getChangesArray() |
|
62
|
|
|
* @return mixed The value associated with this key, or the value specified by $default if not set |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getData($name, $default = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!array_key_exists($name, $this->data)) { |
|
67
|
|
|
$this->data[$name] = $default; |
|
68
|
|
|
} else { |
|
69
|
|
|
if (is_array($this->data[$name])) { |
|
70
|
|
|
$this->data[$name] = new self($this->data[$name]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->data[$name]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function __set($name, $value) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->data[$name] = $value; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function __isset($name) |
|
83
|
|
|
{ |
|
84
|
|
|
return isset($this->data[$name]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function __unset($name) |
|
88
|
|
|
{ |
|
89
|
|
|
unset($this->data[$name]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function __toString() |
|
93
|
|
|
{ |
|
94
|
|
|
if (!$this->data) { |
|
|
|
|
|
|
95
|
|
|
return ""; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return json_encode($this->toArray()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return all data, including defaults, as array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function toArray() |
|
105
|
|
|
{ |
|
106
|
|
|
$output = []; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($this->data as $k => $v) { |
|
109
|
|
|
$output[$k] = (is_object($v) && method_exists($v, 'toArray')) ? $v->toArray() : $v; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $output; |
|
113
|
|
|
} |
|
114
|
|
|
/** |
|
115
|
|
|
* Convert the state to an array including only value that differ from the default state defined by initDefaults() |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getChangesArray(): array |
|
119
|
|
|
{ |
|
120
|
|
|
$output = []; |
|
121
|
|
|
|
|
122
|
|
|
foreach ($this->data as $k => $v) { |
|
123
|
|
|
if (is_object($v) && method_exists($v, 'getChangesArray')) { |
|
124
|
|
|
$value = $v->getChangesArray(); |
|
125
|
|
|
// Empty arrays represent pristine data, so we do not include them |
|
126
|
|
|
if (empty($value)) { |
|
127
|
|
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
} else { |
|
130
|
|
|
$value = $v; |
|
131
|
|
|
// Check if we have a default value for this key and if it matches our current value |
|
132
|
|
|
if (array_key_exists($k, $this->defaults) && $this->defaults[$k] === $value) { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$output[$k] = $value; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $output; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.