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 */
24namespace Facebook\GraphNodes;
25
26/**
27 * Class Collection
28 *
29 * Modified version of Collection in "illuminate/support" by Taylor Otwell
30 *
31 * @package Facebook
32 */
33
34use ArrayAccess;
35use ArrayIterator;
36use Countable;
37use IteratorAggregate;
38
39class Collection implements ArrayAccess, Countable, IteratorAggregate
40{
41    /**
42     * The items contained in the collection.
43     *
44     * @var array
45     */
46    protected $items = [];
47
48    /**
49     * Create a new collection.
50     *
51     * @param array $items
52     */
53    public function __construct(array $items = [])
54    {
55        $this->items = $items;
56    }
57
58    /**
59     * Gets the value of a field from the Graph node.
60     *
61     * @param string $name    The field to retrieve.
62     * @param mixed  $default The default to return if the field doesn't exist.
63     *
64     * @return mixed
65     */
66    public function getField($name, $default = null)
67    {
68        if (isset($this->items[$name])) {
69            return $this->items[$name];
70        }
71
72        return $default;
73    }
74
75    /**
76     * Gets the value of the named property for this graph object.
77     *
78     * @param string $name    The property to retrieve.
79     * @param mixed  $default The default to return if the property doesn't exist.
80     *
81     * @return mixed
82     *
83     * @deprecated 5.0.0 getProperty() has been renamed to getField()
84     * @todo v6: Remove this method
85     */
86    public function getProperty($name, $default = null)
87    {
88        return $this->getField($name, $default);
89    }
90
91    /**
92     * Returns a list of all fields set on the object.
93     *
94     * @return array
95     */
96    public function getFieldNames()
97    {
98        return array_keys($this->items);
99    }
100
101    /**
102     * Returns a list of all properties set on the object.
103     *
104     * @return array
105     *
106     * @deprecated 5.0.0 getPropertyNames() has been renamed to getFieldNames()
107     * @todo v6: Remove this method
108     */
109    public function getPropertyNames()
110    {
111        return $this->getFieldNames();
112    }
113
114    /**
115     * Get all of the items in the collection.
116     *
117     * @return array
118     */
119    public function all()
120    {
121        return $this->items;
122    }
123
124    /**
125     * Get the collection of items as a plain array.
126     *
127     * @return array
128     */
129    public function asArray()
130    {
131        return array_map(function ($value) {
132            return $value instanceof Collection ? $value->asArray() : $value;
133        }, $this->items);
134    }
135
136    /**
137     * Run a map over each of the items.
138     *
139     * @param \Closure $callback
140     *
141     * @return static
142     */
143    public function map(\Closure $callback)
144    {
145        return new static(array_map($callback, $this->items, array_keys($this->items)));
146    }
147
148    /**
149     * Get the collection of items as JSON.
150     *
151     * @param int $options
152     *
153     * @return string
154     */
155    public function asJson($options = 0)
156    {
157        return json_encode($this->asArray(), $options);
158    }
159
160    /**
161     * Count the number of items in the collection.
162     *
163     * @return int
164     */
165    public function count()
166    {
167        return count($this->items);
168    }
169
170    /**
171     * Get an iterator for the items.
172     *
173     * @return ArrayIterator
174     */
175    public function getIterator()
176    {
177        return new ArrayIterator($this->items);
178    }
179
180    /**
181     * Determine if an item exists at an offset.
182     *
183     * @param mixed $key
184     *
185     * @return bool
186     */
187    public function offsetExists($key)
188    {
189        return array_key_exists($key, $this->items);
190    }
191
192    /**
193     * Get an item at a given offset.
194     *
195     * @param mixed $key
196     *
197     * @return mixed
198     */
199    public function offsetGet($key)
200    {
201        return $this->items[$key];
202    }
203
204    /**
205     * Set the item at a given offset.
206     *
207     * @param mixed $key
208     * @param mixed $value
209     *
210     * @return void
211     */
212    public function offsetSet($key, $value)
213    {
214        if (is_null($key)) {
215            $this->items[] = $value;
216        } else {
217            $this->items[$key] = $value;
218        }
219    }
220
221    /**
222     * Unset the item at a given offset.
223     *
224     * @param string $key
225     *
226     * @return void
227     */
228    public function offsetUnset($key)
229    {
230        unset($this->items[$key]);
231    }
232
233    /**
234     * Convert the collection to its string representation.
235     *
236     * @return string
237     */
238    public function __toString()
239    {
240        return $this->asJson();
241    }
242}
243