|
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
|
|
|
use Ubiquity\utils\http\URequest; |
|
10
|
|
|
use Ubiquity\orm\OrmUtils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Rest JsonAPI implementation. |
|
14
|
|
|
* Ubiquity\controllers\rest\api\jsonapi$JsonApiRestController |
|
15
|
|
|
* This class is part of Ubiquity |
|
16
|
|
|
* |
|
17
|
|
|
* @author jcheron <[email protected]> |
|
18
|
|
|
* @version 1.0.0 |
|
19
|
|
|
* @since Ubiquity 2.0.11 |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class JsonApiRestController extends RestBaseController { |
|
23
|
|
|
const API_VERSION = 'JsonAPI 1.0'; |
|
24
|
|
|
|
|
25
|
1 |
|
protected function _setResource($resource) { |
|
26
|
1 |
|
$modelsNS = $this->config ["mvcNS"] ["models"]; |
|
27
|
1 |
|
$this->model = $modelsNS . "\\" . ucfirst ( $resource ); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
protected function _checkResource($resource, $callback) { |
|
31
|
1 |
|
$this->_setResource ( $resource ); |
|
32
|
1 |
|
if (class_exists ( $this->model )) { |
|
33
|
1 |
|
$callback (); |
|
34
|
|
|
} else { |
|
35
|
|
|
$error = new RestError ( 404, "Not existing class", $this->model . " class does not exists!", Startup::getController () . "/" . Startup::getAction () ); |
|
36
|
|
|
echo $this->_format ( $error->asArray () ); |
|
37
|
|
|
} |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
protected function getRequestParam($param, $default) { |
|
41
|
1 |
|
if (isset ( $_GET [$param] )) { |
|
42
|
|
|
return $_GET [$param]; |
|
43
|
|
|
} |
|
44
|
1 |
|
return $default; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function getDatas() { |
|
48
|
|
|
$datas = URequest::getInput (); |
|
49
|
|
|
if (sizeof ( $datas ) > 0) { |
|
50
|
|
|
$datas = current ( array_keys ( $datas ) ); |
|
51
|
|
|
$datas = json_decode ( $datas, true ); |
|
52
|
|
|
$attributes = $datas ["data"] ["attributes"] ?? [ ]; |
|
53
|
|
|
if (isset ( $datas ["id"] )) { |
|
54
|
|
|
$key = OrmUtils::getFirstKey ( $this->model ); |
|
55
|
|
|
$attributes [$key] = $datas ["id"]; |
|
56
|
|
|
} |
|
57
|
|
|
return $attributes; |
|
58
|
|
|
} |
|
59
|
|
|
$this->addError ( 204, "No content", 'The POST request has no content!' ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* @route("{resource}/","methods"=>["options"]) |
|
65
|
|
|
*/ |
|
66
|
|
|
public function options($resource) { |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns all the instances from the model $resource. |
|
71
|
|
|
* Query parameters: |
|
72
|
|
|
* - **included**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true). |
|
73
|
|
|
* - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1). |
|
74
|
|
|
* - **page[number]**: The page to display (in this case, the page size is set to 1). |
|
75
|
|
|
* - **page[size]**: The page size (count of instance per page) (default: 1). |
|
76
|
|
|
* |
|
77
|
|
|
* @route("{resource}/","methods"=>["get"],"priority"=>0) |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getAll_($resource) { |
|
80
|
|
|
$this->_checkResource ( $resource, function () { |
|
81
|
1 |
|
$filter = $this->getRequestParam ( 'filter', '1=1' ); |
|
82
|
1 |
|
$pages = null; |
|
83
|
1 |
|
if (isset ( $_GET ['page'] )) { |
|
84
|
|
|
$pageNumber = $_GET ['page'] ['number']; |
|
85
|
|
|
$pageSize = $_GET ['page'] ['size'] ?? 1; |
|
86
|
|
|
$pages = $this->generatePagination ( $filter, $pageNumber, $pageSize ); |
|
87
|
|
|
} |
|
88
|
1 |
|
$datas = DAO::getAll ( $this->model, $filter, $this->getIncluded ( $this->getRequestParam ( 'included', true ) ) ); |
|
89
|
1 |
|
echo $this->_getResponseFormatter ()->get ( $datas, $pages ); |
|
90
|
1 |
|
} ); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns an instance of $resource, by primary key $id. |
|
95
|
|
|
* Query parameters: |
|
96
|
|
|
* - **included**: A string of associated members to load, comma separated (e.g. users,groups,organization...), or a boolean: true for all members, false for none (default: true). |
|
97
|
|
|
* - **filter**: The filter to apply to the query (where part of an SQL query) (default: 1=1). |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $resource |
|
100
|
|
|
* The resource (model) to use |
|
101
|
|
|
* @param string $id |
|
102
|
|
|
* The primary key value(s), if the primary key is composite, use a comma to separate the values (e.g. 1,115,AB) |
|
103
|
|
|
* |
|
104
|
|
|
* @route("{resource}/{id}/","methods"=>["get"],"priority"=>1000) |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getOne_($resource, $id) { |
|
107
|
|
|
$this->_checkResource ( $resource, function () use ($id) { |
|
108
|
1 |
|
$this->_getOne ( $id, true, false ); |
|
109
|
1 |
|
} ); |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Inserts a new instance of $resource. |
|
114
|
|
|
* Data attributes are send in data[attributes] request header |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $resource |
|
117
|
|
|
* The resource (model) to use |
|
118
|
|
|
* @route("{resource}/","methods"=>["post"],"priority"=>0) |
|
119
|
|
|
*/ |
|
120
|
|
|
public function add_($resource) { |
|
121
|
|
|
$this->_checkResource ( $resource, function () { |
|
122
|
|
|
parent::add (); |
|
123
|
|
|
} ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the api version |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public static function _getApiVersion() { |
|
132
|
1 |
|
return self::API_VERSION; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.