Completed
Push — d64 ( 17b7b4...2d7146 )
by Welling
02:13
created

Entry::getRawData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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
/**
14
 * Entry
15
 *
16
 * @author Welling Guzmán <[email protected]>
17
 */
18
class Entry implements ResponseInterface, \ArrayAccess
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $data = null;
24
25
    /**
26
     * @var array
27
     */
28
    protected $rawData = null;
29
30
    /**
31
     * @var array
32
     */
33
    protected $metadata = null;
34
35
    /**
36
     * Entry constructor.
37
     *
38
     * @param $data
39
     */
40 22
    public function __construct($data)
41
    {
42 22
        $this->rawData = $data;
43 22
        foreach($data as $field => $value) {
44 22
            if (isset($value['rows'])) {
45
                $this->data[$field] = new EntryCollection($value);
46 22
            } else if (is_array($value)) {
47
                $this->data[$field] = new static($value);
48
            } else {
49 22
                $this->data[$field] = $value;
50
            }
51 22
        }
52 22
    }
53
54
    /**
55
     * Get the entry data
56
     *
57
     * @return array
58
     */
59 6
    public function getData()
60
    {
61 6
        return $this->data;
62
    }
63
64
    /**
65
     * Get the entry metadata
66
     *
67
     * @return array
68
     */
69 6
    public function getMetaData()
70
    {
71 6
        return $this->metadata;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function getRawData()
78
    {
79
        return $this->rawData;
80
    }
81
82 2
    public function offsetExists($offset)
83
    {
84 2
        return array_key_exists($offset, $this->data);
85
    }
86
87 2
    public function offsetGet($offset)
88
    {
89 2
        return $this->data[$offset];
90
    }
91
92 2
    public function offsetSet($offset, $value)
93
    {
94 2
        throw new \BadMethodCallException('Entry is read only');
95
    }
96
97 2
    public function offsetUnset($offset)
98
    {
99 2
        throw new \BadMethodCallException('Entry is read only');
100
    }
101
102 2
    public function __get($name)
103
    {
104 2
        if (array_key_exists($name, $this->data)) {
105
            return $this->data[$name];
106
        }
107
108 2
        throw new \InvalidArgumentException('Invalid property: ' . $name);
109
    }
110
111 2
    public function __set($name, $value)
112
    {
113 2
        throw new \BadMethodCallException('Entry is read only');
114
    }
115
116
    /**
117
     * Gets the object representation of this entry
118
     *
119
     * @return object
120
     */
121 4
    public function jsonSerialize()
122
    {
123
        return (object) [
124 4
            'metadata' => $this->getMetaData(),
125 4
            'data' => $this->getData()
126 4
        ];
127
    }
128
}