49 lines
919 B
PHP
49 lines
919 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Altcha;
|
||
|
|
||
|
use App\Altcha\Form\AltchaModel;
|
||
|
use Symfony\Component\Form\DataTransformerInterface;
|
||
|
|
||
|
class AltchaTransformer implements DataTransformerInterface
|
||
|
{
|
||
|
/**
|
||
|
* {@inheritDoc}
|
||
|
*/
|
||
|
public function reverseTransform($value): AltchaModel
|
||
|
{
|
||
|
if (empty($value)) {
|
||
|
return new AltchaModel();
|
||
|
}
|
||
|
|
||
|
$decodedValue = base64_decode($value);
|
||
|
$data = json_decode($decodedValue);
|
||
|
|
||
|
$model = new AltchaModel();
|
||
|
|
||
|
foreach ($data as $property => $value) {
|
||
|
$model->{$property} = $value;
|
||
|
}
|
||
|
|
||
|
return $model;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* {@inheritDoc}
|
||
|
*/
|
||
|
public function transform($value): string
|
||
|
{
|
||
|
if (empty($value)) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
$json = json_encode($value);
|
||
|
|
||
|
if (false === $json) {
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
return base64_encode($json);
|
||
|
}
|
||
|
}
|