Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Converter;
|
||||
|
||||
use Xentral\Modules\Api\Converter\Exception\ConvertionException;
|
||||
|
||||
class JsonConverter implements ConverterInterface
|
||||
{
|
||||
/**
|
||||
* Array zu JSON
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fromArray($array)
|
||||
{
|
||||
$data = json_encode($array);
|
||||
|
||||
if ($data === false || json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new ConvertionException('JSON could not be encoded.');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON zu Array
|
||||
*
|
||||
* @param string $json
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($json)
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw new ConvertionException('JSON could not be decoded.');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user