Skip to content

Commit

Permalink
copy DateTimeType and DateType from Psc but use the Webforge\Common\D…
Browse files Browse the repository at this point in the history
…ateTime
  • Loading branch information
pscheit committed Jan 15, 2014
1 parent 35bb458 commit e666f34
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Webforge/Doctrine/Types/DateTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Webforge\Doctrine\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Webforge\Common\DateTime\DateTime;

class DateTimeType extends \Doctrine\DBAL\Types\DateTimeType {

public function convertToPHPValue($value, AbstractPlatform $platform) {
if ($value === null) {
return null;
}

$val = DateTime::parse($platform->getDateTimeFormatString(), $value);
if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
}

return $val;
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
return sprintf("%s COMMENT '%s' ",
parent::getSQLDeclaration($fieldDeclaration, $platform),
$platform->getDoctrineTypeComment($this)
);
}

public function getName() {
return 'WebforgeDateTime';
}
}
34 changes: 34 additions & 0 deletions lib/Webforge/Doctrine/Types/DateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Webforge\Doctrine\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Webforge\Common\DateTime\Date;

class DateType extends \Doctrine\DBAL\Types\DateType {

public function convertToPHPValue($value, AbstractPlatform $platform) {
if ($value === null) {
return null;
}

$val = Date::parse($platform->getDateFormatString(), $value);
if (!$val) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateFormatString());
}

return $val;
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
return sprintf("%s COMMENT '%s' ",
parent::getSQLDeclaration($fieldDeclaration, $platform),
$platform->getDoctrineTypeComment($this)
);
}

public function getName() {
return 'WebforgeDate';
}
}

0 comments on commit e666f34

Please sign in to comment.