1
|
|
|
<?php namespace Syntax\SteamApi; |
2
|
|
|
|
3
|
|
|
class Collection extends \Illuminate\Database\Eloquent\Collection { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Dynamically retrieve attributes on the model. |
7
|
|
|
* |
8
|
|
|
* @param string $key |
9
|
|
|
* |
10
|
|
|
* @return Collection |
11
|
|
|
*/ |
12
|
|
|
public function __get($key) |
13
|
|
|
{ |
14
|
|
|
$newCollection = new Collection(); |
15
|
|
|
foreach ($this->items as $item) { |
16
|
|
|
if ($item instanceof Collection) { |
17
|
|
|
foreach ($item as $subItem) { |
18
|
|
|
$newCollection->put($newCollection->count(), $subItem->$key); |
19
|
|
|
} |
20
|
|
|
} elseif (is_object($item) && ! $item instanceof Collection && $item->$key instanceof Collection) { |
21
|
|
|
foreach ($item->$key as $subItem) { |
22
|
|
|
$newCollection->put($newCollection->count(), $subItem); |
23
|
|
|
} |
24
|
|
|
} else { |
25
|
|
|
$newCollection->put($newCollection->count(), $item->$key); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $newCollection; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Allow a method to be run on the enitre collection. |
34
|
|
|
* |
35
|
|
|
* @param string $method |
36
|
|
|
* @param array $args |
37
|
|
|
* |
38
|
|
|
* @return Collection |
39
|
|
|
*/ |
40
|
|
|
public function __call($method, $args) |
41
|
|
|
{ |
42
|
|
|
if ($this->count() <= 0) { |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach ($this->items as $item) { |
47
|
|
|
if (! is_object($item)) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
call_user_func_array([$item, $method], $args); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Insert into an object |
58
|
|
|
* |
59
|
|
|
* @param mixed $value |
60
|
|
|
* @param int $afterKey |
61
|
|
|
* |
62
|
|
|
* @return Collection |
63
|
|
|
*/ |
64
|
|
|
public function insertAfter($value, $afterKey) |
65
|
|
|
{ |
66
|
|
|
$new_object = new Collection(); |
67
|
|
|
|
68
|
|
|
foreach ((array)$this->items as $k => $v) { |
69
|
|
|
if ($afterKey == $k) { |
70
|
|
|
$new_object->add($value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$new_object->add($v); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->items = $new_object->items; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Find item in a collection |
83
|
|
|
* |
84
|
|
|
* @param string $column |
85
|
|
|
* @param string|null $value |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function where($column, $value = null) |
90
|
|
|
{ |
91
|
|
|
foreach ($this->items as $key => $item) { |
92
|
|
|
|
93
|
|
|
if (strstr($column, '->')) { |
94
|
|
|
if ($this->handleMultiTap($key, $item, $column, $value)) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
if ($this->handleSingle($key, $item, $column, $value)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $key |
109
|
|
|
* @param $item |
110
|
|
|
* @param $column |
111
|
|
|
* @param $value |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
private function handleMultiTap($key, $item, $column, $value) |
116
|
|
|
{ |
117
|
|
|
$objectToSearch = $this->tapThroughObjects($column, $item); |
118
|
|
|
|
119
|
|
|
if ($this->removeCollectionItem($key, $value, $objectToSearch)) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->removeItem($key, $value, $objectToSearch)) { |
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $key |
132
|
|
|
* @param $item |
133
|
|
|
* @param $column |
134
|
|
|
* @param $value |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
private function handleSingle($key, $item, $column, $value) |
139
|
|
|
{ |
140
|
|
|
if (! $item->$column) { |
141
|
|
|
$this->forget($key); |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($this->removeItem($key, $value, $item->$column)) { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $column |
154
|
|
|
* @param $item |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
private function tapThroughObjects($column, $item) |
159
|
|
|
{ |
160
|
|
|
$taps = explode('->', $column); |
161
|
|
|
|
162
|
|
|
$objectToSearch = $item; |
163
|
|
|
foreach ($taps as $tapKey => $tap) { |
164
|
|
|
|
165
|
|
|
// Keep tapping till we hit the last object. |
166
|
|
|
$objectToSearch = $objectToSearch->$tap; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $objectToSearch; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $key |
174
|
|
|
* @param $value |
175
|
|
|
* @param $objectToSearch |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
private function removeCollectionItem($key, $value, $objectToSearch) |
180
|
|
|
{ |
181
|
|
|
if ($objectToSearch instanceof Collection) { |
182
|
|
|
if (! in_array($value, $objectToSearch->toArray())) { |
183
|
|
|
$this->forget($key); |
184
|
|
|
return true; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param $key |
193
|
|
|
* @param $value |
194
|
|
|
* @param $objectToSearch |
195
|
|
|
* |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
private function removeItem($key, $value, $objectToSearch) |
199
|
|
|
{ |
200
|
|
|
if ($objectToSearch != $value) { |
201
|
|
|
$this->forget($key); |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
} |