src/Cms/CoreBundle/Doctrine/Types/AbstractBitwiseType.php line 66

Open in your IDE?
  1. <?php
  2. namespace Cms\CoreBundle\Doctrine\Types;
  3. use Cms\CoreBundle\Model\AbstractBitwise;
  4. use Doctrine\DBAL\Platforms\AbstractPlatform;
  5. use Doctrine\DBAL\Types\BigIntType;
  6. /**
  7.  * Class AbstractBitwiseType
  8.  * @package Cms\CoreBundle\Doctrine\Types
  9.  */
  10. abstract class AbstractBitwiseType extends BigIntType
  11. {
  12.     protected const BITWISE_NAME null;
  13.     protected const BITWISE_CLASS_NAME null;
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     public function getName(): string
  18.     {
  19.         if (empty(static::BITWISE_NAME)) {
  20.             throw new \LogicException();
  21.         }
  22.         return sprintf(
  23.             'bitwise_%s',
  24.             static::BITWISE_NAME
  25.         );
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public function requiresSQLCommentHint(AbstractPlatform $platform): bool
  31.     {
  32.         return true;
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function convertToDatabaseValue($valueAbstractPlatform $platform)
  38.     {
  39.         switch (true) {
  40.             case is_int($value):
  41.                 break;
  42.             case $value instanceof AbstractBitwise:
  43.                 $value $value->getMask();
  44.                 break;
  45.             case empty($value):
  46.                 $value 0;
  47.                 break;
  48.             default:
  49.                 throw new \RuntimeException();
  50.         }
  51.         return parent::convertToDatabaseValue($value$platform);
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function convertToPHPValue($valueAbstractPlatform $platform)
  57.     {
  58.         $class = static::BITWISE_CLASS_NAME;
  59.         if ( ! is_subclass_of($classAbstractBitwise::class)) {
  60.             throw new \LogicException();
  61.         }
  62.         $value = ((empty($value)) ? : (int) $value);
  63.         return new $class($value);
  64.     }
  65. }