Verified Commit 4a681714 authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Json and base64 encoders

parent 8170070b
Pipeline #7135 canceled with stages
...@@ -30,7 +30,7 @@ namespace core\Encoding; ...@@ -30,7 +30,7 @@ namespace core\Encoding;
class Base64 class Base64
{ {
/** /**
* Encode stream * Decode stream
*/ */
public static function decode($value) public static function decode($value)
{ {
...@@ -45,6 +45,30 @@ class Base64 ...@@ -45,6 +45,30 @@ class Base64
fwrite($stream, base64_decode($chunk)); fwrite($stream, base64_decode($chunk));
} }
rewind($value);
rewind($stream);
return $stream;
}
}
/**
* Encode stream
*/
public static function encode($value)
{
if (is_scalar($value)) {
return base64_encode($value);
}
if (is_resource($value)) {
$stream = fopen('php://temp', 'w+');
while (!feof($value)) {
$chunk = fread($value, 1024*1024*2);
fwrite($stream, base64_encode($chunk));
}
rewind($value);
rewind($stream); rewind($stream);
return $stream; return $stream;
......
...@@ -77,7 +77,11 @@ class json ...@@ -77,7 +77,11 @@ class json
*/ */
public static function encode($data) public static function encode($data)
{ {
$jsonString = \json_encode($data, \JSON_PRETTY_PRINT + \JSON_UNESCAPED_SLASHES + \JSON_UNESCAPED_UNICODE); require_once __DIR__.'/JsonSerializer.php';
$serializer = new JsonSerializer();
//$jsonString = \json_encode($data, \JSON_PRETTY_PRINT + \JSON_UNESCAPED_SLASHES + \JSON_UNESCAPED_UNICODE);
$jsonString = $serializer($data, \JSON_PRETTY_PRINT + \JSON_UNESCAPED_SLASHES + \JSON_UNESCAPED_UNICODE);
if ($jsonString === false) { if ($jsonString === false) {
return false; return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment