Beispiel #1
0
 /**
  * Default handling of PUT request.
  * Must be called explicitly in handlePut function.
  *
  * @param  \EchoIt\JsonApi\Request $request
  * @param  \EchoIt\JsonApi\Model   $model
  * @return \EchoIt\JsonApi\Model
  * @throws Exception
  */
 public function handlePutDefault(Request $request, $model)
 {
     if (empty($request->id)) {
         throw new Exception('No ID provided', static::ERROR_SCOPE | static::ERROR_NO_ID, BaseResponse::HTTP_BAD_REQUEST);
     }
     $updates = $this->parseRequestContent($request->content, $model->getResourceType());
     $model = $model::find($request->id);
     if (is_null($model)) {
         return null;
     }
     // fetch the original attributes
     $originalAttributes = $model->getOriginal();
     // apply our updates
     $model->fill($updates);
     // ensure we can get a succesful save
     if (!$model->save()) {
         throw new Exception('An unknown error occurred', static::ERROR_SCOPE | static::ERROR_UNKNOWN, BaseResponse::HTTP_INTERNAL_SERVER_ERROR);
     }
     // fetch the current attributes (post save)
     $newAttributes = $model->getAttributes();
     // loop through the new attributes, and ensure they are identical
     // to the original ones. if not, then we need to return the model
     foreach ($newAttributes as $attribute => $value) {
         if (!array_key_exists($attribute, $originalAttributes) || $value !== $originalAttributes[$attribute]) {
             $model->markChanged();
             break;
         }
     }
     return $model;
 }
Beispiel #2
0
 /**
  * A method for getting the proper HTTP status code for a successful request
  *
  * @param  string $method "PUT", "POST", "DELETE" or "GET"
  * @param  Model|null $model The model that a PUT request was executed against
  * @return int
  */
 public static function successfulHttpStatusCode($method, $model = null)
 {
     // if we did a put request, we need to ensure that the model wasn't
     // changed in other ways than those specified by the request
     //     Ref: http://jsonapi.org/format/#crud-updating-responses-200
     if (($method === 'PUT' || $method === 'PATCH') && $model instanceof Model) {
         // check if the model has been changed
         if ($model->isChanged()) {
             // return our response as if there was a GET request
             $method = 'GET';
         }
     }
     switch ($method) {
         case 'POST':
             return BaseResponse::HTTP_CREATED;
         case 'PUT':
         case 'PATCH':
         case 'DELETE':
             return BaseResponse::HTTP_NO_CONTENT;
         case 'GET':
             return BaseResponse::HTTP_OK;
     }
     // Code shouldn't reach this point, but if it does we assume that the
     // client has made a bad request, e.g. PATCH
     return BaseResponse::HTTP_BAD_REQUEST;
 }
Beispiel #3
0
 /**
  * Validates passed data against a model
  * Validation performed safely and only if model provides rules
  *
  * @param  \EchoIt\JsonApi\Model $model  model to validate against
  * @param  Array                 $values passed array of values
  *
  * @throws Exception\Validation          Exception thrown when validation fails
  *
  * @return Bool                          true if validation successful
  */
 protected function validateModelData(Model $model, array $values)
 {
     $validationResponse = $model->validateArray($values);
     if ($validationResponse === true) {
         return true;
     }
     throw new Exception\Validation('Bad Request', static::ERROR_SCOPE | static::ERROR_HTTP_METHOD_NOT_ALLOWED, BaseResponse::HTTP_BAD_REQUEST, $validationResponse);
 }