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 $indexNames = []; |
19
|
|
|
protected $allowExtraneous = false; |
20
|
|
|
public $updateMode = false; |
21
|
|
|
|
22
|
41 |
|
public function __construct($resource = null, $schema = null, $database = null) |
23
|
|
|
{ |
24
|
41 |
|
$reflectionHelper = new ReflectionHelper; |
25
|
41 |
|
$this->resource = $resource === null ? $reflectionHelper->createInstance($this->schemaClass) : $resource; |
26
|
41 |
|
$this->database = $database === null ? $reflectionHelper->createInstance($this->databaseClass) : $database; |
27
|
41 |
|
$this->schema = $schema === null ? new SchemaValidator : $schema; |
28
|
41 |
|
$this->schema->allowExtraneous($this->allowExtraneous); |
29
|
41 |
|
} |
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
|
10 |
|
public function getDatabaseName() |
44
|
|
|
{ |
45
|
10 |
|
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 put($resourceId, $newResource) |
82
|
|
|
{ |
83
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
84
|
1 |
|
$res = $this->database->put( |
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
|
2 |
|
public function delete($resourceId) |
94
|
|
|
{ |
95
|
2 |
|
$this->database->delete($this->resourceName, $this->primaryKey, $resourceId); |
96
|
1 |
|
$this->reset(); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function reset() |
100
|
|
|
{ |
101
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
102
|
1 |
|
$this->resource = $reflectionHelper->createInstance($this->schemaClass); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
protected function setUpdateMode($value) |
106
|
|
|
{ |
107
|
1 |
|
$this->updateMode = $value; |
108
|
1 |
|
} |
109
|
|
|
|
110
|
2 |
|
public function setFieldDefaults() |
111
|
|
|
{ |
112
|
2 |
|
$properties = get_object_vars($this->resource); |
113
|
2 |
|
foreach ($this->schema->getFields($this->resource) as $fieldName => $field) { |
114
|
2 |
|
if (isset($properties[$fieldName])) { |
115
|
1 |
|
continue; |
116
|
|
|
} |
117
|
2 |
|
if (!array_key_exists('default', $field)) { |
118
|
2 |
|
continue; |
119
|
|
|
} |
120
|
2 |
|
$this->setFieldDefaultValue($fieldName, $field['default']); |
121
|
|
|
} |
122
|
2 |
|
} |
123
|
|
|
|
124
|
2 |
|
protected function setFieldDefaultValue($fieldName, $value) |
125
|
|
|
{ |
126
|
2 |
|
if (gettype($value) === 'object' && is_callable($value)) { |
127
|
1 |
|
$this->resource->{$fieldName} = $value(); |
128
|
1 |
|
return; |
129
|
|
|
} |
130
|
2 |
|
$this->resource->{$fieldName} = $value; |
131
|
2 |
|
} |
132
|
|
|
|
133
|
7 |
|
public function validateSchema($resource) |
134
|
|
|
{ |
135
|
7 |
|
$this->schema->validate($resource); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
4 |
|
public function validate() |
139
|
|
|
{ |
140
|
4 |
|
$uniqueFields = $this->getUniqueFields(); |
141
|
4 |
|
if (!empty($uniqueFields)) { |
142
|
3 |
|
$this->validateUniqueFields(); |
143
|
|
|
} |
144
|
3 |
|
} |
145
|
|
|
|
146
|
1 |
|
public function toArray() |
147
|
|
|
{ |
148
|
1 |
|
return json_decode(json_encode($this->resource), true); |
149
|
|
|
} |
150
|
|
|
|
151
|
8 |
|
public function getPrimaryKeyValue() |
152
|
|
|
{ |
153
|
8 |
|
if (property_exists($this->resource, $this->primaryKey)) { |
154
|
6 |
|
return $this->resource->{$this->primaryKey}; |
155
|
|
|
} |
156
|
2 |
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
10 |
|
public function getUniqueFields() |
160
|
|
|
{ |
161
|
10 |
|
return $this->schema->getUniqueFields($this->resource); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
abstract public function validateUniqueFields(); |
165
|
|
|
|
166
|
11 |
|
protected function getClient() |
167
|
|
|
{ |
168
|
11 |
|
return $this->database->getClient(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
abstract public function query(); |
172
|
|
|
} |
173
|
|
|
|