Collection::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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