From 38e9afb0b3de3d60ca363ddf59a8208d2b285dbf Mon Sep 17 00:00:00 2001 From: Eugene Leonovich Date: Wed, 22 Jun 2016 22:16:19 +0200 Subject: [PATCH 1/5] Enhance the "text_array" type --- .../DBAL/PostgresTypes/AbstractArrayType.php | 55 ---------------- .../DBAL/PostgresTypes/IntArrayType.php | 36 ++++++---- .../DBAL/PostgresTypes/TextArrayType.php | 60 +++++++++++++++-- .../DBAL/PostgresTypes/TextArrayTypeTest.php | 66 ++++++++++--------- 4 files changed, 113 insertions(+), 104 deletions(-) delete mode 100644 src/Doctrine/DBAL/PostgresTypes/AbstractArrayType.php 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..3509775 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 []; + } + + // @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,52 @@ 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('{"NULL,2"}', array("NULL,2")), + array('{"1",NULL}', array('1', null)), + array('{"NULL"}', array('NULL')), + array('{"1","2"}', array('1', '2')), + array('{"1\"2"}', array('1"2')), + array('{"\"2"}', array('"2')), + array('{"\"\""}', array('""')), + ); + } + + 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')), + array('{"1", "2"}', array('1', '2')), + array('{"1,2", "3,4"}', array('1,2', '3,4')), ); } } From c464eecee480f6805607b7732eb2f41c6b4a81cc Mon Sep 17 00:00:00 2001 From: Eugene Leonovich Date: Wed, 22 Jun 2016 22:28:38 +0200 Subject: [PATCH 2/5] Make TextArrayType PHP 5.3 compatible --- src/Doctrine/DBAL/PostgresTypes/TextArrayType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php b/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php index 3509775..4fbe318 100644 --- a/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php +++ b/src/Doctrine/DBAL/PostgresTypes/TextArrayType.php @@ -49,13 +49,13 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform) { if (empty($value) || '{}' === $value) { - return []; + return array(); } // @see http://stackoverflow.com/a/19082849/1160901 preg_match_all('/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(? Date: Wed, 22 Jun 2016 22:38:54 +0200 Subject: [PATCH 3/5] Remove duplicate data in TextArrayTypeTest::provideValidValues() --- tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php index 5962e5f..4874495 100644 --- a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php @@ -66,7 +66,6 @@ public static function provideValidValues() array('{"1,NULL"}', array("1,NULL")), array('{"NULL,2"}', array("NULL,2")), array('{"1",NULL}', array('1', null)), - array('{"NULL"}', array('NULL')), array('{"1","2"}', array('1', '2')), array('{"1\"2"}', array('1"2')), array('{"\"2"}', array('"2')), From 05f39ed9bcc0946f2d2720e5f70c9f74190afa68 Mon Sep 17 00:00:00 2001 From: Eugene Leonovich Date: Thu, 23 Jun 2016 17:15:41 +0200 Subject: [PATCH 4/5] Fix CS --- .../Tests/DBAL/PostgresTypes/TextArrayTypeTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php index 4874495..a1c0bbf 100644 --- a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php @@ -59,12 +59,12 @@ public static function provideValidValues() 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,2"}', array('NULL,2')), array('{"1",NULL}', array('1', null)), array('{"NULL"}', array('NULL')), - array('{"1,NULL"}', array("1,NULL")), - array('{"NULL,2"}', array("NULL,2")), + array('{"1,NULL"}', array('1,NULL')), + array('{"NULL,2"}', array('NULL,2')), array('{"1",NULL}', array('1', null)), array('{"1","2"}', array('1', '2')), array('{"1\"2"}', array('1"2')), From 0563906b965cb7cb4a4ff2e3b26060a9b30aff65 Mon Sep 17 00:00:00 2001 From: Eugene Leonovich Date: Tue, 28 Jun 2016 11:26:46 +0200 Subject: [PATCH 5/5] Update test data --- .../Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php index a1c0bbf..eb607d1 100644 --- a/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php +++ b/tests/Doctrine/Tests/DBAL/PostgresTypes/TextArrayTypeTest.php @@ -64,12 +64,12 @@ public static function provideValidValues() array('{"1",NULL}', array('1', null)), array('{"NULL"}', array('NULL')), array('{"1,NULL"}', array('1,NULL')), - array('{"NULL,2"}', array('NULL,2')), - 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')), ); } @@ -81,8 +81,6 @@ public static function provideToPHPValidValues() array('{NOTNULL,2}', array('NOTNULL', '2')), array('{NULL2}', array('NULL2')), array('{1,2}', array('1', '2')), - array('{"1", "2"}', array('1', '2')), - array('{"1,2", "3,4"}', array('1,2', '3,4')), ); } }