You are here

public function JsonResponse::setCallback in Zircon Profile 8.0

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

Sets the JSONP callback.

Parameters

string|null $callback The JSONP callback or null to use none:

Return value

JsonResponse

Throws

\InvalidArgumentException When the callback name is not valid

File

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

Class

JsonResponse
Response represents an HTTP response in JSON format.

Namespace

Symfony\Component\HttpFoundation

Code

public function setCallback($callback = null) {
  if (null !== $callback) {

    // taken from http://www.geekality.net/2011/08/03/valid-javascript-identifier/
    $pattern = '/^[$_\\p{L}][$_\\p{L}\\p{Mn}\\p{Mc}\\p{Nd}\\p{Pc}\\x{200C}\\x{200D}]*+$/u';
    $parts = explode('.', $callback);
    foreach ($parts as $part) {
      if (!preg_match($pattern, $part)) {
        throw new \InvalidArgumentException('The callback name is not valid.');
      }
    }
  }
  $this->callback = $callback;
  return $this
    ->update();
}