conversionFailedFormat() public static method

Thrown when a Database to Doctrine Type Conversion fails and we can make a statement about the expected format.
public static conversionFailedFormat ( string $value, string $toType, string $expectedFormat, Exception $previous = null ) : ConversionException
$value string
$toType string
$expectedFormat string
$previous Exception
return ConversionException
 /**
  * @see \Doctrine\DBAL\Types\JsonArrayType::convertToPHPValue()
  * @param string $sValue
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $oPlatform
  * @throws \Doctrine\DBAL\Types\ConversionException
  * @return \Zend\Http\Headers
  */
 public function convertToPHPValue($sValue, \Doctrine\DBAL\Platforms\AbstractPlatform $oPlatform)
 {
     $aValue = parent::convertToPHPValue($sValue, $oPlatform);
     if (is_array($aValue)) {
         $oHeaders = new \Zend\Http\Headers();
         return $oHeaders->addHeaders($aValue);
     }
     throw \Doctrine\DBAL\Types\ConversionException::conversionFailedFormat($sValue, $this->getName(), 'json encoded array');
 }
 /**
  * @param string|DateTime $value
  * @param string          $fromFormat
  * @param string          $toFormat
  *
  * @return string
  *
  * @throws ConversionException
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 private function convertDateTimeString($value, $fromFormat, $toFormat)
 {
     $dateTime = $value instanceof DateTime ? $value : DateTime::createFromFormat($fromFormat, $value);
     if ($dateTime === false) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $fromFormat);
     }
     $result = $dateTime->format($toFormat);
     return $result;
 }
 /**
  * @override
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (null !== $value) {
         if (1 !== preg_match('/^\\d+$/', $value)) {
             throw ConversionException::conversionFailedFormat($value, $this->getName(), '^\\d+$');
         }
         $value = DateInterval::fromSeconds($value);
     }
     return $value;
 }
 /**
  * @override
  * @param mixed $value
  * @param AbstractPlatform $platform
  * @return mixed|DateRange
  * @throws ConversionException
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (null !== $value) {
         if (false == preg_match('/^(\\[|\\()(\\d{4})-(\\d{2})-(\\d{2}),(\\d{4})-(\\d{2})-(\\d{2})(\\]|\\))$/', $value)) {
             throw ConversionException::conversionFailedFormat($value, $this->getName(), '(\\[|\\()(\\d{4})-(\\d{2})-(\\d{2}),(\\d{4})-(\\d{2})-(\\d{2})(\\]|\\))$');
         }
         $value = DateRange::fromString($value);
     }
     return $value;
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     if ($value instanceof Carbon) {
         return $value->copy()->setTimezone('UTC')->format($platform->getDateTimeFormatString());
     }
     throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
 }
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return $value;
     }
     if (!$value instanceof \DateTime) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), self::MICRO_FORMAT);
     }
     return $value->format(self::MICRO_FORMAT);
 }
 /**
  * @see \Doctrine\DBAL\Types\Type::convertToPHPValue()
  * @param string|null $sValue
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $oPlatform
  * @return string|null
  */
 public function convertToPHPValue($sValue, \Doctrine\DBAL\Platforms\AbstractPlatform $oPlatform)
 {
     $sValue = parent::convertToPHPValue($sValue, $oPlatform);
     if (is_null($sValue)) {
         return $sValue;
     }
     if ($sFilterValue = filter_var($sValue, FILTER_VALIDATE_IP)) {
         return $sFilterValue;
     }
     throw \Doctrine\DBAL\Types\ConversionException::conversionFailedFormat($sValue, $this->getName(), 'valid IP adress');
 }
