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
|
44 |
|
public function __construct($resource = null, $schema = null, $database = null) |
23
|
|
|
{ |
24
|
44 |
|
$reflectionHelper = new ReflectionHelper; |
25
|
44 |
|
$this->resource = $resource === null ? $reflectionHelper->createInstance($this->schemaClass) : $resource; |
26
|
44 |
|
$this->database = $database === null ? $reflectionHelper->createInstance($this->databaseClass) : $database; |
27
|
44 |
|
$this->schema = $schema === null ? new SchemaValidator : $schema; |
28
|
44 |
|
$this->schema->allowExtraneous($this->allowExtraneous); |
29
|
44 |
|
} |
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
|
12 |
|
public function getDatabaseName() |
44
|
|
|
{ |
45
|
12 |
|
return $this->database->getDatabaseName(); |
46
|
|
|
} |
47
|
|
|
|
48
|
17 |
|
public function resource() |
49
|
|
|
{ |
50
|
17 |
|
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 = array_diff_key( |
136
|
2 |
|
$this->schema->getFieldsWithDefaults($this->resource), |
137
|
2 |
|
get_object_vars($this->resource) |
138
|
|
|
); |
139
|
2 |
|
foreach ($properties as $fieldName => $field) { |
140
|
2 |
|
$this->setFieldDefaultValue($fieldName, $field['default']); |
141
|
|
|
} |
142
|
2 |
|
} |
143
|
|
|
|
144
|
2 |
|
protected function setFieldDefaultValue($fieldName, $value) |
145
|
|
|
{ |
146
|
2 |
|
if (gettype($value) === 'object' && is_callable($value)) { |
147
|
1 |
|
$this->resource->{$fieldName} = $value(); |
148
|
1 |
|
return; |
149
|
|
|
} |
150
|
2 |
|
$this->resource->{$fieldName} = $value; |
151
|
2 |
|
} |
152
|
|
|
|
153
|
7 |
|
public function validateSchema($resource) |
154
|
|
|
{ |
155
|
7 |
|
$this->schema->updateMode($this->updateMode()); |
156
|
7 |
|
$this->schema->validate($resource); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
5 |
|
public function validate() |
160
|
|
|
{ |
161
|
5 |
|
$uniqueFields = $this->getUniqueFields(); |
162
|
5 |
|
if (!empty($uniqueFields)) { |
163
|
4 |
|
$this->validateUniqueFields(); |
164
|
|
|
} |
165
|
4 |
|
} |
166
|
|
|
|
167
|
1 |
|
public function toArray() |
168
|
|
|
{ |
169
|
1 |
|
return $this->resource->toArray(); |
170
|
|
|
} |
171
|
|
|
|
172
|
9 |
|
public function getPrimaryKeyValue() |
173
|
|
|
{ |
174
|
9 |
|
if (property_exists($this->resource, $this->primaryKey)) { |
175
|
7 |
|
return $this->resource->{$this->primaryKey}; |
176
|
|
|
} |
177
|
2 |
|
return null; |
178
|
|
|
} |
179
|
|
|
|
180
|
11 |
|
public function getUniqueFields() |
181
|
|
|
{ |
182
|
11 |
|
return $this->schema->getUniqueFields($this->resource); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
abstract public function validateUniqueFields(); |
186
|
|
|
|
187
|
13 |
|
protected function getClient() |
188
|
|
|
{ |
189
|
13 |
|
return $this->database->getClient(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
abstract public function query(); |
193
|
|
|
} |
194
|
|
|
|