1
|
|
|
<?php |
2
|
|
|
namespace Balloon\Manager; |
3
|
|
|
|
4
|
|
|
use Balloon\Reader\IFileReader; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FileManager |
8
|
|
|
* @package Balloon\Manager |
9
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class FileManager implements IFileManager |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var IFileReader |
15
|
|
|
*/ |
16
|
|
|
private $fileReader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $primaryKey; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param IFileReader $fileReader |
25
|
|
|
* @param string $primaryKey |
26
|
|
|
*/ |
27
|
|
|
public function __construct(IFileReader $fileReader, $primaryKey = '') |
28
|
|
|
{ |
29
|
|
|
$this->setFileReader($fileReader); |
30
|
|
|
$this->setPrimaryKey($primaryKey); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $id |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function has($id) |
38
|
|
|
{ |
39
|
|
|
return array_key_exists($id, $this->getAll()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $id |
44
|
|
|
* @return mixed|null |
45
|
|
|
*/ |
46
|
|
|
public function get($id) |
47
|
|
|
{ |
48
|
|
|
if($this->has($id)){ |
49
|
|
|
return $this->getAll()[$id]; |
50
|
|
|
} |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $ids |
56
|
|
|
* @return mixed |
|
|
|
|
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function getList(array $ids) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$all = $this->getAll(); |
61
|
|
|
$result = array_intersect_key((array)$all, $ids); |
62
|
|
|
if(is_object($all)){ |
63
|
|
|
return new $all($result); |
64
|
|
|
} |
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param callable $filter |
|
|
|
|
70
|
|
|
* @return mixed |
|
|
|
|
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function find(callable $filter = null) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$all = $this->getAll(); |
75
|
|
|
$result = array_filter((array)$all, $filter); |
76
|
|
|
if(is_object($all)){ |
77
|
|
|
return new $all($result); |
78
|
|
|
} |
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
|
|
|
|
84
|
|
|
*/ |
85
|
|
|
public function getAll() |
86
|
|
|
{ |
87
|
|
|
$result = $this->fileReader->read(); |
88
|
|
|
if(is_null($result)){ |
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
if(is_scalar($result)){ |
92
|
|
|
return (array)$result; |
93
|
|
|
} |
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param mixed $dataList |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function set($dataList) |
102
|
|
|
{ |
103
|
|
|
return $this->fileReader->write($dataList); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $data |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function add($data) |
111
|
|
|
{ |
112
|
|
|
$dataList = $this->getAll(); |
113
|
|
|
$key = $this->retrieveKey($data); |
114
|
|
|
if($key){ |
115
|
|
|
$dataList[$key] = $data; |
116
|
|
|
}else{ |
117
|
|
|
$dataList[] = $data; |
118
|
|
|
} |
119
|
|
|
return $this->set($dataList); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param mixed $data |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
private function retrieveKey($data) |
127
|
|
|
{ |
128
|
|
|
if($this->getPrimaryKey()){ |
129
|
|
|
$pk = $this->getPrimaryKey(); |
|
|
|
|
130
|
|
|
if(is_array($data)){ |
131
|
|
|
if(array_key_exists($pk, $data)) { |
132
|
|
|
return $data[$pk]; |
133
|
|
|
} |
134
|
|
|
}elseif(is_object($data)){ |
135
|
|
|
if(method_exists($data, 'get'.$pk)){ |
136
|
|
|
return $data->{'get'.$pk}(); |
137
|
|
|
} |
138
|
|
|
if(property_exists($data, $pk)) { |
139
|
|
|
return $data->{$pk}; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param mixed $dataList |
148
|
|
|
* @return int |
149
|
|
|
*/ |
150
|
|
|
public function addList($dataList) |
151
|
|
|
{ |
152
|
|
|
$result = 0; |
153
|
|
|
foreach($dataList as $data){ |
154
|
|
|
$result = $this->add($data); |
155
|
|
|
} |
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param mixed $id |
161
|
|
|
* @param mixed $data |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function modify($id, $data) |
165
|
|
|
{ |
166
|
|
|
$dataList = $this->getAll(); |
167
|
|
|
if(isset($dataList[$id])){ |
168
|
|
|
$dataList[$id] = $data; |
169
|
|
|
return $this->set($dataList); |
170
|
|
|
} |
171
|
|
|
return 0; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param mixed $dataList |
176
|
|
|
* @return int |
177
|
|
|
*/ |
178
|
|
|
public function modifyList($dataList) |
179
|
|
|
{ |
180
|
|
|
$result = 0; |
181
|
|
|
foreach($dataList as $id => $data){ |
182
|
|
|
$result = $this->modify($id, $data); |
183
|
|
|
} |
184
|
|
|
return $result; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param mixed $id |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function remove($id) |
192
|
|
|
{ |
193
|
|
|
$dataList = $this->getAll(); |
194
|
|
|
unset($dataList[$id]); |
195
|
|
|
return $this->set($dataList); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param array $ids |
200
|
|
|
* @return int |
201
|
|
|
*/ |
202
|
|
|
public function removeList(array $ids) |
203
|
|
|
{ |
204
|
|
|
$result = 0; |
205
|
|
|
foreach($ids as $id){ |
206
|
|
|
$result = $this->remove($id); |
207
|
|
|
} |
208
|
|
|
return $result; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return int |
213
|
|
|
*/ |
214
|
|
|
public function removeAll() |
215
|
|
|
{ |
216
|
|
|
return $this->set(null); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Getter of $fileReader |
221
|
|
|
* |
222
|
|
|
* @return IFileReader |
223
|
|
|
*/ |
224
|
|
|
protected function getFileReader() |
225
|
|
|
{ |
226
|
|
|
return $this->fileReader; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Getter of $primaryKey |
231
|
|
|
* |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
protected function getPrimaryKey() |
235
|
|
|
{ |
236
|
|
|
return $this->primaryKey; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Setter of $primaryKey |
241
|
|
|
* |
242
|
|
|
* @param string $primaryKey |
243
|
|
|
*/ |
244
|
|
|
private function setPrimaryKey($primaryKey) |
245
|
|
|
{ |
246
|
|
|
$this->primaryKey = (string)$primaryKey; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Setter of $fileReader |
251
|
|
|
* |
252
|
|
|
* @param IFileReader $fileReader |
253
|
|
|
*/ |
254
|
|
|
private function setFileReader(IFileReader $fileReader) |
255
|
|
|
{ |
256
|
|
|
$this->fileReader = $fileReader; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.