<?php
namespace Cms\CoreBundle\Doctrine\Types;
use Cms\CoreBundle\Model\AbstractBitwise;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\BigIntType;
/**
* Class AbstractBitwiseType
* @package Cms\CoreBundle\Doctrine\Types
*/
abstract class AbstractBitwiseType extends BigIntType
{
protected const BITWISE_NAME = null;
protected const BITWISE_CLASS_NAME = null;
/**
* {@inheritdoc}
*/
public function getName(): string
{
if (empty(static::BITWISE_NAME)) {
throw new \LogicException();
}
return sprintf(
'bitwise_%s',
static::BITWISE_NAME
);
}
/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
switch (true) {
case is_int($value):
break;
case $value instanceof AbstractBitwise:
$value = $value->getMask();
break;
case empty($value):
$value = 0;
break;
default:
throw new \RuntimeException();
}
return parent::convertToDatabaseValue($value, $platform);
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$class = static::BITWISE_CLASS_NAME;
if ( ! is_subclass_of($class, AbstractBitwise::class)) {
throw new \LogicException();
}
$value = ((empty($value)) ? 0 : (int) $value);
return new $class($value);
}
}