1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Ember Db - An embeddable document database for php. |
4
|
|
|
* Copyright (C) 2016 Alexander During |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* @link http://github.com/alexanderduring/php-ember-db |
20
|
|
|
* @copyright Copyright (C) 2016 Alexander During |
21
|
|
|
* @license http://www.gnu.org/licenses GNU General Public License v3.0 |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace EmberDb; |
25
|
|
|
|
26
|
|
|
use JsonSerializable; |
27
|
|
|
|
28
|
|
|
class Document implements JsonSerializable |
29
|
|
|
{ |
30
|
|
|
private static $PATH_ID = '_meta.id'; |
31
|
|
|
|
32
|
|
|
private $data; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $data |
38
|
|
|
*/ |
39
|
|
|
public function __construct($data = array()) |
40
|
|
|
{ |
41
|
|
|
$this->data = $data; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $path 'comma.separated.path.to.value' |
48
|
|
|
* @return boolean |
49
|
|
|
*/ |
50
|
|
|
public function has($path) |
51
|
|
|
{ |
52
|
|
|
$exists = true; |
53
|
|
|
|
54
|
|
|
$subtree = $this->data; |
55
|
|
|
$segments = $this->getPathSegments($path); |
56
|
|
|
|
57
|
|
|
foreach ($segments as $key) { |
58
|
|
|
if (is_array($subtree) && array_key_exists($key, $subtree)) { |
59
|
|
|
$subtree = $subtree[$key]; |
60
|
|
|
} else { |
61
|
|
|
$exists = false; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $exists; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path 'comma.separated.path.to.value' |
72
|
|
|
* @return array|mixed |
73
|
|
|
*/ |
74
|
|
|
public function get($path) |
75
|
|
|
{ |
76
|
|
|
$subtree = $this->data; |
77
|
|
|
$segments = $this->getPathSegments($path); |
78
|
|
|
|
79
|
|
|
foreach ($segments as $key) { |
80
|
|
|
$subtree = $subtree[$key]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $subtree; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This method allows you to set a value in the document by using this syntax: |
90
|
|
|
* |
91
|
|
|
* set('path.to.level', 'myValue') |
92
|
|
|
* |
93
|
|
|
* ... does nothing else than ... |
94
|
|
|
* |
95
|
|
|
* document[path][to][level] = 'myValue' |
96
|
|
|
* |
97
|
|
|
* Because the path is translated into an array structure in interations |
98
|
|
|
* we need to store the reference to each intermediate array field |
99
|
|
|
* for usage in the next iteration. |
100
|
|
|
* |
101
|
|
|
* @param string $path 'comma.separated.path.to.value' |
102
|
|
|
* @param mixed $value The value to store |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function set($path, $value) |
106
|
|
|
{ |
107
|
|
|
$subtree = &$this->data; |
108
|
|
|
$segments = $this->getPathSegments($path); |
109
|
|
|
$lastKey = array_pop($segments); |
110
|
|
|
|
111
|
|
|
foreach ($segments as $key) { |
112
|
|
|
if (array_key_exists($key, $subtree)) { |
113
|
|
|
if (!is_array($subtree[$key])) { |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
$subtree = &$subtree[$key]; |
117
|
|
|
} else { |
118
|
|
|
$subtree[$key] = array(); |
119
|
|
|
$subtree = &$subtree[$key]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$subtree[$lastKey] = $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function hasId(): bool |
129
|
|
|
{ |
130
|
|
|
return $this->has(self::$PATH_ID); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
public function getId(): string |
136
|
|
|
{ |
137
|
|
|
return $this->get(self::$PATH_ID); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function setId(string $id) |
143
|
|
|
{ |
144
|
|
|
$this->set(self::$PATH_ID, $id); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
public function toArray() |
150
|
|
|
{ |
151
|
|
|
return $this->data; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $path |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
private function getPathSegments($path) |
160
|
|
|
{ |
161
|
|
|
trim($path, '.'); |
162
|
|
|
$segments = explode('.', $path); |
163
|
|
|
|
164
|
|
|
return $segments; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
|
169
|
|
|
public function jsonSerialize() |
170
|
|
|
{ |
171
|
|
|
return $this->data; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|