You are here

public function JsonResponse::setData in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-foundation/JsonResponse.php \Symfony\Component\HttpFoundation\JsonResponse::setData()

Sets the data to be sent as JSON.

Parameters

mixed $data:

Return value

JsonResponse

Throws

\InvalidArgumentException

2 calls to JsonResponse::setData()
JsonResponse::setEncodingOptions in vendor/symfony/http-foundation/JsonResponse.php
Sets options used while encoding data to JSON.
JsonResponse::__construct in vendor/symfony/http-foundation/JsonResponse.php
Constructor.

File

vendor/symfony/http-foundation/JsonResponse.php, line 96

Class

JsonResponse
Response represents an HTTP response in JSON format.

Namespace

Symfony\Component\HttpFoundation

Code

public function setData($data = array()) {
  if (defined('HHVM_VERSION')) {

    // HHVM does not trigger any warnings and let exceptions
    // thrown from a JsonSerializable object pass through.
    // If only PHP did the same...
    $data = json_encode($data, $this->encodingOptions);
  }
  else {
    try {
      if (PHP_VERSION_ID < 50400) {

        // PHP 5.3 triggers annoying warnings for some
        // types that can't be serialized as JSON (INF, resources, etc.)
        // but doesn't provide the JsonSerializable interface.
        set_error_handler('var_dump', 0);
        $data = @json_encode($data, $this->encodingOptions);
      }
      else {

        // PHP 5.4 and up wrap exceptions thrown by JsonSerializable
        // objects in a new exception that needs to be removed.
        // Fortunately, PHP 5.5 and up do not trigger any warning anymore.
        if (PHP_VERSION_ID < 50500) {

          // Clear json_last_error()
          json_encode(null);
          $errorHandler = set_error_handler('var_dump');
          restore_error_handler();
          set_error_handler(function () use ($errorHandler) {
            if (JSON_ERROR_NONE === json_last_error()) {
              return $errorHandler && false !== call_user_func_array($errorHandler, func_get_args());
            }
          });
        }
        $data = json_encode($data, $this->encodingOptions);
      }
      if (PHP_VERSION_ID < 50500) {
        restore_error_handler();
      }
    } catch (\Exception $e) {
      if (PHP_VERSION_ID < 50500) {
        restore_error_handler();
      }
      if (PHP_VERSION_ID >= 50400 && 'Exception' === get_class($e) && 0 === strpos($e
        ->getMessage(), 'Failed calling ')) {
        throw $e
          ->getPrevious() ?: $e;
      }
      throw $e;
    }
  }
  if (JSON_ERROR_NONE !== json_last_error()) {
    throw new \InvalidArgumentException($this
      ->transformJsonError());
  }
  $this->data = $data;
  return $this
    ->update();
}