Skip to content

Commit d30acb8

Browse files
committed
Refactor Base Json View to auto pretty print flags
1 parent cbf6062 commit d30acb8

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Views/Base/Json.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ abstract class Json implements \BNETDocs\Interfaces\View
1313
*/
1414
public static function jsonFlags(): int
1515
{
16-
return \JSON_PRESERVE_ZERO_FRACTION
17-
| \JSON_THROW_ON_ERROR
18-
| (\php_sapi_name() == 'cli' || \CarlBennett\MVC\Libraries\Common::isBrowser(\getenv('HTTP_USER_AGENT')) ? \JSON_PRETTY_PRINT : 0);
16+
return \JSON_PRESERVE_ZERO_FRACTION | \JSON_THROW_ON_ERROR | self::prettyPrint();
1917
}
2018

2119
/**
@@ -27,4 +25,15 @@ public static function mimeType(): string
2725
{
2826
return \sprintf('%s;charset=utf-8', self::MIMETYPE_JSON);
2927
}
28+
29+
/**
30+
* Automatically passes the JSON_PRETTY_PRINT flag when using php-cli or when client is a browser.
31+
*
32+
* @return integer The JSON_PRETTY_PRINT flag or zero.
33+
*/
34+
private static function prettyPrint(): int
35+
{
36+
return (\php_sapi_name() == 'cli'
37+
|| \BNETDocs\Libraries\Core\StringProcessor::isBrowser() ? \JSON_PRETTY_PRINT : 0);
38+
}
3039
}

0 commit comments

Comments
 (0)