Last active
December 26, 2018 18:30
-
-
Save kermorgant/3f85965b6a36ad9c8f851defc52bc977 to your computer and use it in GitHub Desktop.
Instance state type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Persistence\Types; | |
use AppBundle\State\Instance\InstancePaused; | |
use AppBundle\State\Instance\InstanceRemoved; | |
use AppBundle\State\Instance\InstanceRunning; | |
use AppBundle\State\Instance\InstanceStates; | |
use Doctrine\DBAL\Types\Type; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
class InstanceStateType extends Type | |
{ | |
const INSTANCE_STATE = 'instance_state'; | |
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | |
{ | |
return "ENUM('" . $this->getCommaSeparatedStates() . "') COMMENT '(DC2Type:" . self::INSTANCE_STATE . ")'"; | |
} | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
switch ($value) { | |
case InstanceStates::STATE_REMOVED: | |
return new InstanceRemoved(); | |
case InstanceStates::STATE_PAUSED: | |
return new InstancePaused(); | |
case InstanceStates::STATE_RUNNING: | |
return new InstanceRunning(); | |
} | |
} | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
return $value->getName(); | |
} | |
public function getName() | |
{ | |
return self::INSTANCE_STATE; | |
} | |
private function getCommaSeparatedStates() | |
{ | |
return implode("', '", InstanceStates::toArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment