Skip to content

Instantly share code, notes, and snippets.

@kermorgant
Last active December 26, 2018 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kermorgant/3f85965b6a36ad9c8f851defc52bc977 to your computer and use it in GitHub Desktop.
Save kermorgant/3f85965b6a36ad9c8f851defc52bc977 to your computer and use it in GitHub Desktop.
Instance state type
<?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