vendor/ramsey/uuid-doctrine/src/UuidType.php line 58

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the ramsey/uuid-doctrine library
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  *
  8.  * @copyright Copyright (c) Ben Ramsey <http://benramsey.com>
  9.  * @license http://opensource.org/licenses/MIT MIT
  10.  */
  11. namespace Ramsey\Uuid\Doctrine;
  12. use InvalidArgumentException;
  13. use Ramsey\Uuid\Uuid;
  14. use Ramsey\Uuid\UuidInterface;
  15. use Doctrine\DBAL\Types\ConversionException;
  16. use Doctrine\DBAL\Types\GuidType;
  17. use Doctrine\DBAL\Platforms\AbstractPlatform;
  18. /**
  19.  * Field type mapping for the Doctrine Database Abstraction Layer (DBAL).
  20.  *
  21.  * UUID fields will be stored as a string in the database and converted back to
  22.  * the Uuid value object when querying.
  23.  */
  24. class UuidType extends GuidType
  25. {
  26.     /**
  27.      * @var string
  28.      */
  29.     const NAME 'uuid';
  30.     /**
  31.      * {@inheritdoc}
  32.      *
  33.      * @param string|UuidInterface|null $value
  34.      * @param AbstractPlatform $platform
  35.      *
  36.      * @return UuidInterface|null
  37.      *
  38.      * @throws ConversionException
  39.      */
  40.     public function convertToPHPValue($valueAbstractPlatform $platform)
  41.     {
  42.         if ($value === null || $value === '') {
  43.             return null;
  44.         }
  45.         if ($value instanceof UuidInterface) {
  46.             return $value;
  47.         }
  48.         try {
  49.             $uuid Uuid::fromString($value);
  50.         } catch (InvalidArgumentException $e) {
  51.             throw ConversionException::conversionFailed($value, static::NAME);
  52.         }
  53.         return $uuid;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      *
  58.      * @param UuidInterface|string|null $value
  59.      * @param AbstractPlatform $platform
  60.      *
  61.      * @return string|null
  62.      *
  63.      * @throws ConversionException
  64.      */
  65.     public function convertToDatabaseValue($valueAbstractPlatform $platform)
  66.     {
  67.         if ($value === null || $value === '') {
  68.             return null;
  69.         }
  70.         if (
  71.             $value instanceof UuidInterface
  72.             || (
  73.                 (is_string($value)
  74.                 || (is_object($value) && method_exists($value'__toString')))
  75.                 && Uuid::isValid((string) $value)
  76.             )
  77.         ) {
  78.             return (string) $value;
  79.         }
  80.         throw ConversionException::conversionFailed($value, static::NAME);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      *
  85.      * @return string
  86.      */
  87.     public function getName()
  88.     {
  89.         return static::NAME;
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      *
  94.      * @param AbstractPlatform $platform
  95.      *
  96.      * @return bool
  97.      */
  98.     public function requiresSQLCommentHint(AbstractPlatform $platform)
  99.     {
  100.         return true;
  101.     }
  102.     /**
  103.      * @param AbstractPlatform $platform
  104.      *
  105.      * @return array
  106.      */
  107.     public function getMappedDatabaseTypes(AbstractPlatform $platform)
  108.     {
  109.         return [self::NAME];
  110.     }
  111. }