conversionFailed() public static method

Thrown when a Database to Doctrine Type Conversion fails.
public static conversionFailed ( string $value, string $toType ) : ConversionException
$value string
$toType string
return ConversionException
コード例 #1
0
 /**
  * (non-PHPdoc)
  *
  * @see \Doctrine\DBAL\Types\Type::convertToPHPValue()
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $matches = array();
     preg_match('/(?:(?P<y>[0-9]+) (?:year|years))?' . ' ?(?:(?P<m>[0-9]+) (?:months|month|mons|mon))?' . ' ?(?:(?P<d>[0-9]+) (?:days|day))?' . ' ?(?:(?P<h>[0-9]{2}):(?P<i>[0-9]{2}):(?P<s>[0-9]{2}))?/i', $value, $matches);
     if (empty($matches)) {
         throw ConversionException::conversionFailed($value, self::NAME);
     }
     $interval = new Interval('PT0S');
     if (!empty($matches['y'])) {
         $interval->y = intval($matches['y']);
     }
     if (!empty($matches['m'])) {
         $interval->m = intval($matches['m']);
     }
     if (!empty($matches['d'])) {
         $interval->d = intval($matches['d']);
     }
     if (!empty($matches['h'])) {
         $interval->h = intval($matches['h']);
     }
     if (!empty($matches['i'])) {
         $interval->i = intval($matches['i']);
     }
     if (!empty($matches['s'])) {
         $interval->s = intval($matches['s']);
     }
     return $interval;
 }
コード例 #2
0
ファイル: OrderStateType.php プロジェクト: dumplie/dumplie
 /**
  * @param OrderState $value
  * @param AbstractPlatform $platform
  * @return array|string
  * @throws ConversionException
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (is_null($value)) {
         return [];
     }
     if (!$value instanceof OrderState) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     switch (get_class($value)) {
         case OrderState\Accepted::class:
             return 1;
         case OrderState\Created::class:
             return 2;
         case OrderState\Prepared::class:
             return 3;
         case OrderState\Refunded::class:
             return 4;
         case OrderState\Rejected::class:
             return 5;
         case OrderState\Sent::class:
             return 6;
         default:
             throw ConversionException::conversionFailed($value, $this->getName());
     }
 }
コード例 #3
0
ファイル: TinyIntType.php プロジェクト: ntd1712/common
 /** {@inheritdoc} */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (is_object($value)) {
         throw ConversionException::conversionFailed($value, $this);
     }
     return null === $value ? null : (int) $value;
 }
コード例 #4
0
ファイル: CoordinateType.php プロジェクト: ekyna/GoogleBundle
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (!$value instanceof Coordinate) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     $data = [$value->getLatitude(), $value->getLongitude(), $value->isNoWrap()];
     return parent::convertToDatabaseValue($data, $platform);
 }
