Completed
Push — d64 ( 3a72d6...8b1ebd )
by Welling
02:44
created

EntryCollection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Directus – <http://getdirectus.com>
5
 *
6
 * @link      The canonical repository – <https://github.com/directus/directus>
7
 * @copyright Copyright 2006-2016 RANGER Studio, LLC – <http://rangerstudio.com>
8
 * @license   GNU General Public License (v3) – <http://www.gnu.org/copyleft/gpl.html>
9
 */
10
11
namespace Directus\SDK\Response;
12
13
use Directus\Util\ArrayUtils;
14
15
/**
16
 * Entry Collection
17
 *
18
 * @author Welling Guzmán <[email protected]>
19
 */
20
class EntryCollection implements ResponseInterface, \IteratorAggregate , \Countable
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $items = [];
26
27
    /**
28
     * @var array
29
     */
30
    protected $data = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $metadata = [];
36
37
    /**
38
     * EntryCollection constructor.
39
     *
40
     * @param $data
41
     */
42 8
    public function __construct($data)
43
    {
44 8
        $this->data = $data;
45 8
        $this->metadata = ArrayUtils::omit($data, 'rows');
46
47 8
        $rows = isset($data['rows']) ? $data['rows'] : [];
48 8
        $items = [];
49 8
        foreach($rows as $row) {
50 8
            $items[] = new Entry($row);
51 8
        }
52
53 8
        $this->items = $items;
54 8
    }
55
56
    /**
57
     * Get the response raw data
58
     *
59
     * @return array
60
     */
61 2
    public function getRawData()
62
    {
63 2
        return $this->data;
64
    }
65
66
    /**
67
     * Get the response entries
68
     *
69
     * @return array
70
     */
71 2
    public function getData()
72
    {
73 2
        return $this->items;
74
    }
75
76
    /**
77
     * Get the response metadata
78
     *
79
     * @return array
80
     */
81 2
    public function getMetaData()
82
    {
83 2
        return $this->metadata;
84
    }
85
86
    /**
87
     * Create a new iterator based on this collection
88
     *
89
     * @return \ArrayIterator
90
     */
91 2
    public function getIterator()
92
    {
93 2
        return new \ArrayIterator($this->items);
94
    }
95
96
    /**
97
     * Gets the number of entries in this collection
98
     *
99
     * @return int
100
     */
101 2
    public function count()
102
    {
103 2
        return count($this->items);
104
    }
105
106
    /**
107
     * Gets an object representation of this collection
108
     *
109
     * @return object
110
     */
111 2
    public function jsonSerialize()
112
    {
113
        return (object) [
114 2
            'metadata' => $this->metadata,
115 2
            'data' => $this->items
116 2
        ];
117
    }
118
}