Example #8
0
 /**
  * @param mixed $value
  * @param AbstractPlatform $platform
  * @return \DateTime|null
  * @throws ConversionException
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $val = \DateTime::createFromFormat('!' . self::FORMAT, $value);
     if (empty($val)) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT);
     }
     return $val;
 }
 /**
  * @param \DateTimeImmutable|string|null $value
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  * @return \DateTimeImmutable|null
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof DateTimeImmutable) {
         return $value;
     }
     $dateTime = DateTimeImmutable::createFromFormat('!' . $platform->getTimeFormatString(), $value);
     if ($dateTime === false) {
         throw \Doctrine\DBAL\Types\ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getTimeFormatString());
     }
     return $dateTime;
 }
Example #10
0
 /**
  * {@inheritDoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     try {
         return Decimal::create($value);
     } catch (\Exception $e) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), '0.0');
     }
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateInterval) {
         return $value;
     }
     try {
         return new \DateInterval($value);
     } catch (\Exception $exception) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), 'PY-m-dTH:i:s', $exception);
     }
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (null === $value || $value instanceof \DateTime) {
         return $value;
     }
     $converted = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc ? self::$utc : (self::$utc = new \DateTimeZone('UTC')));
     if (!$converted) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $converted;
 }
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $val = DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, $this->getUTCInstance());
     if ($val === false) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $val;
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateTime) {
         return $value;
     }
     $val = \DateTime::createFromFormat('!' . $platform->getTimeFormatString(), $value);
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getTimeFormatString());
     }
     return $val;
 }
Example #15
0
 /**
  * @param string $dateTimeString
  * @param AbstractPlatform $platform
  *
  * @throws ConversionException
  *
  * @return DateTime|null
  */
 public function convertToPHPValue($dateTimeString, AbstractPlatform $platform)
 {
     if (null === $dateTimeString || $dateTimeString instanceof DateTime) {
         return $dateTimeString;
     }
     $dateTime = DateTime::createFromFormat($platform->getDateTimeFormatString(), $dateTimeString, self::getUtc());
     if (!$dateTime) {
         throw ConversionException::conversionFailedFormat($dateTimeString, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $dateTime;
 }
Example #16
0
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $val = \Improvein\MssqlBundle\Types\ImproveinDateTime::createFromFormat('!' . $platform->getDateFormatString(), $value);
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateFormatString());
     }
     return $val;
 }
 /** @noinspection PhpMissingParentCallCommonInspection
  * @inheritdoc
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     /** @var string|null|DateTime $value */
     if ($value === null) {
         return null;
     }
     $dateTime = $value instanceof DateTime ? $value : DateTime::createFromFormat(static::JSON_API_FORMAT, $value);
     if ($dateTime === false) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), static::JSON_API_FORMAT);
     }
     return $dateTime->format($platform->getDateTimeFormatString());
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof UriInterface) {
         return $value;
     }
     try {
         $val = new Uri($value);
     } catch (\InvalidArgumentException $e) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), 'URI (RFC 3986)');
     }
     return $val;
 }
Example #19
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateTimeZone) {
         return $value;
     }
     try {
         $val = new \DateTimeZone($value);
     } catch (\Exception $e) {
         // Exception message example: DateTimeZone::__construct(): Unknown or bad timezone (foobar)
         throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Valid timezone');
     }
     return $val;
 }
Example #20
0
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === NULL) {
         return NULL;
     } elseif ($value instanceof \DateTime) {
         return NetteDateTime::from($value);
     }
     $val = NetteDateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
     }
     return NetteDateTime::from($val);
 }
 /** @noinspection PhpMissingParentCallCommonInspection
  * @inheritdoc
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     /** @var string|null|DateTime $value */
     if ($value === null || $value instanceof DateTime) {
         return $value;
     }
     $dbFormat = $platform->getDateTimeFormatString();
     $dateTime = DateTime::createFromFormat($dbFormat, $value);
     if ($dateTime === false) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $dbFormat);
     }
     return $dateTime;
 }
Example #22
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateTime) {
         return $value;
     }
     $val = \DateTime::createFromFormat(self::FORMAT_STRING, $value);
     if (!$val) {
         $val = \DateTime::createFromFormat(self::FORMAT_STRING_2, $value);
         if (!$val) {
             throw ConversionException::conversionFailedFormat($value, $this->getName(), self::FORMAT_STRING);
         }
     }
     return $val;
 }
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if ($value === null || is_string($value)) {
         return $value;
     }
     $val = null;
     if ($value instanceof \DateTime) {
         $val = $value->format($platform->getDateTimeFormatStringToDatabase());
     }
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatStringToDatabase());
     }
     return $val;
 }
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     try {
         return parent::convertToPHPValue($value, $platform);
     } catch (ConversionException $e) {
         $val = \DateTime::createFromFormat('Y-m-d H:i:s.uO', $value);
         if (!$val) {
             throw ConversionException::conversionFailedFormat($value, $this->getName(), 'Y-m-d H:i:s.uO');
         }
         return $val;
     }
 }
