Skip to content

Commit 7e8c7bc

Browse files
committed
Fix EnumType
1 parent 422146d commit 7e8c7bc

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

README.MD

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,6 @@ composer require neutronstars/enum
88

99
---
1010

11-
## Doctrine Configuration
12-
13-
If you are using you must add this to the `doctrine.yaml` configuration file:
14-
15-
```yml
16-
doctrine:
17-
dbal:
18-
types:
19-
ns_enum:
20-
class: NeutronStars\Enum\Types\EnumType
21-
```
22-
23-
---
24-
2511
## How create an Enum class:
2612

2713
### Method 1
@@ -234,4 +220,40 @@ echo 'Default value: ' . MyEnum::valueOf('A key that does not exist.');
234220

235221
```
236222
Default value: TWO
237-
```
223+
```
224+
225+
---
226+
227+
## Doctrine Configuration
228+
229+
If you use it, you must create a Type class for your enum:
230+
231+
```php
232+
namespace App\Types;
233+
234+
class MyEnumType extends \NeutronStars\Enum\Types\EnumType {
235+
public const MY_ENUM = 'my_enum';
236+
237+
public function getName(): string
238+
{
239+
return self::MY_ENUM;
240+
}
241+
242+
public function convertToPHPValue($value,\Doctrine\DBAL\Platforms\AbstractPlatform $platform): MyEnum
243+
{
244+
return MyEnum::valueOf($value);
245+
}
246+
}
247+
```
248+
249+
And you must add this to the `doctrine.yaml` configuration file:
250+
251+
```yml
252+
doctrine:
253+
dbal:
254+
types:
255+
my_enum:
256+
class: App\Types\MyEnumType
257+
```
258+
259+
---

src/Types/EnumType.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,15 @@
99
use Doctrine\DBAL\Types\Types;
1010
use NeutronStars\Enum\Enum;
1111

12-
class EnumType extends Type
12+
abstract class EnumType extends Type
1313
{
14-
public const NS_ENUM = 'ns_enum';
15-
16-
public function getName(): string
17-
{
18-
return self::NS_ENUM;
19-
}
20-
2114
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
2215
{
2316
return Types::STRING;
2417
}
2518

26-
public function convertToPHPValue($value, AbstractPlatform $platform): ?string
19+
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
2720
{
28-
return $value instanceof Enum ? (string) $value : null;
21+
return ($value instanceof Enum) ? (string) $value : null;
2922
}
3023
}

0 commit comments

Comments
 (0)