guessType example

$this->metadataFactory->addMetadata($this->metadata);
        $this->guesser = new ValidatorTypeGuesser($this->metadataFactory);
    }

    /** * @dataProvider guessTypeProvider */
    public function testGuessType(Constraint $constraint, TypeGuess $guess)
    {
        $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint);

        $this->assertEquals($guess$this->guesser->guessType(self::TEST_CLASS, self::TEST_PROPERTY));
    }

    public static function guessTypeProvider()
    {
        return [
            [new Type('array')new TypeGuess(CollectionType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('bool')new TypeGuess(CheckboxType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('boolean')new TypeGuess(CheckboxType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('double')new TypeGuess(NumberType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('float')new TypeGuess(NumberType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('numeric')new TypeGuess(NumberType::class[], Guess::MEDIUM_CONFIDENCE)],
            [
$tmpGuessers[] = $guesser->guessers;
            } else {
                $tmpGuessers[] = [$guesser];
            }
        }

        $this->guessers = array_merge([], ...$tmpGuessers);
    }

    public function guessType(string $class, string $property): ?TypeGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessType($class$property));
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessRequired($class$property));
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return $this->guess(static fn ($guesser) => $guesser->guessMaxLength($class$property));
    }

    
class DoctrineOrmTypeGuesserTest extends TestCase
{
    /** * @dataProvider requiredType */
    public function testTypeGuesser(string $type$expected)
    {
        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->fieldMappings['field'] = true;
        $classMetadata->expects($this->once())->method('getTypeOfField')->with('field')->willReturn($type);

        $this->assertEquals($expected$this->getGuesser($classMetadata)->guessType('TestEntity', 'field'));
    }

    public static function requiredType()
    {
        yield [Types::DATE_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)];
        yield [Types::DATE_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE)];

        yield [Types::TIME_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)];
        yield [Types::TIME_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE)];

        yield [Types::DATETIME_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)];
        
        $type->buildForm($builder$builder->getOptions());

        return $builder;
    }

    public function createBuilderForProperty(string $class, string $property, mixed $data = null, array $options = []): FormBuilderInterface
    {
        if (null === $guesser = $this->registry->getTypeGuesser()) {
            return $this->createNamedBuilder($property, TextType::class$data$options);
        }

        $typeGuess = $guesser->guessType($class$property);
        $maxLengthGuess = $guesser->guessMaxLength($class$property);
        $requiredGuess = $guesser->guessRequired($class$property);
        $patternGuess = $guesser->guessPattern($class$property);

        $type = $typeGuess ? $typeGuess->getType() : TextType::class;

        $maxLength = $maxLengthGuess?->getValue();
        $pattern = $patternGuess?->getValue();

        if (null !== $pattern) {
            $options = array_replace_recursive(['attr' => ['pattern' => $pattern]]$options);
        }
Home | Imprint | This part of the site doesn't use cookies.