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