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
|
32 |
|
public function __construct($restObject = null) |
15
|
|
|
{ |
16
|
32 |
|
if ($restObject === null) { |
17
|
6 |
|
$reflectionHelper = new ReflectionHelper; |
18
|
6 |
|
$restObject = $reflectionHelper->createInstance($this->restObjectClass, []); |
19
|
|
|
} |
20
|
32 |
|
$this->restObject = $restObject; |
21
|
32 |
|
} |
22
|
|
|
|
23
|
16 |
|
protected function useResource($obj) |
24
|
|
|
{ |
25
|
|
|
try { |
26
|
16 |
|
$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
|
15 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
21 |
|
public function resource($resource = null) |
37
|
|
|
{ |
38
|
21 |
|
if ($resource !== null) { |
39
|
16 |
|
$this->useResource($resource); |
40
|
|
|
} |
41
|
20 |
|
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->setFieldDefaults(); |
79
|
3 |
|
$this->validate(); |
80
|
2 |
|
$this->restObject->post($this->resource()); |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function update() |
85
|
|
|
{ |
86
|
2 |
|
$this->restObject->updateMode = true; |
87
|
2 |
|
$this->restObject->setFieldDefaults(); |
88
|
2 |
|
$this->validate(); |
89
|
2 |
|
$this->restObject->put($this->restObject->getPrimaryKeyValue(), $this->resource()); |
90
|
1 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
public function delete() |
94
|
|
|
{ |
95
|
2 |
|
$resourceId = $this->restObject->getPrimaryKeyValue(); |
96
|
2 |
|
$this->restObject->delete($resourceId); |
97
|
1 |
|
return $resourceId; |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
public function validate() |
101
|
|
|
{ |
102
|
12 |
|
$this->restObject->validateSchema($this->resource()); |
103
|
7 |
|
$this->restObject->validate(); |
104
|
6 |
|
} |
105
|
|
|
|
106
|
1 |
|
public function toArray() |
107
|
|
|
{ |
108
|
1 |
|
return $this->restObject->toArray(); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function setDatabaseName($databaseName) |
112
|
|
|
{ |
113
|
1 |
|
$this->restObject->setDatabaseName($databaseName); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
public function getDatabaseName() |
117
|
|
|
{ |
118
|
1 |
|
return $this->restObject->getDatabaseName(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|