WeekToArrayTransformer example

private const WIDGETS = [
        'text' => IntegerType::class,
        'choice' => ChoiceType::class,
    ];

    /** * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ('string' === $options['input']) {
            $builder->addModelTransformer(new WeekToArrayTransformer());
        }

        if ('single_text' === $options['widget']) {
            $builder->addViewTransformer(new ReversedTransformer(new WeekToArrayTransformer()));
        } else {
            $yearOptions = $weekOptions = [
                'error_bubbling' => true,
                'empty_data' => '',
            ];
            // when the form is compound the entries of the array are ignored in favor of children data             // so we need to handle the cascade setting here
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\DataTransformer\WeekToArrayTransformer;

class WeekToArrayTransformerTest extends TestCase
{
    public function testTransform()
    {
        $transformer = new WeekToArrayTransformer();

        $this->assertSame(['year' => 2019, 'week' => 1]$transformer->transform('2019-W01'));
    }

    public function testTransformEmpty()
    {
        $transformer = new WeekToArrayTransformer();

        $this->assertSame(['year' => null, 'week' => null]$transformer->transform(null));
    }

    
Home | Imprint | This part of the site doesn't use cookies.