|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\Type; |
| 4 | + |
| 5 | +use Doctrine\DBAL\ParameterType; |
| 6 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 7 | +use Doctrine\DBAL\Types\Type; |
| 8 | +use LogicException; |
| 9 | +use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Blog\PrimaryKey; |
| 10 | +use ShipMonkTests\DoctrineEntityPreloader\Fixtures\Compat\CompatibilityType; |
| 11 | +use function base64_decode; |
| 12 | +use function base64_encode; |
| 13 | +use function get_debug_type; |
| 14 | +use function is_string; |
| 15 | + |
| 16 | +/** |
| 17 | + * This type exists only to have SOME working version of tests that are failing due to ORM bug https://github.com/doctrine/orm/pull/12130 |
| 18 | + * - the key feature here is that convertToDatabaseValue return value equals to PrimaryKey::__toString() |
| 19 | + */ |
| 20 | +final class PrimaryKeyBase64StringType extends Type |
| 21 | +{ |
| 22 | + |
| 23 | + use CompatibilityType; |
| 24 | + |
| 25 | + public function convertToPHPValue( |
| 26 | + mixed $value, |
| 27 | + AbstractPlatform $platform, |
| 28 | + ): ?PrimaryKey |
| 29 | + { |
| 30 | + if ($value === null) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + if (is_string($value)) { |
| 35 | + return new PrimaryKey((int) base64_decode($value, true)); |
| 36 | + } |
| 37 | + |
| 38 | + throw new LogicException('Unexpected value: ' . get_debug_type($value)); |
| 39 | + } |
| 40 | + |
| 41 | + public function convertToDatabaseValue( |
| 42 | + mixed $value, |
| 43 | + AbstractPlatform $platform, |
| 44 | + ): ?string |
| 45 | + { |
| 46 | + if ($value === null) { |
| 47 | + return null; |
| 48 | + |
| 49 | + } elseif ($value instanceof PrimaryKey) { |
| 50 | + return base64_encode((string) $value->getData()); |
| 51 | + |
| 52 | + } else { |
| 53 | + throw new LogicException('Unexpected value: ' . $value); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function getSQLDeclaration( |
| 58 | + array $column, |
| 59 | + AbstractPlatform $platform, |
| 60 | + ): string |
| 61 | + { |
| 62 | + return $platform->getStringTypeDeclarationSQL([]); |
| 63 | + } |
| 64 | + |
| 65 | + public function getName(): string |
| 66 | + { |
| 67 | + return PrimaryKey::DOCTRINE_TYPE_NAME; |
| 68 | + } |
| 69 | + |
| 70 | + public function doGetBindingType(): ParameterType|int // @phpstan-ignore return.unusedType (old dbal compat) |
| 71 | + { |
| 72 | + return ParameterType::STRING; |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments