ValueGuess example

return null;
    }

    /** * Guesses whether a field is required based on the given constraint. */
    public function guessRequiredForConstraint(Constraint $constraint): ?ValueGuess
    {
        return match ($constraint::class) {
            NotNull::class,
            NotBlank::class,
            IsTrue::class => new ValueGuess(true, Guess::HIGH_CONFIDENCE),
            default => null,
        };
    }

    /** * Guesses a field's maximum length based on the given constraint. */
    public function guessMaxLengthForConstraint(Constraint $constraint): ?ValueGuess
    {
        switch ($constraint::class) {
            case Length::class:
                

        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);
    }

    public function configurePatternGuess(string $pattern, int $confidence): void
    {
        $this->patternGuess = new ValueGuess($pattern$confidence);
    }
}
if (!$classMetadatas) {
            return null;
        }

        /** @var ClassMetadataInfo $classMetadata */
        $classMetadata = $classMetadatas[0];

        // Check whether the field exists and is nullable or not         if (isset($classMetadata->fieldMappings[$property])) {
            if (!$classMetadata->isNullable($property) && Types::BOOLEAN !== $classMetadata->getTypeOfField($property)) {
                return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
            }

            return new ValueGuess(false, Guess::MEDIUM_CONFIDENCE);
        }

        // Check whether the association exists, is a to-one association and its         // join column is nullable or not         if ($classMetadata->isAssociationWithSingleJoinColumn($property)) {
            $mapping = $classMetadata->getAssociationMapping($property);

            if (!isset($mapping['joinColumns'][0]['nullable'])) {
                
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)];
    }

    public function testRequiredGuesserSimpleFieldNotNullable()
    {
        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->fieldMappings['field'] = true;
        $classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(false);

        $this->assertEquals(new ValueGuess(true, Guess::HIGH_CONFIDENCE)$this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
    }

    public function testRequiredGuesserSimpleFieldNullable()
    {
        $classMetadata = $this->createMock(ClassMetadata::class);
        $classMetadata->fieldMappings['field'] = true;
        $classMetadata->expects($this->once())->method('isNullable')->with('field')->willReturn(true);

        $this->assertEquals(new ValueGuess(false, Guess::MEDIUM_CONFIDENCE)$this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
    }

    
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)],
            [new Type(\DateTimeImmutable::class)new TypeGuess(DateType::class['input' => 'datetime_immutable'], Guess::MEDIUM_CONFIDENCE)],
            [new Type('\DateTime')new TypeGuess(DateType::class[], Guess::MEDIUM_CONFIDENCE)],
        ];
    }

    public static function guessRequiredProvider()
    {
        return [
            [new NotNull()new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
            [new NotBlank()new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
            [new IsTrue()new ValueGuess(true, Guess::HIGH_CONFIDENCE)],
            [new Length(['min' => 10, 'max' => 10])new ValueGuess(false, Guess::LOW_CONFIDENCE)],
            [new Range(['min' => 1, 'max' => 20])new ValueGuess(false, Guess::LOW_CONFIDENCE)],
        ];
    }

    /** * @dataProvider guessRequiredProvider */
    public function testGuessRequired($constraint$guess)
    {
Home | Imprint | This part of the site doesn't use cookies.