Skip to content

Commit

Permalink
[FEATURE] Allow non cachable plugins to return null on empty
Browse files Browse the repository at this point in the history
  • Loading branch information
twoldanski committed May 10, 2023
1 parent 9a91db2 commit 6f9146f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function cObjGet(array $setup, string $addKey = ''): array
$content[$theKey] = (bool)$content[$theKey];
}
if ($theValue === 'USER_INT' || strpos((string)$content[$theKey], '<!--INT_SCRIPT.') === 0) {
$content[$theKey] = $this->headlessUserInt->wrap($content[$theKey]);
$content[$theKey] = $this->headlessUserInt->wrap($content[$theKey], (int)($conf['ifEmptyReturnNull'] ?? 0) === 1 ? HeadlessUserInt::STANDARD_NULLABLE : HeadlessUserInt::STANDARD);
}
if ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 && $content[$theKey] === '') {
$content[$theKey] = null;
Expand Down
82 changes: 53 additions & 29 deletions Classes/Utility/HeadlessUserInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,16 @@
use function preg_replace;
use function preg_replace_callback;
use function sprintf;
use function strpos;
use function substr;

class HeadlessUserInt
{
public const STANDARD = 'HEADLESS_INT';
public const NESTED = 'NESTED_HEADLESS_INT';
public const STANDARD_NULLABLE = 'HEADLESS_NULL_INT';
public const NESTED_NULLABLE = 'NESTED_HEADLESS_NULL_INT';
private const REGEX = '/("|)%s_START<<(.*?)>>%s_END("|)/s';

/**
* for use in preg_replace_callback
* to unwrap all HEADLESS_INT<<>>HEADLESS_INT blocks
*
* @param array<int, string> $input
* @return string
*/
private function replace(array $input): string
{
$content = $input[2];
if ($input[1] === $input[3] && $input[1] === '"') {
// have a look inside if it might be json already
$decoded = json_decode($content);

if ($decoded !== null) {
return $content;
}
return json_encode($content);
}

// trim one occurrence of double quotes at both ends
$jsonEncoded = json_encode($content);
if ($jsonEncoded[0] === '"' && $jsonEncoded[-1] === '"') {
$jsonEncoded = substr($jsonEncoded, 1, -1);
}
return $jsonEncoded;
}

public function wrap(string $content, string $type = self::STANDARD): string
{
return preg_replace(
Expand All @@ -73,10 +46,61 @@ public function unwrap(string $content): string
);
}

if (strpos($content, self::NESTED_NULLABLE) !== false) {
$content = preg_replace_callback(
sprintf(self::REGEX, self::NESTED_NULLABLE, self::NESTED_NULLABLE),
function (array $content) {
return $this->replace($content, true);
},
$content
);
}

if (strpos($content, self::STANDARD_NULLABLE) !== false) {
$content = preg_replace_callback(
sprintf(self::REGEX, self::STANDARD_NULLABLE, self::STANDARD_NULLABLE),
function (array $content) {
return $this->replace($content, true);
},
$content
);
}

return preg_replace_callback(
sprintf(self::REGEX, self::STANDARD, self::STANDARD),
[$this, 'replace'],
$content
);
}

/**
* for use in preg_replace_callback
* to unwrap all HEADLESS_INT<<>>HEADLESS_INT blocks
*
* @param array<int, string> $input
*/
private function replace(array $input, bool $returnNull = false): ?string
{
$content = $input[2];
if ($input[1] === $input[3] && $input[1] === '"') {
// have a look inside if it might be json already
$decoded = json_decode($content);

if ($decoded === null && $returnNull) {
return null;
}

if ($decoded !== null) {
return $content;
}
return json_encode($content);
}

// trim one occurrence of double quotes at both ends
$jsonEncoded = json_encode($content);
if ($jsonEncoded[0] === '"' && $jsonEncoded[-1] === '"') {
$jsonEncoded = substr($jsonEncoded, 1, -1);
}
return $jsonEncoded;
}
}

0 comments on commit 6f9146f

Please sign in to comment.