<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Form\DataTransformer;

use App\Entity\UtilisateurRole;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\DataTransformerInterface;



class RolesTransformer implements DataTransformerInterface
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    /**
     * Transforms the numeric array (1,2,3,4) to a collection of Categories (Categories[])
     *
     * @param Array|null $roles
     * @return array
     */
    public function transform($role): ?array
    {
        $result = null;

        if (null === $role) {
            return $result;
        }

        return $this->entityManager
            ->getRepository(UtilisateurRole::class)
            ->findOneBy(["id" => $role])
            ;
    }

    /**
     * In this case, the reverseTransform can be empty.
     *
     * @param type $value
     * @return array
     */
    public function reverseTransform($value): ?array
    {
        return null;
    }
}
