Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DynamicResourceTrait.php
Go to the documentation of this file.
1<?php
2
4
10
21
22 abstract protected function _getResponseFormatter();
23
24 abstract public function _setResponseCode($value);
25
26 abstract public function _format($arrayMessage);
27
28 abstract public function _getAll();
29
30 abstract public function _getRelationShip($id, $member);
31
32 abstract public function _getOne($keyValues, $include = false, $useCache = false);
33
34 abstract protected function getRequestParam($param, $default);
35
36 abstract protected function hasErrors();
37
38 abstract protected function displayErrors();
39
40 abstract public function _delete(...$keyValues);
41
42 protected function updateOperation($instance, $datas, $updateMany = false) {
43 $instance->_new = false;
44 return CRUDHelper::update ( $instance, $datas, false, $updateMany );
45 }
46
47 protected function addOperation($instance, $datas, $insertMany = false) {
48 $instance->_new = true;
49 return CRUDHelper::update ( $instance, $datas, false, $insertMany );
50 }
51
52 protected function _setResource($resource) {
53 $modelsNS = Startup::getNS('models');
54 $this->model = $modelsNS . $this->_getResponseFormatter ()->getModel ( $resource );
55 }
56
57 protected function _checkResource($resource, $callback) {
58 $this->_setResource ( $resource );
59 if (\class_exists ( $this->model )) {
60 $callback ();
61 } else {
62 $this->_setResponseCode ( 404 );
63 $error = new RestError ( 404, "Not existing class", $this->model . " class does not exists!", Startup::getController () . "/" . Startup::getAction () );
64 echo $this->_format ( $error->asArray () );
65 }
66 }
67
71 public function getAll_($resource) {
72 $this->_checkResource ( $resource, function () {
73 $this->_getAll ();
74 } );
75 }
76
86 public function getRelationShip_($resource, $id, $member) {
87 $this->_checkResource ( $resource, function () use ($id, $member) {
88 $this->_getRelationShip ( $id, $member );
89 } );
90 }
91
99 public function getOne_($resource, $id) {
100 $this->_checkResource ( $resource, function () use ($id) {
101 $this->_getOne ( $id, $this->getRequestParam ( 'include', false ), false );
102 } );
103 }
104
111 public function add_($resource) {
112 $this->_checkResource ( $resource, function () {
113 parent::_add ();
114 } );
115 }
116
124 public function update_($resource, ...$id) {
125 $this->_checkResource ( $resource, function () use ($id) {
126 if (! $this->hasErrors ()) {
127 parent::_update ( ...$id );
128 } else {
129 echo $this->displayErrors ();
130 }
131 } );
132 }
133
141 public function delete_($resource, ...$id) {
142 $this->_checkResource ( $resource, function () use ($id) {
143 $this->_delete ( ...$id );
144 } );
145 }
146}
Starts the framework.
Definition Startup.php:19
static getController()
Returns the active controller name.
Definition Startup.php:291
static getAction()
Returns tha active action.
Definition Startup.php:318
Ubiquity\controllers\crud$CRUDHelper This class is part of Ubiquity.
getOne_($resource, $id)
Returns an instance of $resource, by primary key $id.
getAll_($resource)
Returns all the instances from the model $resource.
_getOne($keyValues, $include=false, $useCache=false)
add_($resource)
Inserts a new instance of $resource.
updateOperation($instance, $datas, $updateMany=false)
update_($resource,... $id)
Updates an existing instance of $resource.
delete_($resource,... $id)
Deletes an existing instance of $resource.
getRelationShip_($resource, $id, $member)
Returns an associated member value(s).
Gateway class between database and object model.
Definition DAO.php:33
Object/relational mapping utilities.
Definition OrmUtils.php:17