GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#118)
by
unknown
02:51
created

Collection::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Copyright 2015 Dirk Groenen
4
 *
5
 * (c) Dirk Groenen <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace DirkGroenen\Pinterest\Models;
12
13
use DirkGroenen\Pinterest\Exceptions\PinterestException;
14
15
use \DirkGroenen\Pinterest\Pinterest;
16
use \DirkGroenen\Pinterest\Transport\Response;
17
18
class Collection implements \JsonSerializable, \ArrayAccess, \IteratorAggregate{
19
20
    /**
21
     * The items in the collection
22
     *
23
     * @var array
24
     */
25
    private $items = [];
26
27
    /**
28
     * The model of each collection item
29
     *
30
     * @var string
31
     */
32
    private $model;
33
34
    /**
35
     * Stores the pagination object
36
     *
37
     * @var array|boolean
38
     */
39
    public $pagination;
40
41
    /**
42
     * Instance of Pinterest master class
43
     *
44
     * @var Pinterest
45
     */
46
    private $master;
47
48
    /**
49
     * Response instance
50
     *
51
     * @var Response
52
     */
53
    private $response;
54
55
    /**
56
     * Construct
57
     *
58
     * @access public
59
     * @param  Pinterest            $master
60
     * @param  array|Response       $items
61
     * @param  string               $model
62
     * @throws InvalidModelException
63
     */
64 19
    public function __construct(Pinterest $master, $items, $model) {
65 19
        $this->master = $master;
66
67
        // Create class path
68 19
        $this->model = ucfirst(strtolower($model));
69
70 19
        if (!class_exists("\\DirkGroenen\\Pinterest\\Models\\" . $this->model)) {
71
            throw new InvalidModelException;
72
        }
73
74
        // Get items and response instance
75 19
        if (is_array($items)) {
76 1
            $this->response = null;
77 1
            $this->items = $items;
78 18
        } else if ($items instanceof \DirkGroenen\Pinterest\Transport\Response) {
79 18
            $this->response = $items;
80 18
            $this->items = $items->data;
81
        } else {
82
            throw new PinterestException("$items needs to be an instance of Transport\Response or an array.");
83
        }
84
85
        // Transform the raw collection data to models
86 19
        $this->items = $this->buildCollectionModels($this->items);
87
88
        // Add pagination object
89 19
        if (isset($this->response->page) && !empty($this->response->page['next'])) {
90 9
            $this->pagination = $this->response->page;
91
        } else {
92 10
            $this->pagination = false;
93
        }
94 19
    }
95
96
    /**
97
     * Get all items from the collection
98
     *
99
     * @access public
100
     * @return array
101
     */
102 1
    public function all()
103
    {
104 1
        return $this->items;
105
    }
106
107
    /**
108
     * Transform each raw item into a model
109
     *
110
     * @access private
111
     * @param array $items
112
     * @return array
113
     */
114 19
    private function buildCollectionModels(array $items)
115
    {
116 19
        $modelcollection = [];
117
118 19
        foreach ($items as $item) {
119 19
            $class = new \ReflectionClass("\\DirkGroenen\\Pinterest\\Models\\" . $this->model);
120 19
            $modelcollection[] = $class->newInstanceArgs([$this->master, $item]);
121
        }
122
123 19
        return $modelcollection;
124
    }
125
126
    /**
127
     * Check if their is a next page available
128
     *
129
     * @access public
130
     * @return boolean
131
     */
132 1
    public function hasNextPage()
133
    {
134 1
        return ($this->response != null && isset($this->response->page['next']));
135
    }
136
137
    public function nextPage()
138
    {
139
        if ($this->hasNextPage()) {
140
            $response = $this->master->request->execute('GET', $this->response->page['next']);
141
            return new self($this->master, $response, $this->model);
142
        }
143
    }
144 15
145
    /**
146 15
     * Return the item at the given index
147
     *
148
     * @access public
149
     * @param  int $index
150
     * @return Model
151
     */
152
    public function get($index)
153
    {
154
        return $this->items[$index];
155 2
    }
156
157 2
    /**
158
     * Convert the collection to an array
159 2
     *
160 2
     * @access public
161
     * @return array
162
     */
163
    public function toArray()
164 2
    {
165 2
        $items = [];
166
167
        foreach ($this->items as $item) {
168
            $items[] = $item->toArray();
169
        }
170
171
        return array(
172
            "data" => $items,
173
            "page"  => $this->pagination
174
        );
175 1
    }
176
177 1
    /**
178
     * Convert the collection to JSON
179
     *
180
     * @access public
181
     * @return string
182
     */
183
    public function toJson()
184
    {
185
        return json_encode($this->toArray(), true);
186
    }
187
188
    /**
189
     * Convert the object into something JSON serializable.
190
     *
191
     * @access public
192
     * @return array
193
     */
194
    public function jsonSerialize()
195
    {
196
        return $this->toArray();
197
    }
198
199
    /**
200
     * Convert the collection to its string representation
201
     *
202
     * @access public
203
     * @return string
204
     */
205
    public function __toString()
206
    {
207
        return $this->toJson();
208
    }
209
210
    /**
211
     * Determine if the given item exists.
212
     *
213
     * @access public
214
     * @param  mixed  $offset
215
     * @return bool
216
     */
217
    public function offsetExists($offset)
218
    {
219
        return isset($this->items[$offset]);
220
    }
221
    /**
222
     * Get the value for a given offset.
223
     *
224
     * @access public
225
     * @param  mixed  $offset
226
     * @return mixed
227
     */
228
    public function offsetGet($offset)
229
    {
230
        return $this->items[$offset];
231
    }
232
    /**
233
     * Set the value for a given offset.
234
     *
235
     * @access public
236
     * @param  mixed  $offset
237
     * @param  mixed  $value
238
     * @return void
239
     */
240
    public function offsetSet($offset, $value)
241
    {
242
        $this->items[$offset] = $value;
243
    }
244
    /**
245
     * Unset the value for a given offset.
246
     *
247
     * @access public
248
     * @param  mixed  $offset
249
     * @return void
250
     */
251
    public function offsetUnset($offset)
252
    {
253
        unset($this->items[$offset]);
254
    }
255
256
    /**
257
     * Make the collection items iteratable
258
     *
259
     * @access public
260
     * @return ArrayIterator
261
     */
262
    public function getIterator() {
263
        return new \ArrayIterator($this->items);
264
    }
265
266
}
267