1
|
|
|
<?php |
2
|
|
|
namespace StarCitizen\Models; |
3
|
|
|
|
4
|
|
|
use ArrayAccess; |
5
|
|
|
use Countable; |
6
|
|
|
use IteratorAggregate; |
7
|
|
|
use ArrayIterator; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Store |
11
|
|
|
* |
12
|
|
|
* @package StarCitizen\Models; |
13
|
|
|
*/ |
14
|
|
|
class Store implements ArrayAccess, Countable, IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
protected $items = []; |
17
|
|
|
|
18
|
|
|
private $className; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Store constructor. |
22
|
|
|
* |
23
|
|
|
* @param array $data |
24
|
|
|
* @param string $className |
25
|
|
|
* @param string $dataRoot |
26
|
|
|
* @param string $idName |
27
|
|
|
*/ |
28
|
10 |
|
public function __construct(array $data, $className, $dataRoot, $idName) |
29
|
|
|
{ |
30
|
10 |
|
$this->className = 'StarCitizen\Models' . $className; |
31
|
10 |
|
foreach ($data as $item) { |
32
|
10 |
|
if ($dataRoot != '') |
33
|
9 |
|
$objectData = $item[$dataRoot]; |
34
|
|
|
else |
35
|
3 |
|
$objectData = $item; |
36
|
|
|
|
37
|
10 |
|
$storageObject = new \ReflectionClass('StarCitizen\Models' . $className); |
38
|
10 |
|
$this->offsetSet($objectData[$idName], $storageObject->newInstance($objectData)); |
39
|
|
|
} |
40
|
10 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ArrayIterator |
44
|
|
|
*/ |
45
|
9 |
|
public function getIterator() |
46
|
|
|
{ |
47
|
9 |
|
return new ArrayIterator($this->items); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $offset |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
5 |
|
public function offsetExists($offset) |
56
|
|
|
{ |
57
|
5 |
|
if (array_key_exists($offset, $this->items)) |
58
|
3 |
|
return true; |
59
|
|
|
|
60
|
2 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $offset |
65
|
|
|
* |
66
|
|
|
* @return bool|mixed |
67
|
|
|
*/ |
68
|
3 |
|
public function offsetGet($offset) |
69
|
|
|
{ |
70
|
3 |
|
if ($this->offsetExists($offset)) |
71
|
2 |
|
return $this->items[$offset]; |
72
|
|
|
|
73
|
1 |
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $offset |
78
|
|
|
* @param mixed $value |
79
|
|
|
*/ |
80
|
10 |
|
public function offsetSet($offset, $value) |
81
|
|
|
{ |
82
|
10 |
|
if ($value instanceof $this->className) |
83
|
10 |
|
$this->items[$offset] = $value; |
84
|
10 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $offset |
88
|
|
|
*/ |
89
|
1 |
|
public function offsetUnset($offset) |
90
|
|
|
{ |
91
|
1 |
|
if ($this->offsetExists($offset) && isset($this->items[$offset])) |
92
|
|
|
unset ($this->items[$offset]); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
3 |
|
public function count() |
99
|
|
|
{ |
100
|
3 |
|
return count($this->items); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $name |
105
|
|
|
* |
106
|
|
|
* @return bool|mixed |
107
|
|
|
*/ |
108
|
3 |
|
public function __get($name) |
109
|
|
|
{ |
110
|
3 |
|
return $this->offsetGet($name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $name |
115
|
|
|
* @param $value |
116
|
|
|
*/ |
117
|
2 |
|
public function __set($name, $value) |
118
|
|
|
{ |
119
|
2 |
|
$this->offsetSet($name, $value); |
120
|
|
|
} |
121
|
|
|
} |