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

@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Form\DataTransformer;
use Closure;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use function call_user_func;
/**
@@ -59,7 +60,13 @@ class IdToEntityDataTransformer implements DataTransformerInterface
return null;
}
return $this->repository->findOneBy(['id' => (int) $value]);
$object = $this->repository->findOneBy(['id' => (int) $value]);
if (null === $object) {
throw new TransformationFailedException('could not find any object by object id');
}
return $object;
}
/**
@@ -71,12 +78,22 @@ class IdToEntityDataTransformer implements DataTransformerInterface
$ids = [];
foreach ($value as $v) {
$ids[] = call_user_func($this->getId, $v);
$ids[] = $id = call_user_func($this->getId, $v);
if (null === $id) {
throw new TransformationFailedException('id is null');
}
}
return implode(',', $ids);
}
return call_user_func($this->getId, $value);
$id = call_user_func($this->getId, $value);
if (null === $id) {
throw new TransformationFailedException('id is null');
}
return (string) $id;
}
}

View File

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

View File

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

View File

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