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

Add option-parameter to pass to json_encode #1606

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
16 changes: 13 additions & 3 deletions Classes/ViewHelpers/Format/Json/EncodeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public function initializeArguments()
'string',
'A date() format for DateTime values to JSON-compatible values. NULL means JS UNIXTIME (time()*1000)'
);
$this->registerArgument(
'options',
'integer',
'Additional options to pass to json_encode, like JSON_PRETTY_PRINT',
false,
0
);
}

/**
Expand All @@ -97,8 +104,9 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
$preventRecursion = (boolean) $arguments['preventRecursion'];
$recursionMarker = $arguments['recursionMarker'];
$dateTimeFormat = $arguments['dateTimeFormat'];
$options = (integer) $arguments['options'];
static::$encounteredClasses = [];
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat);
$json = static::encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $options);
return $json;
}

Expand All @@ -108,10 +116,11 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
* @param boolean $preventRecursion
* @param string $recursionMarker
* @param string $dateTimeFormat
* @param integer $options
* @return string
* @throws Exception
*/
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat)
protected static function encodeValue($value, $useTraversableKeys, $preventRecursion, $recursionMarker, $dateTimeFormat, $options)
{
if (true === $value instanceof \Traversable) {
// Note: also converts ObjectStorage to \Vendor\Extname\Domain\Model\ObjectType[] which are each converted
Expand All @@ -128,7 +137,8 @@ protected static function encodeValue($value, $useTraversableKeys, $preventRecur
$value = static::recursiveArrayOfDomainObjectsToArray($value, $preventRecursion, $recursionMarker);
$value = static::recursiveDateTimeToUnixtimeMiliseconds($value, $dateTimeFormat);
}
$json = json_encode($value, JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG);
$options |= JSON_HEX_AMP | JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG;
$json = json_encode($value, $options);
if (JSON_ERROR_NONE !== json_last_error()) {
ErrorUtility::throwViewHelperException('The provided argument cannot be converted into JSON.', 1358440181);
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/ViewHelpers/Format/Json/EncodeViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function encodesDateTime()
{
$dateTime = \DateTime::createFromFormat('U', 86400);
$instance = $this->createInstance();
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null);
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $dateTime, false, true, null, null, 0);
$this->assertEquals(86400000, $test);
}

Expand All @@ -48,7 +48,7 @@ public function encodesRecursiveDomainObject()
$object = $this->getInstanceOfFoo();
$object->setFoo($object);
$instance = $this->createInstance();
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $object, true, true, null, null);
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $object, true, true, null, null, 0);
$this->assertEquals('{"bar":"baz","children":[],"foo":null,"name":null,"pid":null,"uid":null}', $test);
}

Expand All @@ -75,7 +75,7 @@ public function encodesTraversable()
{
$traversable = $this->objectManager->get(ObjectStorage::class);
$instance = $this->createInstance();
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $traversable, false, true, null, null);
$test = $this->callInaccessibleMethod($instance, 'encodeValue', $traversable, false, true, null, null, 0);
$this->assertEquals('[]', $test);
}

Expand Down