Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate jsonExpressionFinder usage in Json view helper #97

Merged
merged 7 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/book/helpers/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,22 @@ determine how to handle the content.
<?= $this->json($this->data) ?>
```

> WARNING: **Deprecated**
>
> ### Enabling encoding using Laminas\Json\Expr
>
> **This feature of the Json view helper has been deprecated in version 2.16 and will be removed in version 3.0.**
>
> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and
> used internally to encode data.
> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr`
> objects. This option is disabled by default. To enable this option, pass a boolean `true` to the
> `enableJsonExprFinder` key of the options array:
>
> ```php
> <?= $this->json($this->data, ['enableJsonExprFinder' => true]) ?>
> ``
>
> The JSON helper accepts an array of options that will be passed to `Laminas\Json\Json::encode()` and
> used internally to encode data.
> `Laminas\Json\Json::encode` allows the encoding of native JSON expressions using `Laminas\Json\Expr`
Expand Down
11 changes: 11 additions & 0 deletions src/Helper/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Laminas\Http\Response;
use Laminas\Json\Json as JsonFormatter;

use function trigger_error;

use const E_USER_DEPRECATED;

/**
* Helper for simplifying JSON responses
*/
Expand All @@ -24,6 +28,13 @@ class Json extends AbstractHelper
*/
public function __invoke($data, array $jsonOptions = [])
{
if (isset($jsonOptions['enableJsonExprFinder']) && $jsonOptions['enableJsonExprFinder'] === true) {
trigger_error(
'Json Expression functionality is deprecated and will be removed in laminas-view 3.0',
E_USER_DEPRECATED
);
}

$data = JsonFormatter::encode($data, null, $jsonOptions);

if ($this->response instanceof Response) {
Expand Down
21 changes: 21 additions & 0 deletions test/Helper/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace LaminasTest\View\Helper;

use Laminas\Http\Header\HeaderInterface;
use Laminas\Http\Response;
use Laminas\Json\Json as JsonFormatter;
use Laminas\View\Helper\Json as JsonHelper;
use PHPUnit\Framework\TestCase;

use function assert;

/**
* Test class for Laminas\View\Helper\Json
*
Expand All @@ -15,6 +18,11 @@
*/
class JsonTest extends TestCase
{
/** @var Response */
private $response;
/** @var JsonHelper */
private $helper;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
Expand All @@ -31,6 +39,7 @@ public 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());
}

Expand All @@ -46,4 +55,16 @@ public function testJsonHelperReturnsJsonEncodedString(): void
$this->assertIsString($data);
$this->assertEquals('foobar', JsonFormatter::decode($data));
}

public function testThatADeprecationErrorIsTriggeredWhenExpressionFinderOptionIsUsed(): void
{
$this->expectDeprecation();
$this->helper->__invoke(['foo'], ['enableJsonExprFinder' => true]);
}

public function testThatADeprecationErrorIsNotTriggeredWhenExpressionFinderOptionIsNotUsed(): void
{
$this->expectNotToPerformAssertions();
$this->helper->__invoke(['foo'], ['enableJsonExprFinder' => 'anything other than true']);
}
}