Skip to content

Commit 018a740

Browse files
committed
Added enumeration doctrine type
1 parent 3994191 commit 018a740

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace DHolmes\DoctrineExtras;
4+
5+
use Doctrine\DBAL\Types\Type;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
7+
use DHolmes\LangExtra\Enumeration;
8+
9+
abstract class EnumerationType extends Type
10+
{
11+
/**
12+
* @param array $fieldDeclaration
13+
* @param AbstractPlatform $platform
14+
* @return string
15+
*/
16+
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
17+
{
18+
return 'VARCHAR(50)';
19+
}
20+
21+
/** @return string */
22+
abstract protected function getClass();
23+
24+
/**
25+
* @param string $value
26+
* @param AbstractPlatform $platform
27+
* @return Enumeration
28+
*/
29+
public function convertToPHPValue($value, AbstractPlatform $platform)
30+
{
31+
$phpValue = null;
32+
if ($value !== null)
33+
{
34+
$class = $this->getClass();
35+
$phpValue = $class::get($value);
36+
}
37+
38+
return $phpValue;
39+
}
40+
41+
/**
42+
* @param Enumeration $value
43+
* @param AbstractPlatform $platform
44+
* @return string
45+
*/
46+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
47+
{
48+
$dbValue = null;
49+
$class = $this->getClass();
50+
if ($value instanceof $class)
51+
{
52+
$dbValue = $value->getKey();
53+
}
54+
else if ($value !== null)
55+
{
56+
throw new \Exception(sprintf('Cannot persist value of type "%s', get_class($value)));
57+
}
58+
59+
return $dbValue;
60+
}
61+
}

0 commit comments

Comments
 (0)