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
|
|
|
use Directus\Util\ArrayUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Entry |
16
|
|
|
* |
17
|
|
|
* @author Welling Guzmán <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Entry implements ResponseInterface, \ArrayAccess |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $data = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $rawData = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Entry |
33
|
|
|
*/ |
34
|
|
|
protected $metadata = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Entry constructor. |
38
|
|
|
* |
39
|
|
|
* @param $data |
40
|
|
|
*/ |
41
|
58 |
|
public function __construct($data) |
42
|
|
|
{ |
43
|
58 |
|
$this->rawData = $data; |
44
|
58 |
|
if (!is_array($data)) { |
45
|
10 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Support API 1.1 |
49
|
58 |
|
if (isset($data['data']) && is_array($data['data'])) { |
50
|
2 |
|
$this->metadata = new static(ArrayUtils::get($data, 'meta', [])); |
51
|
2 |
|
unset($data['meta']); |
52
|
|
|
|
53
|
2 |
|
$data = $data['data']; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
58 |
|
foreach($data as $field => $value) { |
57
|
58 |
|
if (isset($value['rows']) || (isset($value['data']) && ArrayUtils::isNumericKeys($value['data']))) { |
58
|
14 |
|
$this->data[$field] = new EntryCollection($value); |
59
|
58 |
|
} else if (is_array($value)) { |
60
|
12 |
|
$this->data[$field] = new static($value); |
61
|
6 |
|
} else { |
62
|
58 |
|
$this->data[$field] = $value; |
63
|
|
|
} |
64
|
29 |
|
} |
65
|
58 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the entry data |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
8 |
|
public function getData() |
73
|
|
|
{ |
74
|
8 |
|
return $this->data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the entry metadata |
79
|
|
|
* |
80
|
|
|
* @return Entry |
81
|
|
|
*/ |
82
|
8 |
|
public function getMetaData() |
83
|
|
|
{ |
84
|
8 |
|
return $this->metadata; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
16 |
|
public function getRawData() |
91
|
|
|
{ |
92
|
16 |
|
return $this->rawData; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function offsetExists($offset) |
96
|
|
|
{ |
97
|
2 |
|
return array_key_exists($offset, $this->data); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
public function offsetGet($offset) |
101
|
|
|
{ |
102
|
4 |
|
return $this->data[$offset]; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function offsetSet($offset, $value) |
106
|
|
|
{ |
107
|
2 |
|
throw new \BadMethodCallException('Entry is read only'); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
public function offsetUnset($offset) |
111
|
|
|
{ |
112
|
2 |
|
throw new \BadMethodCallException('Entry is read only'); |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
public function __get($name) |
|
|
|
|
116
|
|
|
{ |
117
|
4 |
|
if (array_key_exists($name, $this->data)) { |
118
|
2 |
|
return $this->data[$name]; |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
throw new \InvalidArgumentException('Invalid property: ' . $name); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public function __set($name, $value) |
125
|
|
|
{ |
126
|
2 |
|
throw new \BadMethodCallException('Entry is read only'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheridoc |
131
|
|
|
*/ |
132
|
|
|
public function toArray() |
133
|
|
|
{ |
134
|
|
|
return $this->getRawData(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Gets the object representation of this entry |
139
|
|
|
* |
140
|
|
|
* @return object |
141
|
|
|
*/ |
142
|
4 |
|
public function jsonSerialize() |
143
|
|
|
{ |
144
|
|
|
return (object) [ |
145
|
4 |
|
'metadata' => $this->getMetaData(), |
146
|
4 |
|
'data' => $this->getData() |
147
|
2 |
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.