Fix storage of binary data for array and object types #219

Closed
wants to merge 3 commits into
from
View
6 lib/Doctrine/DBAL/Types/ArrayType.php
@@ -35,7 +35,7 @@ public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platfo
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
- return serialize($value);
+ return base64_encode(serialize($value));
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
@@ -45,6 +45,10 @@ public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatf
}
$value = (is_resource($value)) ? stream_get_contents($value) : $value;
+ // check for base64 encoded serialized data
+ if (strpos($value, ':') !== 1 && strpos($value, ';') !== 1) {
+ $value = base64_decode($value);
+ }
$val = unserialize($value);
if ($val === false && $value != 'b:0;') {
throw ConversionException::conversionFailed($value, $this->getName());
View
6 lib/Doctrine/DBAL/Types/ObjectType.php
@@ -35,7 +35,7 @@ public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platfo
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
- return serialize($value);
+ return base64_encode(serialize($value));
}
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
@@ -45,6 +45,10 @@ public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatf
}
$value = (is_resource($value)) ? stream_get_contents($value) : $value;
+ // check for base64 encoded serialized data
+ if (strpos($value, ':') !== 1 && strpos($value, ';') !== 1) {
+ $value = base64_decode($value);
+ }
$val = unserialize($value);
if ($val === false && $value !== 'b:0;') {
throw ConversionException::conversionFailed($value, $this->getName());
View
27 tests/Doctrine/Tests/DBAL/Types/ArrayTest.php
@@ -39,23 +39,48 @@ public function testArrayConvertsToPHPValue()
);
}
+ public function testArrayConvertsBase64ToPHPValue()
+ {
+ $this->assertTrue(
+ is_array($this->_type->convertToPHPValue(base64_encode(serialize(array())), $this->_platform))
+ );
+ }
+
public function testConversionFailure()
{
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
- public function testNullConversion()
+ public function testPlainNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
+ public function testNullConversion()
+ {
+ $this->assertNull($this->_type->convertToPHPValue(serialize(null), $this->_platform));
+ }
+
+ public function testNullConversionForBase64()
+ {
+ $this->assertNull($this->_type->convertToPHPValue(base64_encode(serialize(null)), $this->_platform));
+ }
+
/**
* @group DBAL-73
*/
public function testFalseConversion()
{
$this->assertFalse($this->_type->convertToPHPValue(serialize(false), $this->_platform));
}
+
+ /**
+ * @group DBAL-73
+ */
+ public function testFalseConversionForBase64()
+ {
+ $this->assertFalse($this->_type->convertToPHPValue(base64_encode(serialize(false)), $this->_platform));
+ }
}
View
25 tests/Doctrine/Tests/DBAL/Types/ObjectTest.php
@@ -34,23 +34,46 @@ public function testObjectConvertsToPHPValue()
$this->assertInternalType('object', $this->_type->convertToPHPValue(serialize(new \stdClass), $this->_platform));
}
+ public function testObjectConvertsBase64ToPHPValue()
+ {
+ $this->assertInternalType('object', $this->_type->convertToPHPValue(base64_encode(serialize(new \stdClass)), $this->_platform));
+ }
+
public function testConversionFailure()
{
error_reporting( (E_ALL | E_STRICT) - \E_NOTICE );
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
- public function testNullConversion()
+ public function testPlainNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
+ public function testNullConversion()
+ {
+ $this->assertNull($this->_type->convertToPHPValue(serialize(null), $this->_platform));
+ }
+
+ public function testNullConversionForBase64()
+ {
+ $this->assertNull($this->_type->convertToPHPValue(base64_encode(serialize(null)), $this->_platform));
+ }
+
/**
* @group DBAL-73
*/
public function testFalseConversion()
{
$this->assertFalse($this->_type->convertToPHPValue(serialize(false), $this->_platform));
}
+
+ /**
+ * @group DBAL-73
+ */
+ public function testFalseConversionForBase64()
+ {
+ $this->assertFalse($this->_type->convertToPHPValue(base64_encode(serialize(false)), $this->_platform));
+ }
}