コード例 #5
0
 /**
  * {@inheritdoc}
  *
  * @param TrackingId|null $value
  * @param AbstractPlatform $platform
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return null;
     }
     if ($value instanceof TrackingId) {
         return $value->toString();
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #6
0
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return null;
     }
     if (!is_array($value)) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return Coder::encode($value);
 }
コード例 #7
0
ファイル: ShortidType.php プロジェクト: pugx/shortid-doctrine
 /**
  * {@inheritdoc}
  *
  * @param ShortId|null                              $value
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return;
     }
     if ($value instanceof ShortId || ShortId::isValid($value)) {
         return $value;
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #8
0
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof Price) {
         return (string) PricePresentation::stringifyPrice($value);
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #9
0
ファイル: UuidType.php プロジェクト: brambravo/webtrees
 /**
  * {@inheritdoc}
  *
  * @param Uuid|null                                 $value
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof Uuid) {
         return (string) $value;
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #10
0
 /**
  * {@inheritdoc}
  *
  * @param Uuid|null                                 $value
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof Uuid || Uuid::isValid($value)) {
         return $value->getBytes();
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #11
0
ファイル: MoneyType.php プロジェクト: lakhman/TbbcMoneyBundle
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof Money) {
         return (string) $value->getCurrency() . ' ' . $value->getAmount();
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #12
0
 /**
  * Converts a value from its database representation to its PHP representation
  * of this type.
  *
  * @param mixed $value The value to convert.
  * @param AbstractPlatform $platform The currently used database platform.
  * @return mixed The PHP representation of the value.
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (is_string($value)) {
         $value = fopen('data://text/plain;base64,' . base64_encode($value), 'r');
     } else {
         if (!is_resource($value)) {
             throw ConversionException::conversionFailed($value, self::BLOB);
         }
     }
     return $value;
 }
コード例 #13
0
ファイル: IpType.php プロジェクト: darsyn/ip
 /**
  * Convert to Database Value
  *
  * @access public
  * @param \Darsyn\IP\IP $value
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  * @throws \Doctrine\DBAL\Types\ConversationException
  * @return void
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return;
     }
     try {
         return (string) ($value instanceof IP ? $value : new IP($value));
     } catch (InvalidIpAddressException $e) {
         throw ConversionException::conversionFailed($value, static::NAME);
     }
 }
コード例 #14
0
ファイル: VarDateTimeType.php プロジェクト: rdohms/dbal
 /**
  * @throws ConversionException
  * @param string $value
  * @param AbstractPlatform $platform
  * @return DateTime
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value instanceof \DateTime) {
         return $value;
     }
     $val = date_create($value);
     if (!$val) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #15
0
ファイル: DateType.php プロジェクト: michaelnavarro/zc
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $val = \DateTime::createFromFormat('!' . $platform->getDateFormatString(), $value);
     if (!$val) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #16
0
ファイル: UtcDateTimeTrait.php プロジェクト: DavidHavl/Ajasta
 /**
  * @param  string|null $value
  * @param  string      $formatString
  * @return DateTime|null
  * @throws ConversionException
  */
 public function doConvertToPHPValue($value, $formatString)
 {
     if ($value === null) {
         return null;
     }
     $dateTime = DateTime::createFromFormat('!' . $formatString, $value, self::$utcTimezone ?: (self::$utcTimezone = new DateTimeZone('utc')));
     if (!$dateTime) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $dateTime;
 }
コード例 #17
0
ファイル: UuidBinaryType.php プロジェクト: Briareos/Undine
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     try {
         return \Undine\Functions\uuid_to_binary($value);
     } catch (Uuid1TransformException $e) {
         throw ConversionException::conversionFailed($value, self::NAME);
     }
 }
コード例 #18
0
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc ? self::$utc : (self::$utc = new \DateTimeZone(\DateTimeZone::UTC)));
     if (!$val) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #19
0
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $dateTimeFormatString = \Zend_Locale_Format::convertPhpToIsoFormat($platform->getDateTimeFormatString());
     $val = new \Zend_Date($value, $dateTimeFormatString);
     if (!$val) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #20
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (null === $value || $value instanceof PhoneNumber) {
         return $value;
     }
     $util = PhoneNumberUtil::getInstance();
     try {
         return $util->parse($value, PhoneNumberUtil::UNKNOWN_REGION);
     } catch (NumberParseException $e) {
         throw ConversionException::conversionFailed($value, self::NAME);
     }
 }
コード例 #21
0
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return;
     }
     if (!is_string($value)) {
         throw ConversionException::conversionFailed($value, self::NAME);
     }
     $foo = new Foo();
     $foo->bar = $value;
     return $foo;
 }
コード例 #22
0
ファイル: ArrayType.php プロジェクト: shinichi81/laravel4demo
 public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     $value = is_resource($value) ? stream_get_contents($value) : $value;
     $val = unserialize($value);
     if ($val === false && $value != 'b:0;') {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #23
0
 /**
  * Converts a value from its PHP representation to its database representation
  * of this type.
  *
  * @param mixed $value The value to convert.
  * @param AbstractPlatform $platform The currently used database platform.
  * @return mixed The database representation of the value.
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return null;
     }
     if (null === self::$typeRegistry) {
         self::$typeRegistry = new TypeRegistry();
     }
     if (!$value instanceof PhpType) {
         throw ConversionException::conversionFailed($value, 'string');
     }
     return $this->convertToString($value);
 }
コード例 #24
0
ファイル: MoneyType.php プロジェクト: Codixis/CSBill
 /**
  * {@inheritdoc}
  */
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (null === $value) {
         return 0;
     }
     if ($value instanceof Money) {
         return $value->getAmount();
     }
     if ((int) $value > 0) {
         return (int) $value;
     }
     throw ConversionException::conversionFailed($value, self::NAME);
 }
コード例 #25
0
ファイル: HStoreType.php プロジェクト: molaux/hstore-bundle
 public function convertToDatabaseValue($value, AbstractPlatform $platform)
 {
     if (is_null($value)) {
         return null;
     }
     if (!is_array($value)) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     $parts = array();
     foreach ($value as $key => $value) {
         $parts[] = '"' . addcslashes($key, self::ESCAPE) . '"' . '=>' . ($value === null ? 'NULL' : '"' . addcslashes($value, self::ESCAPE) . '"');
     }
     return join(',', $parts);
 }
コード例 #26
0
 /**
  * @param mixed $value
  * @param Platforms\AbstractPlatform $platform
  *
  * @return \DateTime|NULL
  *
  * @throws Types\ConversionException
  */
 public function convertToPHPValue($value, Platforms\AbstractPlatform $platform)
 {
     if ($value === NULL) {
         return NULL;
     }
     if (self::$utc === NULL) {
         self::$utc = new \DateTimeZone('UTC');
     }
     $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc);
     if (!$val) {
         throw Types\ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #27
0
ファイル: JsonType.php プロジェクト: bburnichon/dbal
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null || $value === '') {
         return null;
     }
     if (is_resource($value)) {
         $value = stream_get_contents($value);
     }
     $val = json_decode($value, true);
     if (json_last_error() !== JSON_ERROR_NONE) {
         throw ConversionException::conversionFailed($value, $this->getName());
     }
     return $val;
 }
コード例 #28
0
ファイル: HstoreType.php プロジェクト: ramunasd/doctrine-psql
 /**
  * @param string $value
  * @param AbstractPlatform $platform
  * @return Hstore|null
  * @throws ConversionException
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     if (empty($value)) {
         return new Hstore();
     }
     try {
         $store = Hstore::fromString($value);
     } catch (\Exception $e) {
         throw ConversionException::conversionFailed($value, self::NAME);
     }
     return $store;
 }
コード例 #29
0
 /**
  * Converts a value from its database representation to its PHP representation
  *
  * @param mixed            $value    The value to convert
  * @param AbstractPlatform $platform The currently used database platform
  *
  * @return mixed
  *
  * @throws ConversionException When the conversion fails
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof StdString) {
         return $value;
     }
     try {
         $string = StdString::create($value);
     } catch (Exception $exception) {
         throw ConversionException::conversionFailed($value, static::TYPE_NAME);
     }
     return $string;
 }
コード例 #30
0
ファイル: MoneyType.php プロジェクト: novuso/common-bundle
 /**
  * Converts a value from its database representation to its PHP representation
  *
  * @param mixed            $value    The value to convert
  * @param AbstractPlatform $platform The currently used database platform
  *
  * @return mixed
  *
  * @throws ConversionException When the conversion fails
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if (empty($value)) {
         return null;
     }
     if ($value instanceof Money) {
         return $value;
     }
     try {
         $money = Money::fromString($value);
     } catch (Exception $exception) {
         throw ConversionException::conversionFailed($value, static::TYPE_NAME);
     }
     return $money;
 }