Skip to content

Instantly share code, notes, and snippets.

@andrewprofile
Forked from szepczynski/UserIdType.php
Created February 17, 2017 20:31
Show Gist options
  • Select an option

  • Save andrewprofile/18a14a9046b2b7612140a3a96c6a7afc to your computer and use it in GitHub Desktop.

Select an option

Save andrewprofile/18a14a9046b2b7612140a3a96c6a7afc to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace User\Infrastructure\Doctrine\DBAL\Types;
use User\Domain\Model\UserId;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
/**
* Class UserIdType
* @package User\Infrastructure\Doctrine\DBAL\Types
*/
class UserIdType extends Type
{
const NAME = 'userId';
/**
* @return string
*/
public function getName()
{
return self::NAME;
}
/**
* @param mixed $value
* @param AbstractPlatform $platform
* @return null|string
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value instanceof UserId) {
return $value->getValue();
}
if ($value) {
return (string)$value;
}
return null;
}
/**
* @param mixed $value
* @param AbstractPlatform $platform
* @return mixed|UserId
* @throws \Assert\AssertionFailedException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof UserId) {
return $value;
}
return new UserId($value);
}
/**
* @param array $fieldDeclaration
* @param AbstractPlatform $platform
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getGuidTypeDeclarationSQL($fieldDeclaration);
}
/**
* @param AbstractPlatform $platform
* @return bool
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment