diff --git a/src/Doctrine/DBAL/PostgresTypes/AbstractArrayType.php b/src/Doctrine/DBAL/PostgresTypes/AbstractArrayType.php deleted file mode 100644 index d23b948..0000000 --- a/src/Doctrine/DBAL/PostgresTypes/AbstractArrayType.php +++ /dev/null @@ -1,55 +0,0 @@ - - */ -abstract class AbstractArrayType extends Type -{ - /** - * Converts a value from its database representation to its PHP representation - * of this type. - * - * @param mixed $value The value to convert. - * @param AbstractPlatform $platform The currently used database platform. - * - * @return mixed The PHP representation of the value. - */ - public function convertToPHPValue($value, AbstractPlatform $platform) - { - $value = trim($value, '{}'); - - if ($value === '') { - return array(); - } - - return explode(',', $value); - } - - /** - * Converts a value from its PHP representation to its database representation - * of this type. - * - * @param mixed $value The value to convert. - * @param AbstractPlatform $platform The currently used database platform. - * - * @return mixed The database representation of the value. - */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) - { - return '{'.implode(',', $value).'}'; - } -} diff --git a/src/Doctrine/DBAL/PostgresTypes/IntArrayType.php b/src/Doctrine/DBAL/PostgresTypes/IntArrayType.php index 2b0b281..35de385 100644 --- a/src/Doctrine/DBAL/PostgresTypes/IntArrayType.php +++ b/src/Doctrine/DBAL/PostgresTypes/IntArrayType.php @@ -8,39 +8,51 @@ namespace Doctrine\DBAL\PostgresTypes; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\Type; /** - * IntArrayType. - * * Only supports single dimensional arrays like text[]. * * @author Richard Fullmer + * @author Eugene Leonovich */ -class IntArrayType extends AbstractArrayType +class IntArrayType extends Type { /** - * @return string + * {@inheritdoc} */ - public function getName() + public function convertToPHPValue($value, AbstractPlatform $platform) { - return 'int_array'; + $value = trim($value, '{}'); + + if ($value === '') { + return array(); + } + + return array_map('intval', explode(',', $value)); } /** * {@inheritdoc} */ - public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + public function convertToDatabaseValue($value, AbstractPlatform $platform) { - return '_int4'; + return '{'.implode(',', $value).'}'; } /** - * {@inheritdoc} + * @return string */ - public function convertToPHPValue($value, AbstractPlatform $platform) + public function getName() { - $result = parent::convertToPHPValue($value, $platform); + return 'int_array'; + } - return array_map('intval', $result); + /** + * {@inheritdoc} + */ + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + { + return '_int4'; } } diff --git a/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php b/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php index 2665ee8..4fbe318 100644 --- a/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php +++ b/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php @@ -8,18 +8,68 @@ namespace Doctrine\DBAL\PostgresTypes; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\Type; /** - * TextArrayType. - * * Only supports single dimensional arrays like text[]. * - * @author Richard Fullmer + * @author Eugene Leonovich */ -class TextArrayType extends AbstractArrayType +class TextArrayType extends Type { /** - * @return string + * {@inheritdoc} + */ + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if (empty($value)) { + return '{}'; + } + + $result = ''; + foreach ($value as $part) { + if (null === $part) { + $result .= 'NULL,'; + continue; + } + if ('' === $part) { + $result .= '"",'; + continue; + } + + $result .= '"'.addcslashes($part, '"').'",'; + } + + return '{'.substr($result, 0, -1).'}'; + } + + /** + * {@inheritdoc} + */ + public function convertToPHPValue($value, AbstractPlatform $platform) + { + if (empty($value) || '{}' === $value) { + return array(); + } + + // @see http://stackoverflow.com/a/19082849/1160901 + preg_match_all('/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(? + * @author Eugene Leonovich */ class TextArrayTypeTest extends \PHPUnit_Framework_TestCase { @@ -27,17 +26,11 @@ class TextArrayTypeTest extends \PHPUnit_Framework_TestCase */ protected $_platform; - /** - * Pre-instantiation setup. - */ public static function setUpBeforeClass() { Type::addType('text_array', 'Doctrine\\DBAL\\PostgresTypes\\TextArrayType'); } - /** - * Pre-execution setup. - */ protected function setUp() { $this->_platform = new PostgreSqlPlatform(); @@ -45,43 +38,49 @@ protected function setUp() } /** - * Test conversion of PHP array to database value. - * - * @dataProvider databaseConvertProvider + * @dataProvider provideValidValues */ public function testTextArrayConvertsToDatabaseValue($serialized, $array) { - $converted = $this->_type->convertToDatabaseValue($array, $this->_platform); - $this->assertInternalType('string', $converted); - $this->assertEquals($serialized, $converted); + $this->assertSame($serialized, $this->_type->convertToDatabaseValue($array, $this->_platform)); } /** - * Test conversion of database value to PHP array. - * - * @dataProvider databaseConvertProvider + * @dataProvider provideToPHPValidValues */ public function testTextArrayConvertsToPHPValue($serialized, $array) { - $converted = $this->_type->convertToPHPValue($serialized, $this->_platform); - $this->assertInternalType('array', $converted); - $this->assertEquals($array, $converted); - - if (sizeof($converted) > 0) { - $this->assertInternalType('string', reset($converted)); - } + $this->assertSame($array, $this->_type->convertToPHPValue($serialized, $this->_platform)); } - /** - * Provider for conversion test values. - * - * @return array - */ - public static function databaseConvertProvider() + public static function provideValidValues() { return array( - array('{simple,extended}', array('simple', 'extended')), array('{}', array()), + array('{""}', array('')), + array('{NULL}', array(null)), + array('{"1,NULL"}', array('1,NULL')), + array('{"NULL,2"}', array('NULL,2')), + array('{"1",NULL}', array('1', null)), + array('{"NULL"}', array('NULL')), + array('{"1,NULL"}', array('1,NULL')), + array('{"1","2"}', array('1', '2')), + array('{"1\"2"}', array('1"2')), + array('{"\"2"}', array('"2')), + array('{"\"\""}', array('""')), + array('{"1","2"}', array('1', '2')), + array('{"1,2","3,4"}', array('1,2', '3,4')), + ); + } + + public static function provideToPHPValidValues() + { + return self::provideValidValues() + array( + array('{NULL,2}', array(null, '2')), + array('{NOTNULL}', array('NOTNULL')), + array('{NOTNULL,2}', array('NOTNULL', '2')), + array('{NULL2}', array('NULL2')), + array('{1,2}', array('1', '2')), ); } }