| Current Path : /var/www/v3.cesa.co.za/src/CoreBundle/Form/DataTransformer/ |
| Current File : /var/www/v3.cesa.co.za/src/CoreBundle/Form/DataTransformer/ArrayToJsonTransformer.php |
<?php
namespace App\CoreBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class ArrayToJsonTransformer implements DataTransformerInterface {
/**
* Transforms an array of IDs to a json string.
*
* @param array|null $ids
* @return string
*/
public function reverseTransform($ids): string {
if ((null === $ids) || (!is_array($ids))) {
return json_encode(array());
}
return json_encode($ids);
}
/**
* Transforms a json string to an array of IDs
*
* @param string $jsonString
*
* @return array
*
* @throws TransformationFailedException if object (issue) is not found.
*/
public function transform($jsonString): array {
if (!$jsonString || strlen($jsonString) <= 0) {
return array();
}
$retVal = json_decode($jsonString, true);
if (!is_array($retVal) || is_null($retVal)) {
return array();
} else {
return $retVal;
}
}
}