src/Graviton/RestBundle/Service/RestUtils.php
public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $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']) {
                $e = new MalformedInputException('Record ID in your payload must be the same');
                $e->setResponse($response);
                throw $e;
            }
        }

        if ($request->getMethod() == 'POST' &&
            array_key_exists('id', $input) &&
            !$model->isIdInPostAllowed()
        ) {
            $e = new MalformedInputException(
                '"id" can not be given on a POST request. Do a PUT request instead to update an existing record.'
            );
            $e->setResponse($response);
            throw $e;
        }
    }
public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $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']) {
                $e = new MalformedInputException('Record ID in your payload must be the same');
                $e->setResponse($response);
                throw $e;
            }
        }

        if ($request->getMethod() == 'POST' &&
            array_key_exists('id', $input) &&
            !$model->isIdInPostAllowed()
        ) {
            $e = new MalformedInputException(
                '"id" can not be given on a POST request. Do a PUT request instead to update an existing record.'
            );
            $e->setResponse($response);
            throw $e;
        }
    }
public function checkJsonRequest ( Request $ request , Response $ response , DocumentModel $ model , $ 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' ] ) { $ e = new MalformedInputException ( 'Record ID in your payload must be the same' ) ; $ e -> setResponse ( $ response ) ; throw $ e ; } } if ( $ request -> getMethod ( ) == 'POST' && array_key_exists ( 'id' , $ input ) && ! $ model -> isIdInPostAllowed ( ) ) { $ e = new MalformedInputException ( '"id" can not be given on a POST request. Do a PUT request instead to update an existing record.' ) ; $ e -> setResponse ( $ response ) ; throw $ e ; } }
validate raw json input

@param Request       $request  request
@param Response      $response response
@param DocumentModel $model    model
@param string        $content  Alternative request content.

@return void
validate raw json input