|
1
|
|
|
<?php namespace Fathomminds\Rest\Objects; |
|
2
|
|
|
|
|
3
|
|
|
use Fathomminds\Rest\Helpers\ReflectionHelper; |
|
4
|
|
|
use Fathomminds\Rest\Contracts\IRestObject; |
|
5
|
|
|
use Fathomminds\Rest\Schema\SchemaValidator; |
|
6
|
|
|
use Fathomminds\Rest\Database\Clusterpoint\Database; |
|
7
|
|
|
|
|
8
|
|
|
abstract class RestObject implements IRestObject |
|
9
|
|
|
{ |
|
10
|
|
|
protected $resourceName; |
|
11
|
|
|
protected $primaryKey; |
|
12
|
|
|
protected $resource; |
|
13
|
|
|
protected $schemaClass; |
|
14
|
|
|
protected $schema; |
|
15
|
|
|
protected $databaseClass; |
|
16
|
|
|
protected $database; |
|
17
|
|
|
protected $indexNames = []; |
|
18
|
|
|
protected $allowExtraneous = false; |
|
19
|
|
|
private $updateMode = false; |
|
20
|
|
|
private $replaceMode = false; |
|
21
|
|
|
|
|
22
|
43 |
|
public function __construct($resource = null, $schema = null, $database = null) |
|
23
|
|
|
{ |
|
24
|
43 |
|
$reflectionHelper = new ReflectionHelper; |
|
25
|
43 |
|
$this->resource = $resource === null ? $reflectionHelper->createInstance($this->schemaClass) : $resource; |
|
26
|
43 |
|
$this->database = $database === null ? $reflectionHelper->createInstance($this->databaseClass) : $database; |
|
27
|
43 |
|
$this->schema = $schema === null ? new SchemaValidator : $schema; |
|
28
|
43 |
|
$this->schema->allowExtraneous($this->allowExtraneous); |
|
29
|
43 |
|
} |
|
30
|
|
|
|
|
31
|
12 |
|
public function createFromObject($object) |
|
32
|
|
|
{ |
|
33
|
12 |
|
$reflectionHelper = new ReflectionHelper; |
|
34
|
12 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass, [$object]); |
|
35
|
11 |
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function setDatabaseName($databaseName) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->database->setDatabaseName($databaseName); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
11 |
|
public function getDatabaseName() |
|
44
|
|
|
{ |
|
45
|
11 |
|
return $this->database->getDatabaseName(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
16 |
|
public function resource() |
|
49
|
|
|
{ |
|
50
|
16 |
|
return $this->resource; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
7 |
|
public function getResourceName() |
|
54
|
|
|
{ |
|
55
|
7 |
|
return $this->resourceName; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
public function get($resourceId = null) |
|
59
|
|
|
{ |
|
60
|
5 |
|
$reflectionHelper = new ReflectionHelper; |
|
61
|
5 |
|
if ($resourceId === null) { |
|
62
|
2 |
|
$rawResources = $this->database->get($this->resourceName, $this->primaryKey); |
|
63
|
2 |
|
$resources = []; |
|
64
|
2 |
|
foreach ($rawResources as $rawResource) { |
|
65
|
1 |
|
$resources[] = $reflectionHelper->createInstance($this->schemaClass, [$rawResource]); |
|
66
|
|
|
} |
|
67
|
2 |
|
return $resources; |
|
68
|
|
|
} |
|
69
|
3 |
|
$res = $this->database->get($this->resourceName, $this->primaryKey, $resourceId); |
|
70
|
3 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass, [$res]); |
|
71
|
3 |
|
return $this->resource; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function post($newResource) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
|
77
|
1 |
|
$res = $this->database->post($this->resourceName, $this->primaryKey, $newResource); |
|
78
|
1 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass, [$res]); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
public function patch($resourceId, $newResource) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
|
84
|
1 |
|
$res = $this->database->patch( |
|
85
|
1 |
|
$this->resourceName, |
|
86
|
1 |
|
$this->primaryKey, |
|
87
|
1 |
|
$resourceId, |
|
88
|
1 |
|
$newResource |
|
89
|
|
|
); |
|
90
|
1 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass, [$res]); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function put($resourceId, $newResource) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
|
96
|
1 |
|
$res = $this->database->put( |
|
97
|
1 |
|
$this->resourceName, |
|
98
|
1 |
|
$this->primaryKey, |
|
99
|
1 |
|
$resourceId, |
|
100
|
1 |
|
$newResource |
|
101
|
|
|
); |
|
102
|
1 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass, [$res]); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
public function delete($resourceId) |
|
106
|
|
|
{ |
|
107
|
2 |
|
$this->database->delete($this->resourceName, $this->primaryKey, $resourceId); |
|
108
|
1 |
|
$this->reset(); |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function reset() |
|
112
|
|
|
{ |
|
113
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
|
114
|
1 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass); |
|
115
|
1 |
|
} |
|
116
|
|
|
|
|
117
|
5 |
|
public function replaceMode($value = null) |
|
118
|
|
|
{ |
|
119
|
5 |
|
if (is_bool($value)) { |
|
120
|
2 |
|
$this->replaceMode = $value; |
|
121
|
|
|
} |
|
122
|
5 |
|
return $this->replaceMode; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
10 |
|
public function updateMode($value = null) |
|
126
|
|
|
{ |
|
127
|
10 |
|
if (is_bool($value)) { |
|
128
|
2 |
|
$this->updateMode = $value; |
|
129
|
|
|
} |
|
130
|
10 |
|
return $this->updateMode; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
public function setFieldDefaults() |
|
134
|
|
|
{ |
|
135
|
2 |
|
$properties = get_object_vars($this->resource); |
|
136
|
2 |
|
foreach ($this->schema->getFields($this->resource) as $fieldName => $field) { |
|
137
|
2 |
|
if (isset($properties[$fieldName])) { |
|
138
|
1 |
|
continue; |
|
139
|
|
|
} |
|
140
|
2 |
|
if (!array_key_exists('default', $field)) { |
|
141
|
2 |
|
continue; |
|
142
|
|
|
} |
|
143
|
2 |
|
$this->setFieldDefaultValue($fieldName, $field['default']); |
|
144
|
|
|
} |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
protected function setFieldDefaultValue($fieldName, $value) |
|
148
|
|
|
{ |
|
149
|
2 |
|
if (gettype($value) === 'object' && is_callable($value)) { |
|
150
|
1 |
|
$this->resource->{$fieldName} = $value(); |
|
151
|
1 |
|
return; |
|
152
|
|
|
} |
|
153
|
2 |
|
$this->resource->{$fieldName} = $value; |
|
154
|
2 |
|
} |
|
155
|
|
|
|
|
156
|
7 |
|
public function validateSchema($resource) |
|
157
|
|
|
{ |
|
158
|
7 |
|
$this->schema->updateMode($this->updateMode()); |
|
159
|
7 |
|
$this->schema->validate($resource); |
|
160
|
2 |
|
} |
|
161
|
|
|
|
|
162
|
5 |
|
public function validate() |
|
163
|
|
|
{ |
|
164
|
5 |
|
$uniqueFields = $this->getUniqueFields(); |
|
165
|
5 |
|
if (!empty($uniqueFields)) { |
|
166
|
4 |
|
$this->validateUniqueFields(); |
|
167
|
|
|
} |
|
168
|
4 |
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
public function toArray() |
|
171
|
|
|
{ |
|
172
|
1 |
|
return $this->resource->toArray(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
9 |
|
public function getPrimaryKeyValue() |
|
176
|
|
|
{ |
|
177
|
9 |
|
if (property_exists($this->resource, $this->primaryKey)) { |
|
178
|
7 |
|
return $this->resource->{$this->primaryKey}; |
|
179
|
|
|
} |
|
180
|
2 |
|
return null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
11 |
|
public function getUniqueFields() |
|
184
|
|
|
{ |
|
185
|
11 |
|
return $this->schema->getUniqueFields($this->resource); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
abstract public function validateUniqueFields(); |
|
189
|
|
|
|
|
190
|
12 |
|
protected function getClient() |
|
191
|
|
|
{ |
|
192
|
12 |
|
return $this->database->getClient(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
abstract public function query(); |
|
196
|
|
|
} |
|
197
|
|
|
|