|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\rest\api\jsonapi; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\DAO; |
|
6
|
|
|
use Ubiquity\controllers\rest\RestError; |
|
7
|
|
|
use Ubiquity\controllers\Startup; |
|
8
|
|
|
use Ubiquity\controllers\rest\RestBaseController; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Rest JsonAPI implementation |
|
12
|
|
|
* Ubiquity\controllers\rest\api\jsonapi$JsonApiRestController |
|
13
|
|
|
* This class is part of Ubiquity |
|
14
|
|
|
* |
|
15
|
|
|
* @author jcheron <[email protected]> |
|
16
|
|
|
* @version 1.0.0 |
|
17
|
|
|
* @since Ubiquity 2.0.11 |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class JsonApiRestController extends RestBaseController { |
|
21
|
|
|
const API_VERSION = 'JsonAPI 1.0'; |
|
22
|
|
|
|
|
23
|
|
|
protected function _setResource($resource) { |
|
24
|
|
|
$modelsNS = $this->config ["mvcNS"] ["models"]; |
|
25
|
|
|
$this->model = $modelsNS . "\\" . ucfirst ( $resource ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function _checkResource($resource, $callback) { |
|
29
|
|
|
$this->_setResource ( $resource ); |
|
30
|
|
|
if (class_exists ( $this->model )) { |
|
31
|
|
|
$callback (); |
|
32
|
|
|
} else { |
|
33
|
|
|
$error = new RestError ( 404, "Not existing class", $this->model . " class does not exists!", Startup::getController () . "/" . Startup::getAction () ); |
|
34
|
|
|
echo $this->_format ( $error->asArray () ); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function getRequestParam($param, $default) { |
|
39
|
|
|
if (isset ( $_GET [$param] )) { |
|
40
|
|
|
return $_GET [$param]; |
|
41
|
|
|
} |
|
42
|
|
|
return $default; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* @route("{resource}/","methods"=>["get"],"priority"=>0) |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getAll_($resource) { |
|
50
|
|
|
$this->_checkResource ( $resource, function () { |
|
51
|
|
|
$filter = $this->getRequestParam ( 'filter', '1=1' ); |
|
52
|
|
|
$pages = null; |
|
53
|
|
|
if (isset ( $_GET ['page'] )) { |
|
54
|
|
|
$pageNumber = $_GET ['page'] ['number']; |
|
55
|
|
|
$pageSize = $_GET ['page'] ['size'] ?? 1; |
|
56
|
|
|
$pages = $this->generatePagination ( $filter, $pageNumber, $pageSize ); |
|
57
|
|
|
} |
|
58
|
|
|
$datas = DAO::getAll ( $this->model, $filter, $this->getIncluded ( $this->getRequestParam ( 'included', true ) ) ); |
|
59
|
|
|
echo $this->_getResponseFormatter ()->get ( $datas, $pages ); |
|
60
|
|
|
} ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* @route("{resource}/{id}/","methods"=>["get"],"priority"=>1000) |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getOne_($resource, $id) { |
|
68
|
|
|
$this->_checkResource ( $resource, function () use ($id) { |
|
69
|
|
|
$this->_getOne ( $id, true, false ); |
|
70
|
|
|
} ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function _getApiVersion() { |
|
74
|
|
|
return self::API_VERSION; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|