TypeGuess example

private array $cache = [];

    public function __construct(ManagerRegistry $registry)
    {
        $this->registry = $registry;
    }

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (!$ret = $this->getMetadata($class)) {
            return new TypeGuess(TextType::class[], Guess::LOW_CONFIDENCE);
        }

        [$metadata$name] = $ret;

        if ($metadata->hasAssociation($property)) {
            $multiple = $metadata->isCollectionValuedAssociation($property);
            $mapping = $metadata->getAssociationMapping($property);

            return new TypeGuess(EntityType::class['em' => $name, 'class' => $mapping['targetEntity'], 'multiple' => $multiple], Guess::HIGH_CONFIDENCE);
        }

        
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)];
        yield [Types::DATETIMETZ_IMMUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE)];
        yield [Types::DATETIME_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE)];
        yield [Types::DATETIMETZ_MUTABLE, new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE)];
    }

    


    /** * Guesses a field class name for a given constraint. */
    public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess
    {
        switch ($constraint::class) {
            case Type::class:
                switch ($constraint->type) {
                    case 'array':
                        return new TypeGuess(CollectionType::class[], Guess::MEDIUM_CONFIDENCE);
                    case 'boolean':
                    case 'bool':
                        return new TypeGuess(CheckboxType::class[], Guess::MEDIUM_CONFIDENCE);

                    case 'double':
                    case 'float':
                    case 'numeric':
                    case 'real':
                        return new TypeGuess(NumberType::class[], Guess::MEDIUM_CONFIDENCE);

                    case 'integer':
                    

    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)],
            [new Type('real')new TypeGuess(NumberType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('int')new TypeGuess(IntegerType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('integer')new TypeGuess(IntegerType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('long')new TypeGuess(IntegerType::class[], Guess::MEDIUM_CONFIDENCE)],
            [new Type('string')new TypeGuess(TextType::class[], Guess::LOW_CONFIDENCE)],
            [new Type(\DateTime::class)new TypeGuess(DateType::class[], Guess::MEDIUM_CONFIDENCE)],
            [

        return $this->maxLengthGuess;
    }

    public function guessPattern($class$property): ?ValueGuess
    {
        return $this->patternGuess;
    }

    public function configureTypeGuess(string $type, array $options, int $confidence): void
    {
        $this->typeGuess = new TypeGuess($type$options$confidence);
    }

    public function configureRequiredGuess(bool $required, int $confidence): void
    {
        $this->requiredGuess = new ValueGuess($required$confidence);
    }

    public function configureMaxLengthGuess(int $maxLength, int $confidence): void
    {
        $this->maxLengthGuess = new ValueGuess($maxLength$confidence);
    }

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