mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
fix cs and add EntityToIdTransformer
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?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 Closure;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use function call_user_func;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
class IdToEntityDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
private Closure $getId;
|
||||
|
||||
private bool $multiple = false;
|
||||
|
||||
private ObjectRepository $repository;
|
||||
|
||||
/**
|
||||
* @param Closure $getId
|
||||
*/
|
||||
public function __construct(ObjectRepository $repository, bool $multiple = false, ?callable $getId = null)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->multiple = $multiple;
|
||||
$this->getId = $getId ?? static function (object $o) { return $o->getId(); };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return array|object[]|T[]|T|object
|
||||
*/
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if ($this->multiple) {
|
||||
if (null === $value | '' === $value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
fn (string $id): ?object => $this->repository->findOneBy(['id' => (int) $id]),
|
||||
explode(',', $value)
|
||||
);
|
||||
}
|
||||
|
||||
if (null === $value | '' === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->repository->findOneBy(['id' => (int) $value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object|T|object[]|T[] $value
|
||||
*/
|
||||
public function transform($value): string
|
||||
{
|
||||
if ($this->multiple) {
|
||||
$ids = [];
|
||||
|
||||
foreach ($value as $v) {
|
||||
$ids[] = call_user_func($this->getId, $v);
|
||||
}
|
||||
|
||||
return implode(',', $ids);
|
||||
}
|
||||
|
||||
return call_user_func($this->getId, $value);
|
||||
}
|
||||
}
|
@@ -13,7 +13,6 @@ namespace Chill\MainBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\Location;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
|
@@ -0,0 +1,118 @@
|
||||
<?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 Form\DataTransformer;
|
||||
|
||||
use Chill\MainBundle\Form\DataTransformer\IdToEntityDataTransformer;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class IdToEntityDataTransformerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testReverseTransformMulti()
|
||||
{
|
||||
$o1 = new stdClass();
|
||||
$o2 = new stdClass();
|
||||
|
||||
$repository = $this->prophesize(ObjectRepository::class);
|
||||
$repository->findOneBy(Argument::exact(['id' => 1]))->willReturn($o1);
|
||||
$repository->findOneBy(Argument::exact(['id' => 2]))->willReturn($o2);
|
||||
|
||||
$transformer = new IdToEntityDataTransformer(
|
||||
$repository->reveal(),
|
||||
true
|
||||
);
|
||||
|
||||
$this->assertEquals([], $transformer->reverseTransform(null));
|
||||
$this->assertEquals([], $transformer->reverseTransform(''));
|
||||
$r = $transformer->reverseTransform('1,2');
|
||||
|
||||
$this->assertIsArray($r);
|
||||
$this->assertSame($o1, $r[0]);
|
||||
$this->assertSame($o2, $r[1]);
|
||||
}
|
||||
|
||||
public function testReverseTransformSingle()
|
||||
{
|
||||
$o = new stdClass();
|
||||
|
||||
$repository = $this->prophesize(ObjectRepository::class);
|
||||
$repository->findOneBy(Argument::exact(['id' => 1]))->willReturn($o);
|
||||
|
||||
$transformer = new IdToEntityDataTransformer(
|
||||
$repository->reveal(),
|
||||
false
|
||||
);
|
||||
|
||||
$this->assertEquals(null, $transformer->reverseTransform(null));
|
||||
$this->assertEquals(null, $transformer->reverseTransform(''));
|
||||
$r = $transformer->reverseTransform('1');
|
||||
|
||||
$this->assertSame($o, $r);
|
||||
}
|
||||
|
||||
public function testTransformMulti()
|
||||
{
|
||||
$o1 = new class() {
|
||||
public function getId()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
$o2 = new class() {
|
||||
public function getId()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
$repository = $this->prophesize(ObjectRepository::class);
|
||||
|
||||
$transformer = new IdToEntityDataTransformer(
|
||||
$repository->reveal(),
|
||||
true
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'1,2',
|
||||
$transformer->transform([$o1, $o2])
|
||||
);
|
||||
}
|
||||
|
||||
public function testTransformSingle()
|
||||
{
|
||||
$o = new class() {
|
||||
public function getId()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
$repository = $this->prophesize(ObjectRepository::class);
|
||||
|
||||
$transformer = new IdToEntityDataTransformer(
|
||||
$repository->reveal(),
|
||||
false
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'1',
|
||||
$transformer->transform($o)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user