Julien Fastré a0392b9216 replace some entity shortcut by fqdn
* ChillPerson:Person
* ChillMain:Center
* ChillActivity:Activity
2022-04-30 00:20:18 +02:00

76 lines
1.6 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Form\DataTransformer;
use Chill\PersonBundle\Entity\Person;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class PersonToIdTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
* Transforms a string (id) to an object (issue).
*
* @param string $id
*
* @throws TransformationFailedException if object (issue) is not found.
*
* @return Person|null
*/
public function reverseTransform($id)
{
if (!$id) {
return null;
}
$issue = $this->om
->getRepository(\Chill\PersonBundle\Entity\Person::class)
->findOneBy(['id' => $id]);
if (null === $issue) {
throw new TransformationFailedException(sprintf(
'An issue with id "%s" does not exist!',
$id
));
}
return $issue;
}
/**
* Transforms an object (issue) to a string (id).
*
* @param Person|null $issue
*
* @return string
*/
public function transform($issue)
{
if (null === $issue) {
return '';
}
return $issue->getId();
}
}