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