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
|
|
|
|
18
|
|
|
public function __construct($resource = null, $schema = null, $database = null) |
19
|
|
|
{ |
20
|
|
|
$reflectionHelper = new ReflectionHelper; |
21
|
|
|
$this->resource = $resource === null ? new \StdClass : $resource; |
22
|
|
|
$this->database = $database === null ? $reflectionHelper->createInstance($this->databaseClass) : $database; |
23
|
|
|
$this->schema = $schema === null ? $reflectionHelper->createInstance($this->schemaClass) : $schema; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createFromObject(\StdClass $object) |
27
|
|
|
{ |
28
|
|
|
$this->resource = $object; |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getResource() |
33
|
|
|
{ |
34
|
|
|
return $this->resource; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getResourceName() |
38
|
|
|
{ |
39
|
|
|
return $this->resourceName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function get($resourceId = null) |
43
|
|
|
{ |
44
|
|
|
if ($resourceId == null) { |
45
|
|
|
$resources = $this->database->get($this->resourceName, $this->primaryKey); |
46
|
|
|
return $resources; |
47
|
|
|
} |
48
|
|
|
$this->resource = $this->database->get($this->resourceName, $this->primaryKey, $resourceId); |
49
|
|
|
return $this->resource; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function post($newResource) |
53
|
|
|
{ |
54
|
|
|
$this->setUpdateMode(false); |
55
|
|
|
$this->setFieldDefaults(); |
56
|
|
|
$this->validateSchema($newResource); |
57
|
|
|
$this->validate(); |
58
|
|
|
$this->resource = $this->database->post($this->resourceName, $this->primaryKey, $newResource); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function put($resourceId, $newResource) |
62
|
|
|
{ |
63
|
|
|
$this->setUpdateMode(true); |
64
|
|
|
$this->setFieldDefaults(); |
65
|
|
|
$this->validateSchema($newResource); |
66
|
|
|
$this->validate(); |
67
|
|
|
$this->resource = $this->database->put( |
68
|
|
|
$this->resourceName, |
69
|
|
|
$this->primaryKey, |
70
|
|
|
$resourceId, |
71
|
|
|
$newResource |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function delete($resourceId) |
76
|
|
|
{ |
77
|
|
|
$this->database->delete($this->resourceName, $this->primaryKey, $resourceId); |
78
|
|
|
$this->reset(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function reset() |
82
|
|
|
{ |
83
|
|
|
$this->resource = new \StdClass(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function setUpdateMode($value) |
87
|
|
|
{ |
88
|
|
|
$this->updateMode = $value; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function setFieldDefaults() |
92
|
|
|
{ |
93
|
|
|
$properties = get_object_vars($this->resource); |
94
|
|
|
foreach ($this->schema->getFields() as $fieldName => $field) { |
95
|
|
|
if (isset($properties[$fieldName])) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
if (!isset($field['default'])) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
$this->setFieldDefaultValue($fieldName, $field['default']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function setFieldDefaultValue($fieldName, $value) |
106
|
|
|
{ |
107
|
|
|
if (gettype($value) === 'object' && is_callable($value)) { |
108
|
|
|
$this->resource->{$fieldName} = $value(); |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
$this->resource->{$fieldName} = $value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function validateSchema($resource) |
115
|
|
|
{ |
116
|
|
|
$this->schema->validate($resource); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function validate() |
120
|
|
|
{ |
121
|
|
|
$this->validateUniqueFields(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function toArray() |
125
|
|
|
{ |
126
|
|
|
return json_decode(json_encode($this->resource), true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getProperty($propertyName) |
130
|
|
|
{ |
131
|
|
|
if (property_exists($this->resource, $propertyName)) { |
132
|
|
|
return $this->resource->{$propertyName}; |
133
|
|
|
} |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function setProperty($propertyName, $propertyValue) |
138
|
|
|
{ |
139
|
|
|
$this->resource->{$propertyName} = $propertyValue; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getPrimaryKeyValue() |
144
|
|
|
{ |
145
|
|
|
if (property_exists($this->resource, $this->primaryKey)) { |
146
|
|
|
return $this->resource->{$this->primaryKey}; |
147
|
|
|
} |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getUniqueFields() |
152
|
|
|
{ |
153
|
|
|
return $this->schema->getUniqueFields(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
abstract public function validateUniqueFields(); |
157
|
|
|
|
158
|
|
|
protected function getDatabaseName() |
159
|
|
|
{ |
160
|
|
|
return $this->database->getDatabaseName(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
protected function getClient() |
164
|
|
|
{ |
165
|
|
|
return $this->database->getClient(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: