Collection::setCursor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rexlabs\Smokescreen\Resource;
4
5
use Rexlabs\Smokescreen\Exception\ArrayConversionException;
6
use Rexlabs\Smokescreen\Exception\NotIterableException;
7
use Rexlabs\Smokescreen\Pagination\CursorInterface;
8
use Rexlabs\Smokescreen\Pagination\PageableInterface;
9
use Rexlabs\Smokescreen\Pagination\PaginatorInterface;
10
11
class Collection extends AbstractResource implements PageableInterface, \IteratorAggregate
12
{
13
    /** @var PaginatorInterface */
14
    protected $paginator;
15
16
    /** @var CursorInterface */
17
    protected $cursor;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    public function getPaginator()
23
    {
24 1
        return $this->paginator;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function setPaginator(PaginatorInterface $paginator)
31
    {
32 1
        $this->cursor = null;
33 1
        $this->paginator = $paginator;
34
35 1
        return $this;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 4
    public function hasPaginator(): bool
42
    {
43 4
        return $this->paginator instanceof PaginatorInterface;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function getCursor()
50
    {
51 1
        return $this->cursor;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function setCursor(CursorInterface $cursor)
58
    {
59 1
        $this->paginator = null;
60 1
        $this->cursor = $cursor;
61
62 1
        return $this;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function hasCursor(): bool
69
    {
70 1
        return $this->cursor instanceof CursorInterface;
71
    }
72
73
    /**
74
     * Returns an Iterator (to implement the ArrayIterator) interface for
75
     * easily traversing a collection.
76
     *
77
     * @throws \Rexlabs\Smokescreen\Exception\NotIterableException
78
     *
79
     * @return \Traversable
80
     */
81 6
    public function getIterator(): \Traversable
82
    {
83 6
        if ($this->data instanceof \ArrayIterator) {
84 1
            return $this->data;
85
        }
86
87 6
        if ($this->data instanceof \IteratorAggregate) {
88 1
            return $this->data->getIterator();
89
        }
90
91 6
        if (!\is_array($this->data)) {
92 1
            throw new NotIterableException('Cannot get iterator for data');
93
        }
94
95 6
        return new \ArrayIterator($this->data);
96
    }
97
98
    /**
99
     * Converts the Collection data to an array.
100
     *
101
     * @throws \Rexlabs\Smokescreen\Exception\ArrayConversionException
102
     *
103
     * @return array|\ArrayIterator|mixed|null
104
     */
105 1
    public function toArray()
106
    {
107 1
        if (\is_array($this->data)) {
108 1
            return $this->data;
109
        }
110
111 1
        if ($this->data instanceof \ArrayIterator) {
112 1
            return iterator_to_array($this->data, false);
113
        }
114
115 1
        if ($this->data instanceof \IteratorAggregate) {
116 1
            return iterator_to_array($this->data->getIterator(), false);
117
        }
118
119 1
        throw new ArrayConversionException('Cannot convert data to array');
120
    }
121
}
122