|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\Extension\Core; |
| 13 | + |
| 14 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 15 | +use Symfony\Component\Form\Extension\Core\Type\DateIntervalType; |
| 16 | +use Symfony\Component\Form\Extension\Core\Type\DateTimeType; |
| 17 | +use Symfony\Component\Form\Extension\Core\Type\EnumType; |
| 18 | +use Symfony\Component\Form\Extension\Core\Type\FileType; |
| 19 | +use Symfony\Component\Form\Extension\Core\Type\IntegerType; |
| 20 | +use Symfony\Component\Form\Extension\Core\Type\NumberType; |
| 21 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 22 | +use Symfony\Component\Form\Extension\Core\Type\UlidType; |
| 23 | +use Symfony\Component\Form\Extension\Core\Type\UuidType; |
| 24 | +use Symfony\Component\Form\FormTypeGuesserInterface; |
| 25 | +use Symfony\Component\Form\Guess\Guess; |
| 26 | +use Symfony\Component\Form\Guess\TypeGuess; |
| 27 | +use Symfony\Component\Form\Guess\ValueGuess; |
| 28 | + |
| 29 | +class ReflectionTypeGuesser implements FormTypeGuesserInterface |
| 30 | +{ |
| 31 | + public function guessType(string $class, string $property): ?TypeGuess |
| 32 | + { |
| 33 | + $type = $this->getReflectionType($class, $property); |
| 34 | + |
| 35 | + if (!($type instanceof \ReflectionNamedType)) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $name = $type->getName(); |
| 40 | + |
| 41 | + if (enum_exists($name)) { |
| 42 | + return new TypeGuess(EnumType::class, ['class' => $name], Guess::MEDIUM_CONFIDENCE); |
| 43 | + } |
| 44 | + |
| 45 | + return match ($name) { |
| 46 | + // PHP types |
| 47 | + 'bool' => new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 48 | + 'float' => new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 49 | + 'int' => new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 50 | + 'string' => new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE), |
| 51 | + |
| 52 | + // PHP classes |
| 53 | + 'DateTime' => new TypeGuess(DateTimeType::class, [], Guess::LOW_CONFIDENCE), |
| 54 | + 'DateTimeImmutable' => new TypeGuess(DateTimeType::class, ['input' => 'datetime_immutable'], Guess::LOW_CONFIDENCE), |
| 55 | + 'DateInterval' => new TypeGuess(DateIntervalType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 56 | + 'DateTimeZone' => new TypeGuess(TimezoneType::class, ['input' => 'datetimezone'], Guess::MEDIUM_CONFIDENCE), |
| 57 | + 'IntlTimeZone' => new TypeGuess(TimezoneType::class, ['input' => 'intltimezone'], Guess::MEDIUM_CONFIDENCE), |
| 58 | + |
| 59 | + // Symfony classes |
| 60 | + 'Symfony\Component\HttpFoundation\File\File' => new TypeGuess(FileType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 61 | + 'Symfony\Component\Uid\Ulid' => new TypeGuess(UlidType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 62 | + 'Symfony\Component\Uid\Uuid' => new TypeGuess(UuidType::class, [], Guess::MEDIUM_CONFIDENCE), |
| 63 | + |
| 64 | + default => null, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + public function guessRequired(string $class, string $property): ?ValueGuess |
| 69 | + { |
| 70 | + $type = $this->getReflectionType($class, $property); |
| 71 | + |
| 72 | + if (!$type) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return new ValueGuess(!$type->allowsNull(), Guess::MEDIUM_CONFIDENCE); |
| 77 | + } |
| 78 | + |
| 79 | + public function guessMaxLength(string $class, string $property): ?ValueGuess |
| 80 | + { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + public function guessPattern(string $class, string $property): ?ValueGuess |
| 85 | + { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + private function getReflectionType(string $class, string $property): ?\ReflectionType |
| 90 | + { |
| 91 | + try { |
| 92 | + $reflection = new \ReflectionProperty($class, $property); |
| 93 | + } catch (\ReflectionException $e) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return $reflection->getType(); |
| 98 | + } |
| 99 | +} |
0 commit comments