Example #25
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($databaseValue, AbstractPlatform $platform)
 {
     if (null === $databaseValue || $databaseValue instanceof \DateTime) {
         return $databaseValue;
     }
     // The changed part is the following bloc where we put the DateTimeZone as the third argument rather than
     // relying on the local timezone
     $phpValue = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $databaseValue, new \DateTimeZone('UTC'));
     if (false === $phpValue) {
         $phpValue = date_create($databaseValue);
     }
     if (false === $phpValue) {
         throw ConversionException::conversionFailedFormat($databaseValue, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $phpValue;
 }
 /**
  * @param \DateTimeImmutable|string|null $value
  * @param AbstractPlatform               $platform
  * @return \DateTimeImmutable|null
  * @throws ConversionException
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateTimeImmutable) {
         return $value;
     }
     if (is_string($value)) {
         $dateTime = \DateTimeImmutable::createFromFormat($platform->getDateTimeFormatString(), $value);
         if ($dateTime !== false) {
             return $dateTime;
         }
     }
     if (is_array($value) || is_object($value)) {
         $value = print_r($value, true);
     }
     throw ConversionException::conversionFailedFormat((string) $value, $this->getName(), $platform->getDateTimeFormatString());
 }
 public function testConvertToPHPValueWithInvalidString()
 {
     $value = 'invalid';
     /** @var MySqlPlatform|\PHPUnit_Framework_MockObject_MockObject $mysqlPlatform */
     $mysqlPlatform = $this->getMockWithoutInvokingTheOriginalConstructor(MySqlPlatform::class);
     /** @var DateTimeImmutableType|\PHPUnit_Framework_MockObject_MockObject $dateTimeImmutableType */
     $dateTimeImmutableType = $this->getMockBuilder(DateTimeImmutableType::class)->disableOriginalConstructor()->setMethods(null)->getMock();
     $mysqlPlatform->expects($this->any())->method('getDateTimeFormatString')->willReturn('Y-m-d H:i:s');
     $expectedMessage = ConversionException::conversionFailedFormat($value, DateTimeImmutableType::NAME, $mysqlPlatform->getDateTimeFormatString())->getMessage();
     try {
         $dateTimeImmutableType->convertToPHPValue($value, $mysqlPlatform);
         $this->fail('Not fail with: ' . $expectedMessage);
     } catch (ConversionException $e) {
         $this->assertSame($expectedMessage, $e->getMessage());
     }
 }
Example #28
0
 /**
  * (non-PHPdoc)
  * 
  * @see \Doctrine\DBAL\Types\Type::convertToPHPValue()
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $matches = array();
     if (!preg_match('`\\((-?[0-9.]+),(-?[0-9.]+)\\),\\((-?[0-9.]+),(-?[0-9.]+)\\)`', $value, $matches)) {
         throw ConversionException::conversionFailedFormat($value, self::NAME, '(x1,y1),(x2,y2)');
     }
     $box = new Box();
     $box->left = floatval($matches[1]);
     $box->bottom = floatval($matches[2]);
     $box->right = floatval($matches[3]);
     $box->top = floatval($matches[4]);
     return $box;
 }
Example #29
0
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value == null) {
         return null;
     }
     $parts = explode("::", $value);
     if (count($parts) != 2) {
         throw ConversionException::conversionFailedFormat($value, "enum", "class::field");
     }
     $clazz = $parts[0];
     $value = $parts[1];
     if (!class_exists($clazz)) {
         throw ConversionException::conversionFailed($value, "enum {$clazz}");
     }
     return Enum::parseEnum($clazz, $value);
 }
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return $value;
     }
     if ($value instanceof DateTime) {
         return $value->setTimezone($this->dateTimeZone());
     }
     $val = DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, $this->dateTimeZone());
     if (!$val) {
         $val = date_create($value, $this->dateTimeZone());
     }
     if (!$val) {
         throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
     }
     return $val;
 }