|
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
|
25 |
|
public function __construct($restObject = null) |
|
15
|
|
|
{ |
|
16
|
25 |
|
if ($restObject === null) { |
|
17
|
1 |
|
$reflectionHelper = new ReflectionHelper; |
|
18
|
1 |
|
$restObject = $reflectionHelper->createInstance($this->restObjectClass, []); |
|
19
|
|
|
} |
|
20
|
25 |
|
$this->restObject = $restObject; |
|
21
|
25 |
|
} |
|
22
|
|
|
|
|
23
|
16 |
|
public function use($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
|
17 |
|
public function resource() |
|
37
|
|
|
{ |
|
38
|
17 |
|
return $this->restObject->resource(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
public function one($resourceId) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->restObject->get($resourceId); |
|
44
|
3 |
|
if ($this->restObject->getPrimaryKeyValue() !== $resourceId) { |
|
45
|
2 |
|
throw new RestException( |
|
46
|
2 |
|
'Resource does not exist', |
|
47
|
|
|
[ |
|
48
|
2 |
|
'resourceName' => $this->restObject->getResourceName(), |
|
49
|
2 |
|
'resourceId' => $resourceId, |
|
50
|
|
|
] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
1 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function all() |
|
57
|
|
|
{ |
|
58
|
1 |
|
$list = $this->restObject->get(); |
|
59
|
1 |
|
return $list; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function create() |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->restObject->post($this->resource()); |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
public function update() |
|
69
|
|
|
{ |
|
70
|
2 |
|
$this->restObject->put($this->restObject->getPrimaryKeyValue(), $this->resource()); |
|
71
|
1 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function delete() |
|
75
|
|
|
{ |
|
76
|
2 |
|
$resourceId = $this->restObject->getPrimaryKeyValue(); |
|
77
|
2 |
|
$this->restObject->delete($resourceId); |
|
78
|
1 |
|
return $resourceId; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
public function validate() |
|
82
|
|
|
{ |
|
83
|
7 |
|
$this->restObject->validateSchema($this->resource()); |
|
84
|
3 |
|
$this->restObject->validate(); |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
public function toArray() |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->restObject->toArray(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|