1
|
|
|
<?php |
2
|
|
|
namespace Fathomminds\Rest\Objects; |
3
|
|
|
|
4
|
|
|
use Fathomminds\Rest\Helpers\ReflectionHelper; |
5
|
|
|
use Fathomminds\Rest\Contracts\IRestObject; |
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 $updateMode = false; |
18
|
|
|
|
19
|
32 |
|
public function __construct($resource = null, $schema = null, $database = null) |
20
|
|
|
{ |
21
|
32 |
|
$reflectionHelper = new ReflectionHelper; |
22
|
32 |
|
$this->resource = $resource === null ? new \StdClass : $resource; |
23
|
32 |
|
$this->database = $database === null ? $reflectionHelper->createInstance($this->databaseClass) : $database; |
24
|
32 |
|
$this->schema = $schema === null ? $reflectionHelper->createInstance($this->schemaClass) : $schema; |
25
|
32 |
|
} |
26
|
|
|
|
27
|
11 |
|
public function createFromObject(\StdClass $object) |
28
|
|
|
{ |
29
|
11 |
|
$this->resource = $object; |
30
|
11 |
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
public function getResource() |
34
|
|
|
{ |
35
|
7 |
|
return $this->resource; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function getResourceName() |
39
|
|
|
{ |
40
|
2 |
|
return $this->resourceName; |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
public function get($resourceId = null) |
44
|
|
|
{ |
45
|
4 |
|
if ($resourceId == null) { |
46
|
1 |
|
$resources = $this->database->get($this->resourceName, $this->primaryKey); |
47
|
1 |
|
return $resources; |
48
|
|
|
} |
49
|
3 |
|
$this->resource = $this->database->get($this->resourceName, $this->primaryKey, $resourceId); |
50
|
3 |
|
return $this->resource; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function post($newResource) |
54
|
|
|
{ |
55
|
2 |
|
$this->setUpdateMode(false); |
56
|
2 |
|
$this->setFieldDefaults(); |
57
|
2 |
|
$this->validateSchema($newResource); |
58
|
1 |
|
$this->validate(); |
59
|
1 |
|
$this->resource = $this->database->post($this->resourceName, $this->primaryKey, $newResource); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function put($resourceId, $newResource) |
63
|
|
|
{ |
64
|
1 |
|
$this->setUpdateMode(true); |
65
|
1 |
|
$this->setFieldDefaults(); |
66
|
1 |
|
$this->validateSchema($newResource); |
67
|
1 |
|
$this->validate(); |
68
|
1 |
|
$this->resource = $this->database->put( |
69
|
1 |
|
$this->resourceName, |
70
|
1 |
|
$this->primaryKey, |
71
|
|
|
$resourceId, |
72
|
|
|
$newResource |
73
|
|
|
); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
2 |
|
public function delete($resourceId) |
77
|
|
|
{ |
78
|
2 |
|
$this->database->delete($this->resourceName, $this->primaryKey, $resourceId); |
79
|
1 |
|
$this->reset(); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
public function reset() |
83
|
|
|
{ |
84
|
1 |
|
$this->resource = new \StdClass(); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
4 |
|
protected function setUpdateMode($value) |
88
|
|
|
{ |
89
|
4 |
|
$this->updateMode = $value; |
90
|
4 |
|
} |
91
|
|
|
|
92
|
4 |
|
protected function setFieldDefaults() |
93
|
|
|
{ |
94
|
4 |
|
$properties = get_object_vars($this->resource); |
95
|
4 |
|
foreach ($this->schema->getFields() as $fieldName => $field) { |
96
|
2 |
|
if (isset($properties[$fieldName])) { |
97
|
1 |
|
continue; |
98
|
|
|
} |
99
|
2 |
|
if (!isset($field['default'])) { |
100
|
2 |
|
continue; |
101
|
|
|
} |
102
|
2 |
|
$this->setFieldDefaultValue($fieldName, $field['default']); |
103
|
|
|
} |
104
|
4 |
|
} |
105
|
|
|
|
106
|
2 |
|
protected function setFieldDefaultValue($fieldName, $value) |
107
|
|
|
{ |
108
|
2 |
|
if (gettype($value) === 'object' && is_callable($value)) { |
109
|
1 |
|
$this->resource->{$fieldName} = $value(); |
110
|
1 |
|
return; |
111
|
|
|
} |
112
|
2 |
|
$this->resource->{$fieldName} = $value; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
7 |
|
public function validateSchema($resource) |
116
|
|
|
{ |
117
|
7 |
|
$this->schema->validate($resource); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
4 |
|
public function validate() |
121
|
|
|
{ |
122
|
4 |
|
$uniqueFields = $this->getUniqueFields(); |
123
|
4 |
|
if (!empty($uniqueFields)) { |
124
|
1 |
|
$this->validateUniqueFields(); |
125
|
|
|
} |
126
|
4 |
|
} |
127
|
|
|
|
128
|
1 |
|
public function toArray() |
129
|
|
|
{ |
130
|
1 |
|
return json_decode(json_encode($this->resource), true); |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
public function getProperty($propertyName) |
134
|
|
|
{ |
135
|
4 |
|
if (property_exists($this->resource, $propertyName)) { |
136
|
3 |
|
return $this->resource->{$propertyName}; |
137
|
|
|
} |
138
|
1 |
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
public function setProperty($propertyName, $propertyValue) |
142
|
|
|
{ |
143
|
2 |
|
$this->resource->{$propertyName} = $propertyValue; |
144
|
2 |
|
return $this; |
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(); |
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
|
|
|
|