mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 17:13:49 +00:00
Add Rector Rule to inject normalization methods into export classes
This update introduces a Rector rule to automatically add `normalizeFormData`, `denormalizeFormData`, and `getNormalizationVersion` methods to export-related classes implementing specific interfaces. It ensures consistency and reduces manual work by leveraging traits and default implementations for normalizing form data. Test fixtures and configurations are included to validate and support this functionality.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Chill\Utils\Rector\Tests\ChillBundleAddNormalizationMethodsOnExportRector;
|
||||
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class ChillBundleAddNormalizationMethodsOnExportRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public static function provideData(): \Iterator
|
||||
{
|
||||
return self::yieldFilesFromDirectory(__DIR__.'/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__.'/config/config.php';
|
||||
}
|
||||
}
|
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
class MyFilter implements FilterInterface
|
||||
{
|
||||
public function getTitle()
|
||||
{
|
||||
return "some title";
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('field', ChoiceType::class, [
|
||||
'choices' => ['one', 'two', 'three', 'four', 'five'],
|
||||
'multiple' => false,
|
||||
]);
|
||||
|
||||
$builder->add('entity', EntityType::class, []);
|
||||
|
||||
$builder->add('user', \Chill\MainBundle\Form\Type\PickUserDynamicType::class, []);
|
||||
|
||||
$builder->add('rolling_date', \Chill\MainBundle\Form\Type\PickRollingDateType::class);
|
||||
|
||||
$builder->add('date', \Chill\MainBundle\Form\Type\ChillDateType::class, []);
|
||||
}
|
||||
|
||||
public function getFormDefaultData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function addRole(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data, \Chill\MainBundle\Export\ExportGenerationContext $exportGenerationContext)
|
||||
{
|
||||
// nothing to add here
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return ['something'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
|
||||
class MyFilter implements FilterInterface
|
||||
{
|
||||
use \Chill\MainBundle\Export\ExportDataNormalizerTrait;
|
||||
public function getTitle()
|
||||
{
|
||||
return "some title";
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
$builder->add('field', ChoiceType::class, [
|
||||
'choices' => ['one', 'two', 'three', 'four', 'five'],
|
||||
'multiple' => false,
|
||||
]);
|
||||
|
||||
$builder->add('entity', EntityType::class, []);
|
||||
|
||||
$builder->add('user', \Chill\MainBundle\Form\Type\PickUserDynamicType::class, []);
|
||||
|
||||
$builder->add('rolling_date', \Chill\MainBundle\Form\Type\PickRollingDateType::class);
|
||||
|
||||
$builder->add('date', \Chill\MainBundle\Form\Type\ChillDateType::class, []);
|
||||
}
|
||||
public function getNormalizationVersion(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
public function normalizeFormData(array $formData): array
|
||||
{
|
||||
return ['field' => $formData['field'], 'entity' => $this->normalizeDoctrineEntity($formData['entity']), 'user' => $this->normalizeDoctrineEntity($formData['user']), 'rolling_date' => $formData['rolling_date']->normalize(), 'date' => $this->normalizeDate($formData['date'])];
|
||||
}
|
||||
public function denormalizeFormData(array $formData, int $fromVersion): array
|
||||
{
|
||||
return ['field' => $formData['field'], 'entity' => $this->denormalizeDoctrineEntity($formData['entity'], $this->someRepository), 'user' => $this->denormalizeDoctrineEntity($formData['user'], $this->someRepository), 'rolling_date' => \Chill\MainBundle\Service\RollingDate\RollingDate::fromNormalized($formData['rolling_date']), 'date' => $this->denormalizeDate($formData['date'])];
|
||||
}
|
||||
|
||||
public function getFormDefaultData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function addRole(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data, \Chill\MainBundle\Export\ExportGenerationContext $exportGenerationContext)
|
||||
{
|
||||
// nothing to add here
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return ['something'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
use Chill\MainBundle\Export\FilterInterface;
|
||||
|
||||
class MyFilterUpdated implements FilterInterface
|
||||
{
|
||||
public function getTitle()
|
||||
{
|
||||
return "some title";
|
||||
}
|
||||
|
||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
||||
{
|
||||
}
|
||||
|
||||
public function getFormDefaultData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function normalizeFormData(array $formData): array
|
||||
{
|
||||
return $formData;
|
||||
}
|
||||
|
||||
public function denormalizeFormData(array $formData, int $fromVersion): array
|
||||
{
|
||||
return $formData;
|
||||
}
|
||||
|
||||
public function getNormalizationVersion(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
public function describeAction($data, $format = 'string')
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function addRole(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data, \Chill\MainBundle\Export\ExportGenerationContext $exportGenerationContext)
|
||||
{
|
||||
// nothing to add here
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return ['something'];
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function doSomething(): void
|
||||
{
|
||||
// dummy
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
return static function (Rector\Config\RectorConfig $rectorConfig): void {
|
||||
$rectorConfig->rule(Chill\Utils\Rector\Rector\ChillBundleAddNormalizationMethodsOnExportRector::class);
|
||||
};
|
Reference in New Issue
Block a user