create generic data transformer with get an object by id, and add test

for CalendarType
This commit is contained in:
2022-05-20 19:23:47 +02:00
parent b6e0379583
commit 67fad5d764
11 changed files with 422 additions and 159 deletions

View File

@@ -11,65 +11,13 @@ 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;
use Chill\MainBundle\Form\DataTransformer\IdToEntityDataTransformer;
use Chill\PersonBundle\Repository\PersonRepository;
class PersonToIdTransformer implements DataTransformerInterface
class PersonToIdTransformer extends IdToEntityDataTransformer
{
/**
* @var ObjectManager
*/
private $om;
public function __construct(ObjectManager $om)
public function __construct(PersonRepository $repository)
{
$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();
parent::__construct($repository, false);
}
}

View File

@@ -0,0 +1,23 @@
<?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\MainBundle\Form\DataTransformer\IdToEntityDataTransformer;
use Chill\PersonBundle\Repository\PersonRepository;
class PersonsToIdDataTransformer extends IdToEntityDataTransformer
{
public function __construct(PersonRepository $repository)
{
parent::__construct($repository, true);
}
}