Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
CRAP
95.24% covered (success)
95.24%
20 / 21
StudentConventionTransformer
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
11
95.24% covered (success)
95.24%
20 / 21
 transform
0.00% covered (danger)
0.00%
0 / 1
5.01
91.67% covered (success)
91.67%
11 / 12
 reverseTransform
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
9 / 9
<?php
/*
 * This file is part of the Incipio package.
 *
 * (c) Théo FIDRY <theo.fidry@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace FrontBundle\Form\DataTransformer;
use FrontBundle\Utils\IriHelper;
use Symfony\Component\Form\DataTransformerInterface;
/**
 * @link http://symfony.com/doc/current/cookbook/form/data_transformers.html#about-model-and-view-transformers
 *
 * @author Théo FIDRY <theo.fidry@gmail.com>
 */
class StudentConventionTransformer implements DataTransformerInterface
{
    /**
     * API URI for the StudentConvention. Is put as a constant here to avoid to have to inject the router service to
     * generate it. This would indeed result in declaring those forms as services which is quite heavy and ugly.
     */
    const API_URI_PREFIX = '/api/student_conventions';
    /**
     * {@inheritdoc}
     */
    public function transform($value)
    {
        if (null === $value) {
            return;
        }
        if (isset($value['@id'])) {
            $value['@id'] = IriHelper::extractId($value['@id']);
        }
        if (isset($value['dateOfSignature'])) {
            $value['dateOfSignature'] = (true === empty($value['dateOfSignature']))
                ? null
                : new \DateTime($value['dateOfSignature'])
            ;
        }
        return $value;
    }
    /**
     * {@inheritdoc}
     */
    public function reverseTransform($value)
    {
        if (null === $value) {
            return;
        }
        if (isset($value['@id']) && false === empty($value['@id'])) {
            $value['@id'] = sprintf('%s/%s', self::API_URI_PREFIX, IriHelper::extractId($value['@id']));
        }
        if (isset($value['dateOfSignature']) && $value['dateOfSignature'] instanceof \DateTime) {
            $value['dateOfSignature'] = $value['dateOfSignature']->format('Y-m-d\TH:i:sP');
        }
        return $value;
    }
}