1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2017 Facebook, Inc. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
namespace Facebook\GraphNode; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Modified version of Collection in "illuminate/support" by Taylor Otwell. |
27
|
|
|
* |
28
|
|
|
* @package Facebook |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use ArrayAccess; |
32
|
|
|
use ArrayIterator; |
33
|
|
|
use Countable; |
34
|
|
|
use IteratorAggregate; |
35
|
|
|
|
36
|
|
|
class Collection implements ArrayAccess, Countable, IteratorAggregate |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The items contained in the collection. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $items = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new collection. |
47
|
|
|
* |
48
|
|
|
* @param array $items |
49
|
|
|
*/ |
50
|
|
|
public function __construct(array $items = []) |
51
|
|
|
{ |
52
|
|
|
$this->items = $items; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Gets the value of a field from the Graph node. |
57
|
|
|
* |
58
|
|
|
* @param string $name the field to retrieve |
59
|
|
|
* @param mixed $default the default to return if the field doesn't exist |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function getField($name, $default = null) |
64
|
|
|
{ |
65
|
|
|
if (isset($this->items[$name])) { |
66
|
|
|
return $this->items[$name]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $default; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns a list of all fields set on the object. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getFieldNames() |
78
|
|
|
{ |
79
|
|
|
return array_keys($this->items); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get all of the items in the collection. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function all() |
88
|
|
|
{ |
89
|
|
|
return $this->items; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the collection of items as a plain array. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function asArray() |
98
|
|
|
{ |
99
|
|
|
return array_map(function ($value) { |
100
|
|
|
return $value instanceof Collection ? $value->asArray() : $value; |
101
|
|
|
}, $this->items); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Run a map over each of the items. |
106
|
|
|
* |
107
|
|
|
* @param \Closure $callback |
108
|
|
|
* |
109
|
|
|
* @return static |
110
|
|
|
*/ |
111
|
|
|
public function map(\Closure $callback) |
112
|
|
|
{ |
113
|
|
|
return new static(array_map($callback, $this->items, array_keys($this->items))); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the collection of items as JSON. |
118
|
|
|
* |
119
|
|
|
* @param int $options |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function asJson($options = 0) |
124
|
|
|
{ |
125
|
|
|
return json_encode($this->asArray(), $options); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Count the number of items in the collection. |
130
|
|
|
* |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function count() |
134
|
|
|
{ |
135
|
|
|
return count($this->items); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get an iterator for the items. |
140
|
|
|
* |
141
|
|
|
* @return ArrayIterator |
142
|
|
|
*/ |
143
|
|
|
public function getIterator() |
144
|
|
|
{ |
145
|
|
|
return new ArrayIterator($this->items); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Determine if an item exists at an offset. |
150
|
|
|
* |
151
|
|
|
* @param mixed $key |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function offsetExists($key) |
156
|
|
|
{ |
157
|
|
|
return array_key_exists($key, $this->items); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get an item at a given offset. |
162
|
|
|
* |
163
|
|
|
* @param mixed $key |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
public function offsetGet($key) |
168
|
|
|
{ |
169
|
|
|
return $this->items[$key]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set the item at a given offset. |
174
|
|
|
* |
175
|
|
|
* @param mixed $key |
176
|
|
|
* @param mixed $value |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function offsetSet($key, $value) |
181
|
|
|
{ |
182
|
|
|
if (is_null($key)) { |
183
|
|
|
$this->items[] = $value; |
184
|
|
|
} else { |
185
|
|
|
$this->items[$key] = $value; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Unset the item at a given offset. |
191
|
|
|
* |
192
|
|
|
* @param string $key |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function offsetUnset($key) |
197
|
|
|
{ |
198
|
|
|
unset($this->items[$key]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Convert the collection to its string representation. |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function __toString() |
207
|
|
|
{ |
208
|
|
|
return $this->asJson(); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|