PagedList::items()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest\Objects;
15
16
use InvalidArgumentException;
17
use Pinterest\Authentication;
18
19
/**
20
 * This class represents a paged list.
21
 *
22
 * @author Hans Ott <[email protected]>
23
 */
24
final class PagedList
25
{
26
    /**
27
     * The paged list items.
28
     *
29
     * @var array
30
     */
31
    private $items;
32
33
    /**
34
     * The url for retrieving the next set of items.
35
     *
36
     * @var string
37
     */
38
    private $nextUrl;
39
40
    /**
41
     * Creates a new paged list.
42
     *
43
     * @param array  $items   The paged list items.
44
     * @param string $nextUrl The url for retrieving the next set of items.
45
     */
46 26
    public function __construct(array $items = array(), $nextUrl = null)
47
    {
48 26
        $this->guardThatTheseAreAllPinterestObjects($items);
49 20
        $this->assertValidUri($nextUrl);
50 20
        $this->items = $items;
51 20
        $this->nextUrl = $nextUrl;
52 20
    }
53
54
    /**
55
     * Returns the items.
56
     *
57
     * @return array The items.
58
     */
59 8
    public function items()
60
    {
61 8
        return $this->items;
62
    }
63
64
    /**
65
     * Returns whether the paged list has more items.
66
     *
67
     * @return bool Whether there are more items.
68
     */
69 4
    public function hasNext()
70
    {
71 4
        return !empty($this->nextUrl);
72
    }
73
74
    /**
75
     * Returns the url to get the next set of items.
76
     *
77
     * @return string The url to get the next set of items.
78
     */
79 2
    public function getNextUrl()
80
    {
81 2
        return $this->nextUrl;
82
    }
83
84
    /**
85
     * Checks if all items are pinterest objects.
86
     *
87
     * @param array $items
88
     *
89
     * @throws InvalidArgumentException
90
     */
91 26
    private function guardThatTheseAreAllPinterestObjects(array $items)
92
    {
93 26
        foreach ($items as $item) {
94 22
            if (!($item instanceof BaseObject)) {
95 6
                throw new InvalidArgumentException(sprintf(
96 6
                    'Expected "Pinterest\Objects\BaseObject" but instead got: "%s"',
97 14
                    is_object($item) ? get_class($item) : gettype($item)
98 3
                ));
99
            }
100 10
        }
101 20
    }
102
103
    /**
104
     * Checks if the next uri is valid.
105
     *
106
     * @throws InvalidArgumentException
107
     *
108
     * @param $nextUri
109
     */
110 20
    private function assertValidUri($nextUri)
111
    {
112 20
        if ($nextUri === null) {
113 18
            return;
114
        }
115
116 4
        if (strpos($nextUri, Authentication::BASE_URI) === false) {
117 2
            throw new InvalidArgumentException(
118 1
                'Not a pinterest api uri'
119 1
            );
120
        }
121 4
    }
122
}
123