mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
create generic data transformer with get an object by id, and add test
for CalendarType
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user