Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AmicronBooleanType | |
100.00% |
11 / 11 |
|
100.00% |
3 / 3 |
14 | |
100.00% |
1 / 1 |
| convertToDatabaseValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| convertToPHPValue | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
10 | |||
| requiresSQLCommentHint | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Satag\AmicronEntityBundle\Doctrine; |
| 6 | |
| 7 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 8 | use Doctrine\DBAL\Types\BooleanType; |
| 9 | |
| 10 | /** |
| 11 | * Amicron boolean type: stores PHP true/false as 'J'/'N' in Firebird VARCHAR(1) columns. |
| 12 | * |
| 13 | * Usage in QueryBuilder (Symfony 6.4+ / Doctrine ORM 3.x): |
| 14 | * ->setParameter('val', true, 'boolean') |
| 15 | * or use the type-aware form: |
| 16 | * ->setParameter('val', true, Types::BOOLEAN) |
| 17 | */ |
| 18 | class AmicronBooleanType extends BooleanType |
| 19 | { |
| 20 | #[\Override] |
| 21 | public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed |
| 22 | { |
| 23 | if ($value === null) { |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | return (bool) $value ? 'J' : 'N'; |
| 28 | } |
| 29 | |
| 30 | #[\Override] |
| 31 | public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?bool |
| 32 | { |
| 33 | if ($value === null) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | if ($value === 'J' || $value === true || $value === 1 || $value === '1') { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | if ($value === 'N' || $value === false || $value === 0 || $value === '0') { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | return (bool) $platform->convertFromBoolean($value); |
| 46 | } |
| 47 | |
| 48 | #[\Override] |
| 49 | public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | } |