1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\domain\base; |
3
|
|
|
|
4
|
|
|
use keeko\core\model\Api; |
5
|
|
|
use keeko\core\model\ApiQuery; |
6
|
|
|
use keeko\framework\service\ServiceContainer; |
7
|
|
|
use keeko\framework\domain\payload\Found; |
8
|
|
|
use keeko\framework\domain\payload\NotFound; |
9
|
|
|
use Tobscure\JsonApi\Parameters; |
10
|
|
|
use keeko\framework\utils\NameUtils; |
11
|
|
|
use keeko\framework\domain\payload\Created; |
12
|
|
|
use keeko\framework\domain\payload\Updated; |
13
|
|
|
use keeko\framework\domain\payload\NotUpdated; |
14
|
|
|
use keeko\framework\domain\payload\NotValid; |
15
|
|
|
use keeko\framework\domain\payload\Deleted; |
16
|
|
|
use keeko\framework\domain\payload\NotDeleted; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
*/ |
20
|
|
|
trait ApiDomainTrait { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param mixed $data |
24
|
|
|
*/ |
25
|
|
|
public function create($data) { |
26
|
|
|
// hydrate |
27
|
|
|
$serializer = Api::getSerializer(); |
28
|
|
|
$api = $serializer->hydrate(new Api(), $data); |
29
|
|
|
|
30
|
|
|
// validate |
31
|
|
|
if (!$api->validate()) { |
32
|
|
|
return new NotValid([ |
33
|
|
|
'errors' => $api->getValidationFailures() |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$api->save(); |
38
|
|
|
return new Created(['model' => $api]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $id |
43
|
|
|
*/ |
44
|
|
|
public function delete($id) { |
45
|
|
|
// find |
46
|
|
|
$api = ApiQuery::create()->findOneById($id); |
47
|
|
|
|
48
|
|
|
if ($api === null) { |
49
|
|
|
return new NotFound(['message' => 'Api not found.']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// delete |
53
|
|
|
$api->delete(); |
54
|
|
|
$payload = ['model' => $api]; |
55
|
|
|
|
56
|
|
|
if ($api->isDeleted()) { |
57
|
|
|
return new Deleted($payload); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new NotDeleted($payload); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Parameters $params |
65
|
|
|
*/ |
66
|
|
|
public function paginate(Parameters $params) { |
67
|
|
|
$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences(); |
68
|
|
|
$defaultSize = $sysPrefs->getPaginationSize(); |
69
|
|
|
$page = $params->getPage('number'); |
|
|
|
|
70
|
|
|
$size = $params->getPage('size', $defaultSize); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$query = ApiQuery::create(); |
73
|
|
|
|
74
|
|
|
// sorting |
75
|
|
|
$sort = $params->getSort(Api::getSerializer()->getSortFields()); |
76
|
|
|
foreach ($sort as $field => $order) { |
77
|
|
|
$method = 'orderBy' . NameUtils::toStudlyCase($field); |
78
|
|
|
$query->$method($order); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// filtering |
82
|
|
|
$filter = $params->getFilter(); |
83
|
|
|
if (!empty($filter)) { |
84
|
|
|
$this->applyFilter($query, $filter); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// paginate |
88
|
|
|
$api = $query->paginate($page, $size); |
89
|
|
|
|
90
|
|
|
// run response |
91
|
|
|
return new Found(['model' => $api]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param mixed $id |
96
|
|
|
*/ |
97
|
|
|
public function read($id) { |
98
|
|
|
// read |
99
|
|
|
$api = ApiQuery::create()->findOneById($id); |
100
|
|
|
|
101
|
|
|
// check existence |
102
|
|
|
if ($api === null) { |
103
|
|
|
$payload = new NotFound(['message' => 'Api not found.']); |
104
|
|
|
} else { |
105
|
|
|
$payload = new Found(['model' => $api]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// run response |
109
|
|
|
return $payload; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $id |
114
|
|
|
* @param mixed $data |
115
|
|
|
*/ |
116
|
|
|
public function update($id, $data) { |
117
|
|
|
// find |
118
|
|
|
$api = ApiQuery::create()->findOneById($id); |
119
|
|
|
|
120
|
|
|
if ($api === null) { |
121
|
|
|
return new NotFound(['message' => 'Api not found.']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// hydrate |
125
|
|
|
$serializer = Api::getSerializer(); |
126
|
|
|
$api = $serializer->hydrate($api, $data); |
127
|
|
|
|
128
|
|
|
// validate |
129
|
|
|
if (!$api->validate()) { |
130
|
|
|
return new NotValid([ |
131
|
|
|
'errors' => $api->getValidationFailures() |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$rows = $api->save(); |
136
|
|
|
$payload = ['model' => $api]; |
137
|
|
|
|
138
|
|
|
if ($rows === 0) { |
139
|
|
|
return new NotUpdated($payload); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return new Updated($payload); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Implement this functionality at keeko\core\domain\ApiDomain |
147
|
|
|
* |
148
|
|
|
* @param ApiQuery $query |
149
|
|
|
*/ |
150
|
|
|
abstract protected function applyFilter(ApiQuery $query); |
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the service container |
154
|
|
|
* |
155
|
|
|
* @return ServiceContainer |
156
|
|
|
*/ |
157
|
|
|
abstract protected function getServiceContainer(); |
158
|
|
|
} |
159
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.