-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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>. | ||
*/ | ||
|
||
|
||
namespace Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
|
||
/** | ||
* Variable DateTime Type using date_create() instead of DateTime::createFromFormat() | ||
* | ||
* This type has performance implications as it runs twice as long as the regular | ||
* {@see DateTimeType}, however in certain PostgreSQL configurations with | ||
* TIMESTAMP(n) columns where n > 0 it is necessary to use this type. | ||
* | ||
* @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> | ||
*/ | ||
class VarDateTimeType extends DateTimeType | ||
{ | ||
/** | ||
* @throws ConversionException | ||
* @param string $value | ||
* @param AbstractPlatform $platform | ||
* @return DateTime | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
$val = date_create($value); | ||
if (!$val) { | ||
throw ConversionException::conversionFailed($value, $this->getName()); | ||
} | ||
return $val; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Types\Type; | ||
use Doctrine\Tests\DBAL\Mocks; | ||
|
||
require_once __DIR__ . '/../../TestInit.php'; | ||
|
||
class VarDateTimeTest extends \Doctrine\Tests\DbalTestCase | ||
{ | ||
protected | ||
$_platform, | ||
$_type; | ||
|
||
protected function setUp() | ||
{ | ||
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); | ||
if (!Type::hasType('vardatetime')) { | ||
Type::addType('vardatetime', 'Doctrine\DBAL\Types\VarDateTimeType'); | ||
} | ||
$this->_type = Type::getType('vardatetime'); | ||
} | ||
|
||
public function testDateTimeConvertsToDatabaseValue() | ||
{ | ||
$date = new \DateTime('1985-09-01 10:10:10'); | ||
|
||
$expected = $date->format($this->_platform->getDateTimeTzFormatString()); | ||
$actual = is_string($this->_type->convertToDatabaseValue($date, $this->_platform)); | ||
|
||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
public function testDateTimeConvertsToPHPValue() | ||
{ | ||
// Birthday of jwage and also birthday of Doctrine. Send him a present ;) | ||
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform); | ||
$this->assertType('DateTime', $date); | ||
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); | ||
$this->assertEquals('000000', $date->format('u')); | ||
} | ||
|
||
public function testInvalidDateTimeFormatConversion() | ||
{ | ||
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); | ||
$this->_type->convertToPHPValue('abcdefg', $this->_platform); | ||
} | ||
|
||
public function testConversionWithMicroseconds() | ||
{ | ||
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00.123456', $this->_platform); | ||
$this->assertType('DateTime', $date); | ||
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s')); | ||
$this->assertEquals('123456', $date->format('u')); | ||
} | ||
|
||
public function testNullConversion() | ||
{ | ||
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform)); | ||
} | ||
} |