Created
May 19, 2014 07:04
-
-
Save StanAngeloff/a5e5ea8b31b96178f911 to your computer and use it in GitHub Desktop.
A quick & dirty Doctrine DBAL type for PostgreSQL text[] column.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* (c) PSP UK Group Ltd. <[email protected]> | |
* | |
* For the full copyright and license information, | |
* please view the LICENSE file that was distributed with this source code. | |
*/ | |
namespace Psp\Components\DoctrineTypes\DBAL\Types; | |
use Psp\Components\DoctrineTypes\DBAL\Exception\PlatformNotSupportedException; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Doctrine\DBAL\Platforms\PostgreSqlPlatform; | |
use Doctrine\DBAL\Types\ConversionException; | |
use Doctrine\DBAL\Types\Type; | |
/** | |
* Doctrine DBAL 'text[]' type for PostgreSQL | |
* | |
* @SuppressWarnings(PMD.StaticAccess) | |
*/ | |
class ArrayOfTextType extends Type | |
{ | |
const ARRAY_OF_TEXT = 'text[]'; | |
# {{{ Type | |
/** | |
* {@inheritdoc} | |
*/ | |
public function canRequireSQLConversion() | |
{ | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function convertToPHPValueSQL($sqlExpr, $platform) | |
{ | |
$this->ensurePostgreSqlPlatform($platform); | |
return sprintf('array_to_json(%s)', $sqlExpr); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | |
{ | |
$this->ensurePostgreSqlPlatform($platform); | |
return 'text[]'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
$this->ensurePostgreSqlPlatform($platform); | |
if ($value === null) { | |
return null; | |
} | |
return json_decode($value, /* $assoc = */ true); | |
} | |
/** | |
* {@inheritdoc} | |
* | |
* @SuppressWarnings(PMD.ShortVariableName) | |
*/ | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
$this->ensurePostgreSqlPlatform($platform); | |
if ($value === null) { | |
return null; | |
} | |
if (is_scalar($value)) { | |
$value = array($value); | |
} elseif (is_object($value)) { | |
$value = iterator_to_array($value, /* $use_keys = */ false); | |
} | |
return sprintf( | |
'{%s}', | |
implode( | |
/* $glue = */ ',', | |
array_map( | |
# Double quotes and backslashes embedded in element values will be backslash-escaped. | |
# | |
# See http://www.postgresql.org/docs/9.2/static/arrays.html#ARRAYS-IO | |
function ($v) { | |
# The value may be an object which implements __toString(..). | |
return sprintf('"%s"', addcslashes((string) $v, '"\\')); | |
}, | |
array_filter( | |
$value, | |
function ($v) { | |
return isset ($v); | |
} | |
) | |
) | |
) | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return self::ARRAY_OF_TEXT; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBindingType() | |
{ | |
return \PDO::PARAM_STR; | |
} | |
# }}} | |
/** | |
* Ensure the given AbstractPlatform instance is a PostgreSqlPlatform one. | |
* | |
* @return void | |
* | |
* @throws PlatformNotSupportedException If the platform is not PostgreSqlPlatform. | |
*/ | |
private function ensurePostgreSqlPlatform(AbstractPlatform $platform) | |
{ | |
if ($platform instanceof PostgreSqlPlatform) { | |
return; | |
} | |
throw new PlatformNotSupportedException( | |
strtr( | |
'The platform "{platform}" is not supported by "{class}".', | |
array( | |
'{platform}' => get_class($platform), | |
'{class}' => get_class($this), | |
) | |
), | |
1400420170 | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment