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