Ejemplo n.º 1
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 Currency) {
         return $value;
     }
     try {
         $currency = Currency::fromValue($value);
     } catch (Exception $exception) {
         throw ConversionException::conversionFailed($value, static::TYPE_NAME);
     }
     return $currency;
 }
Ejemplo n.º 2
0
 /**
  * Creates instance from a string representation
  *
  * @param string $value The money string
  *
  * @return Money
  *
  * @throws DomainException When the money string is not valid
  */
 public static function fromString(string $value) : Money
 {
     $pattern = '/\\A(?P<code>[A-Z]{3}):(?P<amount>-?[\\d]+)\\z/';
     if (!preg_match($pattern, $value, $matches)) {
         $message = sprintf('%s expects $value in "CUR:0000" format', __METHOD__);
         throw new DomainException($message);
     }
     /** @var int $amount */
     $amount = (int) $matches['amount'];
     /** @var Currency $currency */
     $currency = Currency::fromValue($matches['code']);
     return new static($amount, $currency);
 }
Ejemplo n.º 3
0
 /**
  * Creates instance from a money string
  *
  * @param string $money The money string
  *
  * @return Money
  *
  * @throws DomainException When the string is invalid
  */
 public static function fromString($money)
 {
     assert(Test::isString($money), sprintf('%s expects $money to be a string; received (%s) %s', __METHOD__, gettype($money), VarPrinter::toString($money)));
     $pattern = '/\\A(?P<code>[A-Z]{3}):(?P<amount>-?[\\d]+)\\z/';
     if (!preg_match($pattern, $money, $matches)) {
         $message = sprintf('Format must include currency and amount (eg. USD:1725); received "%s"', $money);
         throw DomainException::create($message);
     }
     $amount = (int) $matches['amount'];
     $currency = Currency::fromValue($matches['code']);
     return new self($amount, $currency);
 }