mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 15:43:51 +00:00
Feature: Allow to filter periods to reassign by postal code
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?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\MainBundle\Form\Type\DataTransformer;
|
||||
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Chill\MainBundle\Repository\PostalCodeRepositoryInterface;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use function gettype;
|
||||
use function is_int;
|
||||
|
||||
class PostalCodeToIdTransformer implements DataTransformerInterface
|
||||
{
|
||||
private PostalCodeRepositoryInterface $postalCodeRepository;
|
||||
|
||||
public function __construct(PostalCodeRepositoryInterface $postalCodeRepository)
|
||||
{
|
||||
$this->postalCodeRepository = $postalCodeRepository;
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if (null === $value || trim('') === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!is_int((int) $value)) {
|
||||
throw new TransformationFailedException('Cannot transform ' . gettype($value));
|
||||
}
|
||||
|
||||
return $this->postalCodeRepository->find((int) $value);
|
||||
}
|
||||
|
||||
public function transform($value)
|
||||
{
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof PostalCode) {
|
||||
return $value->getId();
|
||||
}
|
||||
|
||||
throw new TransformationFailedException('Could not reverseTransform ' . gettype($value));
|
||||
}
|
||||
}
|
49
src/Bundle/ChillMainBundle/Form/Type/PickPostalCodeType.php
Normal file
49
src/Bundle/ChillMainBundle/Form/Type/PickPostalCodeType.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?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\MainBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Entity\PostalCode;
|
||||
use Chill\MainBundle\Form\Type\DataTransformer\PostalCodeToIdTransformer;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class PickPostalCodeType extends AbstractType
|
||||
{
|
||||
private PostalCodeToIdTransformer $postalCodeToIdTransformer;
|
||||
|
||||
public function __construct(PostalCodeToIdTransformer $postalCodeToIdTransformer)
|
||||
{
|
||||
$this->postalCodeToIdTransformer = $postalCodeToIdTransformer;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->addViewTransformer($this->postalCodeToIdTransformer);
|
||||
}
|
||||
|
||||
public function buildView(FormView $view, FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['uniqid'] = $view->vars['attr']['data-input-postal-code'] = uniqid('input_pick_postal_code_');
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setDefault('class', PostalCode::class)
|
||||
->setDefault('multiple', false)
|
||||
->setAllowedTypes('multiple', ['bool'])
|
||||
->setDefault('compound', false);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user