setCallback example

/** * Sets validator options * * @param string|array $callback * @param mixed $max * @param boolean $inclusive * @return void */
    public function __construct($callback = null)
    {
        if (is_callable($callback)) {
            $this->setCallback($callback);
        } elseif (is_array($callback)) {
            if (isset($callback['callback'])) {
                $this->setCallback($callback['callback']);
            }
            if (isset($callback['options'])) {
                $this->setOptions($callback['options']);
            }
        }

        if (null === ($initializedCallack = $this->getCallback())) {
            throw new Zend_Validate_Exception('No callback registered');
        }
if (!empty($options)) {
                $temp['options'] = array_shift($options);
            }

            $options = $temp;
        }

        if (!array_key_exists('callback', $options)) {
            throw new Zend_Filter_Exception('Missing callback to use');
        }

        $this->setCallback($options['callback']);
        if (array_key_exists('options', $options)) {
            $this->setOptions($options['options']);
        }
    }

    /** * Returns the set callback * * @return string|array Set callback */
    public function getCallback()
    {
protected $streamed;
    private bool $headersSent;

    /** * @param int $status The HTTP status code (200 "OK" by default) */
    public function __construct(callable $callback = null, int $status = 200, array $headers = [])
    {
        parent::__construct(null, $status$headers);

        if (null !== $callback) {
            $this->setCallback($callback);
        }
        $this->streamed = false;
        $this->headersSent = false;
    }

    /** * Sets the PHP callback associated with this Response. * * @return $this */
    public function setCallback(callable $callback)static
    {
$twig = $this->container->get('twig');

        $callback = function D) use ($twig$view$parameters) {
            $twig->display($view$parameters);
        };

        if (null === $response) {
            return new StreamedResponse($callback);
        }

        $response->setCallback($callback);

        return $response;
    }

    /** * Returns a NotFoundHttpException. * * This will result in a 404 response code. Usage example: * * throw $this->createNotFoundException('Page not found!'); */
    
throw $e;
            }

            return $response = $this->handleThrowable($e$request$type);
        } finally {
            $this->requestStack->pop();

            if ($response instanceof StreamedResponse) {
                $callback = $response->getCallback();
                $requestStack = $this->requestStack;

                $response->setCallback(static function D) use ($request$callback$requestStack) {
                    $requestStack->push($request);
                    try {
                        $callback();
                    } finally {
                        $requestStack->pop();
                    }
                });
            }
        }
    }

    
throw $e;
            }

            return $response = $this->handleThrowable($e$request$type);
        } finally {
            $this->requestStack->pop();

            if ($response instanceof StreamedResponse) {
                $callback = $response->getCallback();
                $requestStack = $this->requestStack;

                $response->setCallback(static function D) use ($request$callback$requestStack) {
                    $requestStack->push($request);
                    try {
                        $callback();
                    } finally {
                        $requestStack->pop();
                    }
                });
            }
        }
    }

    
$response = new JsonResponse('[1]', 200, [], true);
        $this->assertEquals('[1]', $response->getContent());

        $response = new JsonResponse(null, 200, []);
        $response->setJson('true');
        $this->assertEquals('true', $response->getContent());
    }

    public function testSetCallback()
    {
        $response = (new JsonResponse(['foo' => 'bar']))->setCallback('callback');

        $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
        $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
    }

    public function testJsonEncodeFlags()
    {
        $response = new JsonResponse('<>\'&"');

        $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
    }

    
protected $streamed;
    private bool $headersSent;

    /** * @param int $status The HTTP status code (200 "OK" by default) */
    public function __construct(callable $callback = null, int $status = 200, array $headers = [])
    {
        parent::__construct(null, $status$headers);

        if (null !== $callback) {
            $this->setCallback($callback);
        }
        $this->streamed = false;
        $this->headersSent = false;
    }

    /** * Sets the PHP callback associated with this Response. * * @return $this */
    public function setCallback(callable $callback)static
    {
Home | Imprint | This part of the site doesn't use cookies.