Skip to content

Commit

Permalink
BooleanEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkl578 authored and VasekPurchart committed Oct 12, 2017
1 parent 1d040fa commit 7719e14
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Now you can use the `Sex` enum in your `User` entity. There are two important th

You can specify any other parameters for `ORM\Column` as you would usually (nullability, length...).

There is also `integer_enum` and `float_enum` which can be used respectively for their types.
There is also `integer_enum`, `float_enum` and `boolean_enum` which can be used respectively for their types.

2) `@Enum(class=Sex::class)` - this will be used for reconstructing the `Sex`
enum object when loading the value back from database
Expand Down Expand Up @@ -139,6 +139,7 @@ composer require consistence/consistence-doctrine
```php
<?php

use Consistence\Doctrine\Enum\Type\BooleanEnumType;
use Consistence\Doctrine\Enum\Type\FloatEnumType;
use Consistence\Doctrine\Enum\Type\IntegerEnumType;
use Consistence\Doctrine\Enum\Type\StringEnumType;
Expand All @@ -154,6 +155,7 @@ $loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

// register Doctrine DBAL types
DoctrineType::addType(BooleanEnumType::NAME, BooleanEnumType::class); // boolean_enum
DoctrineType::addType(FloatEnumType::NAME, FloatEnumType::class); // float_enum
DoctrineType::addType(IntegerEnumType::NAME, IntegerEnumType::class); // integer_enum
DoctrineType::addType(StringEnumType::NAME, StringEnumType::class); // string_enum
Expand Down
34 changes: 34 additions & 0 deletions src/Enum/Type/BooleanEnumType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types = 1);

namespace Consistence\Doctrine\Enum\Type;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class BooleanEnumType extends \Doctrine\DBAL\Types\BooleanType
{

const NAME = 'boolean_enum';

public function getName(): string
{
return self::NAME;
}

/**
* @param \Consistence\Enum\Enum|bool|null $value
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
* @return bool|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return EnumType::convertToDatabaseValue($value);
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

}
4 changes: 4 additions & 0 deletions tests/Enum/Type/EnumTypeIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function convertEnumToDatabaseProvider(): array
[DoctrineType::getType(IntegerEnumType::NAME), FooFloatEnum::get(FooFloatEnum::ONE), FooFloatEnum::ONE],
[DoctrineType::getType(IntegerEnumType::NAME), FooIntegerEnum::get(FooIntegerEnum::ONE), FooIntegerEnum::ONE],
[DoctrineType::getType(StringEnumType::NAME), FooStringEnum::get(FooStringEnum::ONE), FooStringEnum::ONE],
[DoctrineType::getType(BooleanEnumType::NAME), FooBooleanEnum::get(FooBooleanEnum::ENABLED), FooBooleanEnum::ENABLED],
];
}

Expand All @@ -45,6 +46,7 @@ public function enumTypesProvider(): array
[DoctrineType::getType(FloatEnumType::NAME)],
[DoctrineType::getType(IntegerEnumType::NAME)],
[DoctrineType::getType(StringEnumType::NAME)],
[DoctrineType::getType(BooleanEnumType::NAME)],
];
}

Expand All @@ -68,6 +70,7 @@ public function convertScalarValueToDatabaseProvider(): array
[DoctrineType::getType(FloatEnumType::NAME), FooFloatEnum::ONE],
[DoctrineType::getType(IntegerEnumType::NAME), FooIntegerEnum::ONE],
[DoctrineType::getType(StringEnumType::NAME), FooStringEnum::ONE],
[DoctrineType::getType(BooleanEnumType::NAME), FooBooleanEnum::ENABLED],
];
}

Expand All @@ -92,6 +95,7 @@ public function enumTypeClassesProvider(): array
[FloatEnumType::class],
[IntegerEnumType::class],
[StringEnumType::class],
[BooleanEnumType::class],
];
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Enum/Type/data/FooBooleanEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types = 1);

namespace Consistence\Doctrine\Enum\Type;

class FooBooleanEnum extends \Consistence\Enum\Enum
{

const ENABLED = true;
const DISABLED = false;

}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types = 1);

use Consistence\Doctrine\Enum\Type\BooleanEnumType;
use Consistence\Doctrine\Enum\Type\FloatEnumType;
use Consistence\Doctrine\Enum\Type\IntegerEnumType;
use Consistence\Doctrine\Enum\Type\StringEnumType;
Expand All @@ -17,3 +18,4 @@
DoctrineType::addType(FloatEnumType::NAME, FloatEnumType::class);
DoctrineType::addType(IntegerEnumType::NAME, IntegerEnumType::class);
DoctrineType::addType(StringEnumType::NAME, StringEnumType::class);
DoctrineType::addType(BooleanEnumType::NAME, BooleanEnumType::class);

0 comments on commit 7719e14

Please sign in to comment.