|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meilisearch\Bundle\Tests\Dbal\Type; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 8 | +use Doctrine\DBAL\Types\ConversionException; |
| 9 | +use Doctrine\DBAL\Types\Type; |
| 10 | +use Meilisearch\Bundle\Tests\Entity\ObjectId\DummyObjectId; |
| 11 | + |
| 12 | +final class DummyObjectIdType extends Type |
| 13 | +{ |
| 14 | + public function getName(): string |
| 15 | + { |
| 16 | + return 'dummy_object_id'; |
| 17 | + } |
| 18 | + |
| 19 | + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string |
| 20 | + { |
| 21 | + return $platform->getIntegerTypeDeclarationSQL($column); |
| 22 | + } |
| 23 | + |
| 24 | + public function convertToPHPValue($value, AbstractPlatform $platform): ?DummyObjectId |
| 25 | + { |
| 26 | + if ($value instanceof DummyObjectId || null === $value) { |
| 27 | + return $value; |
| 28 | + } |
| 29 | + |
| 30 | + if (!\is_string($value) && !is_int($value)) { |
| 31 | + throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]); |
| 32 | + } |
| 33 | + |
| 34 | + return new DummyObjectId((int) $value); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws ConversionException |
| 39 | + */ |
| 40 | + public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int |
| 41 | + { |
| 42 | + if ($value instanceof DummyObjectId) { |
| 43 | + return $value->toInt(); |
| 44 | + } |
| 45 | + |
| 46 | + if (null === $value || '' === $value) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + if (!\is_string($value) && !is_int($value)) { |
| 51 | + throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]); |
| 52 | + } |
| 53 | + |
| 54 | + return (int) $value; |
| 55 | + } |
| 56 | + |
| 57 | + public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
| 58 | + { |
| 59 | + return true; |
| 60 | + } |
| 61 | +} |
0 commit comments