Commit 2cc1cdc5 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-22 - Throw ConversionException if DateTime, Time, Date, DateTimeTz, Array...

DBAL-22 - Throw ConversionException if DateTime, Time, Date, DateTimeTz, Array or Object conversions from the Database to PHP fail
parent d399d3c0
...@@ -41,7 +41,11 @@ class ArrayType extends Type ...@@ -41,7 +41,11 @@ class ArrayType extends Type
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{ {
$value = (is_resource($value)) ? stream_get_contents($value) : $value; $value = (is_resource($value)) ? stream_get_contents($value) : $value;
return unserialize($value); $val = unserialize($value);
if (!is_array($val)) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
public function getName() public function getName()
......
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
/**
* Conversion Exception is thrown when the database to PHP conversion fails
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
namespace Doctrine\DBAL\Types;
class ConversionException extends \Doctrine\DBAL\DBALException
{
/**
* Thrown when a Database to Doctrine Type Conversion fails.
*
* @param string $value
* @param string $toType
* @return ConversionException
*/
static public function conversionFailed($value, $toType)
{
$value = (strlen($value) > 32) ? substr($value, 0, 20) . "..." : $value;
return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
}
}
\ No newline at end of file
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -48,7 +46,11 @@ class DateTimeType extends Type ...@@ -48,7 +46,11 @@ class DateTimeType extends Type
public function convertToPHPValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
return ($value !== null) $val = ($value !== null)
? \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value) : null; ? \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value) : null;
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
} }
\ No newline at end of file
...@@ -66,7 +66,11 @@ class DateTimeTzType extends Type ...@@ -66,7 +66,11 @@ class DateTimeTzType extends Type
public function convertToPHPValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
return ($value !== null) $val = ($value !== null)
? \DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value) : null; ? \DateTime::createFromFormat($platform->getDateTimeTzFormatString(), $value) : null;
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
} }
\ No newline at end of file
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -48,7 +46,11 @@ class DateType extends Type ...@@ -48,7 +46,11 @@ class DateType extends Type
public function convertToPHPValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
return ($value !== null) $val = ($value !== null)
? \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value) : null; ? \DateTime::createFromFormat('!'.$platform->getDateFormatString(), $value) : null;
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
} }
\ No newline at end of file
...@@ -22,7 +22,11 @@ class ObjectType extends Type ...@@ -22,7 +22,11 @@ class ObjectType extends Type
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{ {
$value = (is_resource($value)) ? stream_get_contents($value) : $value; $value = (is_resource($value)) ? stream_get_contents($value) : $value;
return unserialize($value); $val = unserialize($value);
if (!is_object($val)) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
public function getName() public function getName()
......
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
...@@ -57,7 +55,11 @@ class TimeType extends Type ...@@ -57,7 +55,11 @@ class TimeType extends Type
*/ */
public function convertToPHPValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
return ($value !== null) $val = ($value !== null)
? \DateTime::createFromFormat($platform->getTimeFormatString(), $value) : null; ? \DateTime::createFromFormat($platform->getTimeFormatString(), $value) : null;
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
} }
} }
\ No newline at end of file
...@@ -19,6 +19,12 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase ...@@ -19,6 +19,12 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('array'); $this->_type = Type::getType('array');
} }
public function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testArrayConvertsToDatabaseValue() public function testArrayConvertsToDatabaseValue()
{ {
$this->assertTrue( $this->assertTrue(
...@@ -32,4 +38,12 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase ...@@ -32,4 +38,12 @@ class ArrayTest extends \Doctrine\Tests\DbalTestCase
is_array($this->_type->convertToPHPValue(serialize(array()), $this->_platform)) is_array($this->_type->convertToPHPValue(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);
}
} }
\ No newline at end of file
...@@ -61,4 +61,10 @@ class DateTest extends \Doctrine\Tests\DbalTestCase ...@@ -61,4 +61,10 @@ class DateTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('00:00:00', $date->format('H:i:s')); $this->assertEquals('00:00:00', $date->format('H:i:s'));
$this->assertEquals('2009-11-01', $date->format('Y-m-d')); $this->assertEquals('2009-11-01', $date->format('Y-m-d'));
} }
public function testInvalidDateFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
} }
\ No newline at end of file
...@@ -36,4 +36,10 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -36,4 +36,10 @@ class DateTimeTest extends \Doctrine\Tests\DbalTestCase
$this->assertType('DateTime', $date); $this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
} }
public function testInvalidDateTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
} }
\ No newline at end of file
...@@ -36,4 +36,10 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase ...@@ -36,4 +36,10 @@ class DateTimeTzTest extends \Doctrine\Tests\DbalTestCase
$this->assertType('DateTime', $date); $this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); $this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
} }
public function testInvalidDateFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
} }
\ No newline at end of file
...@@ -19,6 +19,11 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase ...@@ -19,6 +19,11 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
$this->_type = Type::getType('object'); $this->_type = Type::getType('object');
} }
public function tearDown()
{
error_reporting(-1); // reactive all error levels
}
public function testObjectConvertsToDatabaseValue() public function testObjectConvertsToDatabaseValue()
{ {
$this->assertTrue( $this->assertTrue(
...@@ -32,4 +37,11 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase ...@@ -32,4 +37,11 @@ class ObjectTest extends \Doctrine\Tests\DbalTestCase
is_object($this->_type->convertToPHPValue(serialize(new \stdClass), $this->_platform)) is_object($this->_type->convertToPHPValue(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);
}
} }
\ No newline at end of file
...@@ -33,4 +33,10 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase ...@@ -33,4 +33,10 @@ class TimeTest extends \Doctrine\Tests\DbalTestCase
instanceof \DateTime instanceof \DateTime
); );
} }
public function testInvalidTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment