1
|
|
|
<?php |
2
|
|
|
namespace Fathomminds\Rest; |
3
|
|
|
|
4
|
|
|
use Fathomminds\Rest\Helpers\ReflectionHelper; |
5
|
|
|
use Fathomminds\Rest\Exceptions\RestException; |
6
|
|
|
use Fathomminds\Rest\Objects\RestObject; |
7
|
|
|
use Fathomminds\Rest\Contracts\IRestModel; |
8
|
|
|
|
9
|
|
|
abstract class RestModel implements IRestModel |
10
|
|
|
{ |
11
|
|
|
protected $restObjectClass; |
12
|
|
|
protected $restObject; |
13
|
|
|
|
14
|
33 |
|
public function __construct($restObject = null) |
15
|
|
|
{ |
16
|
33 |
|
if ($restObject === null) { |
17
|
6 |
|
$reflectionHelper = new ReflectionHelper; |
18
|
6 |
|
$restObject = $reflectionHelper->createInstance($this->restObjectClass, []); |
19
|
|
|
} |
20
|
33 |
|
$this->restObject = $restObject; |
21
|
33 |
|
} |
22
|
|
|
|
23
|
17 |
|
protected function useResource($obj) |
24
|
|
|
{ |
25
|
|
|
try { |
26
|
17 |
|
$this->restObject = $this->restObject->createFromObject($obj); |
27
|
1 |
|
} catch (\Exception $ex) { |
28
|
1 |
|
throw new RestException( |
29
|
1 |
|
'Setting model resource failed', |
30
|
1 |
|
['originalException' => $ex] |
31
|
|
|
); |
32
|
|
|
} |
33
|
16 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
22 |
|
public function resource($resource = null) |
37
|
|
|
{ |
38
|
22 |
|
if ($resource !== null) { |
39
|
17 |
|
$this->useResource($resource); |
40
|
|
|
} |
41
|
21 |
|
return $this->restObject->resource(); |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function query() |
45
|
|
|
{ |
46
|
2 |
|
return $this->restObject->query(); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function find($client = null) |
50
|
|
|
{ |
51
|
3 |
|
return $this->restObject->find($client); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function one($resourceId) |
55
|
|
|
{ |
56
|
3 |
|
$this->restObject->get($resourceId); |
57
|
3 |
|
if ($this->restObject->getPrimaryKeyValue() !== $resourceId) { |
58
|
2 |
|
throw new RestException( |
59
|
2 |
|
'Resource does not exist', |
60
|
|
|
[ |
61
|
2 |
|
'resourceName' => $this->restObject->getResourceName(), |
62
|
2 |
|
'resourceId' => $resourceId, |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
} |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function all() |
70
|
|
|
{ |
71
|
1 |
|
$list = $this->restObject->get(); |
72
|
1 |
|
return $list; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public function create() |
76
|
|
|
{ |
77
|
3 |
|
$this->restObject->updateMode(false); |
78
|
3 |
|
$this->restObject->replaceMode(false); |
79
|
3 |
|
$this->restObject->setFieldDefaults(); |
80
|
3 |
|
$this->validate(); |
81
|
2 |
|
$this->restObject->post($this->resource()); |
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function update() |
86
|
|
|
{ |
87
|
1 |
|
$this->restObject->updateMode(true); |
88
|
1 |
|
$this->restObject->replaceMode(false); |
89
|
1 |
|
$this->restObject->setFieldDefaults(); |
90
|
1 |
|
$this->validate(); |
91
|
1 |
|
$this->restObject->patch($this->restObject->getPrimaryKeyValue(), $this->resource()); |
92
|
1 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function replace() |
96
|
|
|
{ |
97
|
2 |
|
$this->restObject->updateMode(false); |
98
|
2 |
|
$this->restObject->replaceMode(true); |
99
|
2 |
|
$this->restObject->setFieldDefaults(); |
100
|
2 |
|
$this->validate(); |
101
|
2 |
|
$this->restObject->put($this->restObject->getPrimaryKeyValue(), $this->resource()); |
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
public function delete() |
106
|
|
|
{ |
107
|
2 |
|
$resourceId = $this->restObject->getPrimaryKeyValue(); |
108
|
2 |
|
$this->restObject->delete($resourceId); |
109
|
1 |
|
return $resourceId; |
110
|
|
|
} |
111
|
|
|
|
112
|
13 |
|
public function validate() |
113
|
|
|
{ |
114
|
13 |
|
$this->restObject->validateSchema($this->resource()); |
115
|
8 |
|
$this->restObject->validate(); |
116
|
7 |
|
} |
117
|
|
|
|
118
|
1 |
|
public function toArray() |
119
|
|
|
{ |
120
|
1 |
|
return $this->restObject->toArray(); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function setDatabaseName($databaseName) |
124
|
|
|
{ |
125
|
1 |
|
$this->restObject->setDatabaseName($databaseName); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
1 |
|
public function getDatabaseName() |
129
|
|
|
{ |
130
|
1 |
|
return $this->restObject->getDatabaseName(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|