Esempio n. 1
0
 /**
  * validate raw json input
  *
  * @param Request  $request  request
  * @param Response $response response
  *
  * @return void
  */
 private function checkJsonRequest(Request $request, Response $response)
 {
     $content = $request->getContent();
     if (is_resource($content)) {
         throw new BadRequestHttpException('unexpected resource in validation');
     }
     // Decode the json from request
     if (!($input = json_decode($content, true)) && JSON_ERROR_NONE === json_last_error()) {
         $e = new NoInputException();
         $e->setResponse($response);
         throw $e;
     }
     // specially check for parse error ($input decodes to null) and report accordingly..
     if (is_null($input) && JSON_ERROR_NONE !== json_last_error()) {
         $e = new MalformedInputException($this->getLastJsonErrorMessage());
         $e->setErrorType(json_last_error());
         $e->setResponse($response);
         //$e->setResponse($event->getResponse());
         throw $e;
     }
     if ($request->getMethod() == 'PUT' && array_key_exists('id', $input)) {
         // we need to check for id mismatches....
         if ($request->attributes->get('id') != $input['id']) {
             throw new BadRequestHttpException('Record ID in your payload must be the same');
         }
     }
 }
Esempio n. 2
0
 /**
  * validate raw json input
  *
  * @param Request  $request  request
  * @param Response $response response
  * @param string   $content  Alternative request content.
  *
  * @return void
  */
 public function checkJsonRequest(Request $request, Response $response, $content = '')
 {
     if (empty($content)) {
         $content = $request->getContent();
     }
     if (is_resource($content)) {
         throw new BadRequestHttpException('unexpected resource in validation');
     }
     // is request body empty
     if ($content === '') {
         $e = new NoInputException();
         $e->setResponse($response);
         throw $e;
     }
     $input = json_decode($content, true);
     if (JSON_ERROR_NONE !== json_last_error()) {
         $e = new MalformedInputException($this->getLastJsonErrorMessage());
         $e->setErrorType(json_last_error());
         $e->setResponse($response);
         throw $e;
     }
     if (!is_array($input)) {
         $e = new MalformedInputException('JSON request body must be an object');
         $e->setResponse($response);
         throw $e;
     }
     if ($request->getMethod() == 'PUT' && array_key_exists('id', $input)) {
         // we need to check for id mismatches....
         if ($request->attributes->get('id') != $input['id']) {
             throw new BadRequestHttpException('Record ID in your payload must be the same');
         }
     }
 }