diff --git a/docs/book/v2/migration/preparing-for-v3.md b/docs/book/v2/migration/preparing-for-v3.md index 6787e769c..284ac670f 100644 --- a/docs/book/v2/migration/preparing-for-v3.md +++ b/docs/book/v2/migration/preparing-for-v3.md @@ -30,7 +30,7 @@ try { } ``` -## Deprecations +## Deprecations ### Undocumented Behaviour diff --git a/docs/book/v3/helpers/json.md b/docs/book/v3/helpers/json.md deleted file mode 100644 index 655a9f788..000000000 --- a/docs/book/v3/helpers/json.md +++ /dev/null @@ -1,21 +0,0 @@ -# Json - -When creating views that return JSON, it's important to also set the appropriate -response header. The JSON view helper does exactly that. In addition, by -default, it disables layouts (if currently enabled), as layouts generally aren't -used with JSON responses. - -The JSON helper sets the following header: - -```http -Content-Type: application/json -``` - -Most XmlHttpRequest libraries look for this header when parsing responses to -determine how to handle the content. - -## Basic Usage - -```php -json($this->data) ?> -``` diff --git a/docs/book/v3/migration/v2-to-v3.md b/docs/book/v3/migration/v2-to-v3.md index 25b17c54f..c6eae30ec 100644 --- a/docs/book/v3/migration/v2-to-v3.md +++ b/docs/book/v3/migration/v2-to-v3.md @@ -48,11 +48,6 @@ These helpers now have constructors that expect an [Escaper](https://docs.lamina The encoding defaults to UTF-8 as it has always done but can be overridden in configuration by setting `view_manager.encoding` to your preferred value. -#### Json View Helper - -In previous versions of laminas-view the [Json View Helper](helpers/json.md) made use of the [laminas-json](https://docs.laminas.dev/laminas-json/) library which enabled encoding of [JSON Expressions](https://docs.laminas.dev/laminas-json/advanced/#json-expressions). -Support for this library and the expression finder feature has been removed. - ## Removed Class and Traits ### Removed Helpers @@ -98,3 +93,11 @@ function gravatarImage( string $rating = 'g' ); ``` + +#### Json + +The deprecated Json view helper has been removed. +To encode data to Json for output in a view, you can call [`json_encode`](https://www.php.net/json_encode) directly. + +If you were relying on behaviour that was previously available via `laminas-json`, for example, calling object methods `toArray` or `toJson` prior to encoding, you should make the relevant objects implement `JsonSerializable`. +You can find documentation on the `JsonSerializable` interface [on the PHP website](https://www.php.net/manual/class.jsonserializable.php). diff --git a/mkdocs.yml b/mkdocs.yml index d0f255355..52687be90 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -65,7 +65,6 @@ nav: - HtmlTag: v3/helpers/html-tag.md - Identity: v3/helpers/identity.md - InlineScript: v3/helpers/inline-script.md - - Json: v3/helpers/json.md - Layout: v3/helpers/layout.md - Partial: v3/helpers/partial.md - Placeholder: v3/helpers/placeholder.md @@ -115,7 +114,6 @@ plugins: helpers/html-tag.md: v3/helpers/html-tag.md helpers/identity.md: v3/helpers/identity.md helpers/inline-script.md: v3/helpers/inline-script.md - helpers/json.md: v3/helpers/json.md helpers/layout.md: v3/helpers/layout.md helpers/partial.md: v3/helpers/partial.md helpers/placeholder.md: v3/helpers/placeholder.md diff --git a/src/Helper/Json.php b/src/Helper/Json.php deleted file mode 100644 index 542119922..000000000 --- a/src/Helper/Json.php +++ /dev/null @@ -1,75 +0,0 @@ -= 2.20.0 - * - * @var Response|null - */ - protected $response; - - /** - * Encode data as JSON and set response header - * - * @param mixed $data - * @param array{prettyPrint?: bool} $jsonOptions Options to pass to JsonFormatter::encode() - * @return string - */ - public function __invoke($data, array $jsonOptions = []) - { - $data = json_encode($data, $this->optionsToFlags($jsonOptions)); - - if ($this->response instanceof Response) { - $headers = $this->response->getHeaders(); - $headers->addHeaderLine('Content-Type', 'application/json'); - } - - return $data; - } - - /** @param array{prettyPrint?: bool} $options */ - private function optionsToFlags(array $options = []): int - { - $prettyPrint = $options['prettyPrint'] ?? false; - $flags = JSON_THROW_ON_ERROR; - $flags |= $prettyPrint ? 0 : JSON_PRETTY_PRINT; - - return $flags; - } - - /** - * Set the response object - * - * @deprecated since >= 2.20.0. If you need to set response headers, use the methods available in - * the framework. For example in Laminas MVC this can be achieved in the controller or in - * Mezzio, you can change response headers in Middleware. This method will be removed in 3.0 - * without replacement functionality. - * - * @return Json - */ - public function setResponse(Response $response) - { - $this->response = $response; - return $this; - } -} diff --git a/src/HelperPluginManager.php b/src/HelperPluginManager.php index bbadf35a4..7a01f7f70 100644 --- a/src/HelperPluginManager.php +++ b/src/HelperPluginManager.php @@ -111,8 +111,6 @@ class HelperPluginManager extends AbstractPluginManager 'inlinescript' => Helper\InlineScript::class, 'inlineScript' => Helper\InlineScript::class, 'InlineScript' => Helper\InlineScript::class, - 'json' => Helper\Json::class, - 'Json' => Helper\Json::class, 'layout' => Helper\Layout::class, 'Layout' => Helper\Layout::class, 'paginationcontrol' => Helper\PaginationControl::class, @@ -181,7 +179,6 @@ class HelperPluginManager extends AbstractPluginManager Helper\HtmlObject::class => InvokableFactory::class, Helper\HtmlPage::class => InvokableFactory::class, Helper\InlineScript::class => InvokableFactory::class, - Helper\Json::class => InvokableFactory::class, Helper\Layout::class => InvokableFactory::class, Helper\PaginationControl::class => InvokableFactory::class, Helper\PartialLoop::class => InvokableFactory::class, @@ -217,7 +214,6 @@ class HelperPluginManager extends AbstractPluginManager 'laminasviewhelperhtmlobject' => InvokableFactory::class, 'laminasviewhelperhtmlpage' => InvokableFactory::class, 'laminasviewhelperinlinescript' => InvokableFactory::class, - 'laminasviewhelperjson' => InvokableFactory::class, 'laminasviewhelperlayout' => InvokableFactory::class, 'laminasviewhelperpaginationcontrol' => InvokableFactory::class, 'laminasviewhelperpartialloop' => InvokableFactory::class, diff --git a/src/Renderer/PhpRenderer.php b/src/Renderer/PhpRenderer.php index 271cf33da..050dc29bf 100644 --- a/src/Renderer/PhpRenderer.php +++ b/src/Renderer/PhpRenderer.php @@ -69,7 +69,6 @@ * @method string htmlPage($data, array $attribs = array(), array $params = array(), $content = null) * @method mixed|null identity() * @method \Laminas\View\Helper\InlineScript inlineScript($mode = \Laminas\View\Helper\HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript') - * @method string|void json($data, array $jsonOptions = array()) * @method \Laminas\View\Helper\Layout layout($template = null) * @method \Laminas\View\Helper\Navigation navigation($container = null) * @method string paginationControl(\Laminas\Paginator\Paginator $paginator = null, $scrollingStyle = null, $partial = null, $params = null) diff --git a/test/Helper/JsonTest.php b/test/Helper/JsonTest.php deleted file mode 100644 index b829abf73..000000000 --- a/test/Helper/JsonTest.php +++ /dev/null @@ -1,56 +0,0 @@ -response = new Response(); - $this->helper = new JsonHelper(); - $this->helper->setResponse($this->response); - } - - private function verifyJsonHeader(): void - { - $headers = $this->response->getHeaders(); - $this->assertTrue($headers->has('Content-Type')); - $header = $headers->get('Content-Type'); - self::assertInstanceOf(HeaderInterface::class, $header); - $this->assertEquals('application/json', $header->getFieldValue()); - } - - public function testJsonHelperSetsResponseHeader(): void - { - $this->helper->__invoke('foobar'); - $this->verifyJsonHeader(); - } - - public function testJsonHelperReturnsJsonEncodedString(): void - { - $input = [ - 'dory' => 'blue', - 'nemo' => 'orange', - ]; - $expect = json_encode($input, JSON_THROW_ON_ERROR); - self::assertJsonStringEqualsJsonString($expect, ($this->helper)($input)); - } -}