Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ parameters:
count: 1
path: src/Qossmic/DataTransformer/ValueObjectTransformer.php

-
message: '#^Method Qossmic\\RichModelForms\\DataTransformer\\ValueObjectTransformer\:\:getPropertyValue\(\) should return bool\|int\|string\|null but returns mixed\.$#'
identifier: return.type
count: 1
path: src/Qossmic/DataTransformer/ValueObjectTransformer.php

-
message: '#^Parameter \#1 \$strategy of method Qossmic\\RichModelForms\\ExceptionHandling\\ExceptionHandlerRegistry\:\:has\(\) expects string, mixed given\.$#'
identifier: argument.type
Expand Down
7 changes: 2 additions & 5 deletions src/Qossmic/DataTransformer/ValueObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ public function __construct(ExceptionHandlerRegistry $exceptionHandlerRegistry,

/**
* @param object|null $value
*
* @return array<string,bool|int|string|null>|bool|int|string|null
*/
public function transform(mixed $value): array|bool|int|string|null
public function transform(mixed $value): mixed
{
if (null === $value) {
return null;
Expand Down Expand Up @@ -90,10 +88,9 @@ public function reverseTransform(mixed $value): ?object
}
}

private function getPropertyValue(FormBuilderInterface $form, object $object): bool|int|string|null
private function getPropertyValue(FormBuilderInterface $form, object $object): mixed
{
if (null !== $form->getPropertyPath()) {
/* @phpstan-ignore-next-line */
return $this->propertyAccessor->getValue($object, $form->getPropertyPath());
}

Expand Down
40 changes: 40 additions & 0 deletions tests/Fixtures/Form/NonScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the RichModelFormsBundle package.
*
* (c) Christian Flothmann <christian.flothmann@qossmic.com>
* (c) Christopher Hertel <mail@christopher-hertel.de>
* (c) QOSSMIC GmbH <info@qossmic.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Qossmic\RichModelForms\Tests\Fixtures\Form;

use Qossmic\RichModelForms\Tests\Fixtures\Model\ForNonScalarType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NonScalarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('dateFrom', DateType::class, [
'widget' => 'single_text',
'required' => true,
'input' => 'datetime_immutable',
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('factory', [ForNonScalarType::class, 'create']);
$resolver->setDefault('immutable', true);
}
}
34 changes: 34 additions & 0 deletions tests/Fixtures/Model/ForNonScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the RichModelFormsBundle package.
*
* (c) Christian Flothmann <christian.flothmann@qossmic.com>
* (c) Christopher Hertel <mail@christopher-hertel.de>
* (c) QOSSMIC GmbH <info@qossmic.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Qossmic\RichModelForms\Tests\Fixtures\Model;

class ForNonScalarType
{
private function __construct(
private \DateTimeImmutable $dateFrom,
) {
}

public static function create(\DateTimeImmutable $dateFrom): self
{
return new self($dateFrom);
}

public function getDateFrom(): \DateTimeImmutable
{
return $this->dateFrom;
}
}
15 changes: 15 additions & 0 deletions tests/Integration/ValueObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use Qossmic\RichModelForms\Extension\RichModelFormsTypeExtension;
use Qossmic\RichModelForms\Tests\ExceptionHandlerRegistryTrait;
use Qossmic\RichModelForms\Tests\Fixtures\Form\GrossPriceType;
use Qossmic\RichModelForms\Tests\Fixtures\Form\NonScalarType;
use Qossmic\RichModelForms\Tests\Fixtures\Form\PriceType;
use Qossmic\RichModelForms\Tests\Fixtures\Model\ForNonScalarType;
use Qossmic\RichModelForms\Tests\Fixtures\Model\GrossPrice;
use Qossmic\RichModelForms\Tests\Fixtures\Model\Price;
use Symfony\Component\Form\Exception\TransformationFailedException;
Expand All @@ -34,6 +36,19 @@ class ValueObjectsTest extends TestCase
{
use ExceptionHandlerRegistryTrait;

public function testValueObjectTransformerSupportsNonScalarValue(): void
{
$form = $this->createNamedForm('amount', NonScalarType::class);
$form->submit([
'dateFrom' => '2025-01-01',
]);

$data = $form->getData();

$this->assertInstanceOf(ForNonScalarType::class, $data);
$this->assertEquals(new \DateTimeImmutable('2025-01-01'), $data->getDateFrom());
}

public function testNonCompoundRootFormDoesNotRequirePropertyPathToBeSetIfPropertyPathCanBeDerivedFromFormName(): void
{
$form = $this->createNamedForm('amount', PriceType::class, new Price(500), [
Expand Down
Loading