|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FForattini\Btrieve\Persistence; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
class ActiveRecord |
|
8
|
|
|
{ |
|
9
|
|
|
private $columns; |
|
10
|
|
|
private $data; |
|
11
|
|
|
private $attributes; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Shorter way to create an ActiveRecord using an array of attributes. |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $attributes |
|
17
|
|
|
* |
|
18
|
|
|
* @return ActiveRecord |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function attributes($attributes = []) |
|
21
|
|
|
{ |
|
22
|
|
|
return (new static())->setAttributes($attributes); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $columns |
|
29
|
|
|
* @param array $data |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($columns = [], $data = []) |
|
32
|
|
|
{ |
|
33
|
|
|
if (count($columns) !== count($data)) { |
|
34
|
|
|
throw new InvalidArgumentException('Columns are required to have the same # of elements from data.'); |
|
35
|
|
|
} |
|
36
|
|
|
$this->setColumns($columns); |
|
37
|
|
|
$this->setData($data); |
|
38
|
|
|
if (!empty($columns)) { |
|
39
|
|
|
$this->setAttributes(array_combine($this->getColumns(), $this->getData())); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Set columns. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $columns |
|
47
|
|
|
* |
|
48
|
|
|
* @return ActiveRecord |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setColumns($columns) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->columns = $columns; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get columns. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getColumns() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->columns; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set correspondent data from columns order. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* |
|
72
|
|
|
* @return ActiveRecord |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setData($data) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->data = $data; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get correspondent data from columns order. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getData() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->data; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns combination array of attributes. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function toArray() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->getAttributes(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Defines columns and data from an array of attributes. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $attributes |
|
105
|
|
|
* |
|
106
|
|
|
* @return ActiveRecord |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setAttributes($attributes) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->attributes = $attributes; |
|
111
|
|
|
$this->setColumns(array_keys($attributes)); |
|
112
|
|
|
$this->setData(array_values($attributes)); |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get an array of attributes. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getAttributes() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->attributes; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Verify if columns exists. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $key |
|
131
|
|
|
* |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function has($key) |
|
135
|
|
|
{ |
|
136
|
|
|
if (isset($this->attributes[$key])) { |
|
137
|
|
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns attribute of the element. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $key |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
|
|
public function attribute($key) |
|
151
|
|
|
{ |
|
152
|
|
|
if (!$this->has($key)) { |
|
153
|
|
|
throw new InvalidArgumentException('Attribute \''.$key.'\' doesnt exists.'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $this->attributes[$key]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @aliasof attribute |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $key |
|
163
|
|
|
* |
|
164
|
|
|
* @return mixed |
|
165
|
|
|
*/ |
|
166
|
|
|
public function attr($key) |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->attribute($key); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Update attribute value. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $key |
|
175
|
|
|
* @param mixed $value |
|
176
|
|
|
*/ |
|
177
|
|
|
public function update($key, $value) |
|
178
|
|
|
{ |
|
179
|
|
|
if (!$this->has($key)) { |
|
180
|
|
|
throw new InvalidArgumentException('Attribute \''.$key.'\' doesnt exists.'); |
|
181
|
|
|
} |
|
182
|
|
|
$this->attributes[$key] = $value; |
|
183
|
|
|
$this->data[array_search($key, $this->getColumns())] = $value; |
|
184
|
|
|
|
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Set new attribute value. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $key |
|
192
|
|
|
* @param mixed $value |
|
193
|
|
|
*/ |
|
194
|
|
|
public function set($key, $value) |
|
195
|
|
|
{ |
|
196
|
|
|
if ($this->has($key)) { |
|
197
|
|
|
return $this->update($key, $value); |
|
198
|
|
|
} |
|
199
|
|
|
$this->attributes[$key] = $value; |
|
200
|
|
|
$this->columns[] = $key; |
|
201
|
|
|
$this->data[] = $value; |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|