46 return $_GET [$param] ?? $default;
49 protected function operate_($instance, $callback, $status, $exceptionMessage, $keyValues) {
50 if (isset ( $instance )) {
51 $result = $callback ( $instance );
52 if ($result ==
true) {
54 echo $formatter->format ( [
"status" => $status,
"data" => $formatter->cleanRestObject ( $instance ) ] );
55 } elseif ($result ===
null) {
58 throw new \Exception ( $exceptionMessage );
62 echo $this->
_getResponseFormatter ()->format ( [
"message" =>
"No result found",
"keyValues" => $keyValues ] );
67 $count = DAO::count ( $this->model, $filter );
68 $pagesCount = \ceil ( $count / $pageSize );
69 $pages = [
'self' => $pageNumber,
'first' => 1,
'last' => $pagesCount,
'pageSize' => $pageSize ];
70 if ($pageNumber - 1 > 0) {
71 $pages [
'prev'] = $pageNumber - 1;
73 if ($pageNumber + 1 <= $pagesCount) {
74 $pages [
'next'] = $pageNumber + 1;
76 $offset = ($pageNumber - 1) * $pageSize;
77 $filter .=
' limit ' . $offset .
',' . $pageSize;
82 return DAO::update ( $instance, $updateMany );
85 protected function addOperation($instance, $datas, $insertMany =
false) {
86 return DAO::insert ( $instance, $insertMany );
94 if (! isset ( $this->responseFormatter )) {
97 return $this->responseFormatter;
114 if (! isset ( $this->requestFormatter )) {
115 $this->requestFormatter = $this->getRequestFormatter ();
117 return $this->requestFormatter;
130 if (! isset ( $this->server )) {
131 $this->server = $this->getRestServer ();
133 return $this->server;
154 if (URequest::isJSON ()) {
155 if (\is_string ( $values )) {
156 $values = \json_decode ( $values,
true );
159 $className = \get_class ( $instance );
160 $fieldsInRelationForUpdate = OrmUtils::getFieldsInRelationsForUpdate_ ( $className );
161 $manyToOneRelations = $fieldsInRelationForUpdate [
"manyToOne"];
163 $members = \array_keys ( $values );
164 OrmUtils::setFieldToMemberNames ( $members, $fieldsInRelationForUpdate [
"relations"] );
165 URequest::setValuesToObject ( $instance, $values );
166 if ($manyToOneRelations) {
167 $this->updateManyToOne ( $manyToOneRelations, $members, $className, $instance, $values );
171 protected function updateManyToOne($manyToOneRelations, $members, $className, $instance, $values) {
172 foreach ( $manyToOneRelations as $member ) {
173 if (\array_search ( $member, $members ) !==
false) {
174 $joinColumn = OrmUtils::getAnnotationInfoMember ( $className,
"#joinColumn", $member );
176 $fkClass = $joinColumn [
"className"];
177 $fkField = $joinColumn [
"name"];
178 if (isset ( $values [$fkField] )) {
179 $fkObject = DAO::getById ( $fkClass, $values [
"$fkField"] );
180 Reflexion::setMemberValue ( $instance, $member, $fkObject );
193 if (! UString::isBooleanStr ( $include )) {
194 return \explode (
',', $include );
196 return UString::isBooleanTrue ( $include );
199 protected function addError($code, $title, $detail =
null, $source =
null, $status =
null) {
200 $this->errors [] =
new RestError( $code, $title, $detail, $source, $status );
204 return \is_array ( $this->errors ) && \count ( $this->errors ) > 0;
208 if ($this->hasErrors ()) {
211 foreach ( $this->errors as $error ) {
212 $errors [] = $error->asArray ();
213 if ($error->getStatus () > $status) {
214 $status = $error->getStatus ();
217 echo $this->_getResponseFormatter ()->format ( [
'errors' => $errors ] );
218 $this->_setResponseCode ( $status );
235 $include = $this->getInclude ( $include );
236 $useCache = UString::isBooleanTrue ( $useCache );
237 $datas = $getDatas ( [ $this->model,$ids ], $member, $include, $useCache );
239 echo $this->_getResponseFormatter ()->get ( $datas );
241 echo $this->_getResponseFormatter ()->getOne ( $datas );
246 if ($this->useValidation) {
248 $violations = ValidatorsManager::validate ( $instance,
'', $excludedValidators );
249 foreach ( $violations as $violation ) {
250 if (\array_search ( $violation->getMember (), $members ) !==
false) {
251 $this->addViolation ( $violation );
265 $pks = OrmUtils::getKeyFields ( $model );
267 foreach ( $pks as $pk ) {
268 if (isset ( $datas [$pk] )) {
269 $result [] = $datas [$pk];
271 $this->addError ( 404,
'Primary key required',
'The primary key ' . $pk .
' is required!', $pk );
278 if (\is_array ( $condition )) {
280 foreach ( $condition as $f => $v ) {
281 $conds [] = $f .
"='" . \urldecode ( $v ) .
"'";
283 $condition = \implode (
' AND ', $conds );
285 $condition = \urldecode ( $condition );
287 if (\strpos ( $condition,
'like' ) !==
false) {
288 $condition = \str_replace (
'*',
'%', $condition );
Constraint Violation Generated During Validation with the ValidatorsManager.
getRequestParam($param, $default)
updateManyToOne($manyToOneRelations, $members, $className, $instance, $values)
getPrimaryKeysFromDatas($datas, $model)
generatePagination(&$filter, $pageNumber, $pageSize)
addOperation($instance, $datas, $insertMany=false)
getRestServer()
To override, returns the active RestServer.
updateOperation($instance, $datas, $updateMany=false)
_validateInstance($instance, $members, $excludedValidators=[])
getResponseFormatter()
To override, returns the active formatter for the response.
_setValuesToObject($instance, $values=[])
Updates $instance with $values To eventually be redefined in derived classes.
addViolation(ConstraintViolation $violation)
operate_($instance, $callback, $status, $exceptionMessage, $keyValues)
getAssociatedMemberValues_($ids, $getDatas, $member, $include=false, $useCache=false, $multiple=true)
addError($code, $title, $detail=null, $source=null, $status=null)
getRequestFormatter()
To override, returns the active formatter for the request.
Gateway class between database and object model.
Object/relational mapping utilities.
Reflection utilities in dev environment only.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.