/** * 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); }
/** * {@inheritdoc} */ public static function fromString($id) { assert(Test::isString($id), sprintf('%s expects $id to be a string', __METHOD__)); return new static($id); }
/** * Creates instance from a date/time string * * @param string $dateTime The date/time string * * @return DateTime * * @throws DomainException When the string is invalid */ public static function fromString($dateTime) { assert(Test::isString($dateTime), sprintf('%s expects $dateTime to be a string; received (%s) %s', __METHOD__, gettype($dateTime), VarPrinter::toString($dateTime))); $pattern = sprintf('/\\A%s-%s-%sT%s:%s:%s\\.%s\\[%s\\]\\z/', '(?P<year>[\\d]{4})', '(?P<month>[\\d]{2})', '(?P<day>[\\d]{2})', '(?P<hour>[\\d]{2})', '(?P<minute>[\\d]{2})', '(?P<second>[\\d]{2})', '(?P<micro>[\\d]{6})', '(?P<timezone>.+)'); if (!preg_match($pattern, $dateTime, $matches)) { $message = sprintf('%s expects $dateTime in "Y-m-d\\TH:i:s.u[timezone]" format', __METHOD__); throw DomainException::create($message); } $year = (int) $matches['year']; $month = (int) $matches['month']; $day = (int) $matches['day']; $hour = (int) $matches['hour']; $minute = (int) $matches['minute']; $second = (int) $matches['second']; $micro = (int) $matches['micro']; $timezone = $matches['timezone']; return new self(Date::create($year, $month, $day), Time::create($hour, $minute, $second, $micro), Timezone::create($timezone)); }
/** * Creates instance from a base URI and relative reference * * @SuppressWarnings(PHPMD) * * @see http://tools.ietf.org/html/rfc3986#section-5.2 * * @param Uri|string $base A Uri instance or string * @param string $reference A relative URI reference * @param bool $strict Whether or not to enable strict parsing * * @return Uri * * @throws DomainException When the base or reference are invalid */ public static function resolve($base, $reference, $strict = true) { if (!$base instanceof self) { $base = static::parse($base); } assert(Test::isString($reference), sprintf('%s expects $reference to be a string; received (%s) %s', __METHOD__, gettype($reference), VarPrinter::toString($reference))); preg_match(self::URI_PATTERN, $reference, $matches); $ref = static::componentsFromMatches($matches); // http://tools.ietf.org/html/rfc3986#section-5.2.2 // A non-strict parser may ignore a scheme in the reference if it is // identical to the base URI's scheme if (!$strict && ($ref['scheme'] !== null && $base->scheme() === $ref['scheme'])) { $ref['scheme'] = null; } if ($ref['scheme'] !== null) { $scheme = $ref['scheme']; $authority = $ref['authority']; $path = static::removeDotSegments($ref['path']); $query = $ref['query']; } else { // http://tools.ietf.org/html/rfc3986#section-3.3 // In addition, a URI reference (Section 4.1) may be a // relative-path reference, in which case the first path segment // cannot contain a colon (":") character. // START: extra check for colon in first segment $segments = explode('/', trim($ref['path'], '/')); if (isset($segments[0]) && strpos($segments[0], ':') !== false) { $message = sprintf('First segment in reference (%s) cannot contain a colon (":")', $reference); throw DomainException::create($message); } // END: extra check for colon in first segment if ($ref['authority'] !== null) { $authority = $ref['authority']; $path = static::removeDotSegments($ref['path']); $query = $ref['query']; } else { if ($ref['path'] === '') { $path = $base->path(); if ($ref['query'] !== null) { $query = $ref['query']; } else { $query = $base->query(); } } else { if ($ref['path'][0] === '/') { $path = static::removeDotSegments($ref['path']); } else { $path = static::mergePaths($base, $ref['path']); $path = static::removeDotSegments($path); } $query = $ref['query']; } $authority = $base->authority(); } $scheme = $base->scheme(); } $fragment = $ref['fragment']; return new static($scheme, $authority, $path, $query, $fragment); }
/** * Creates instance from a timezone string * * @param string $timezone The timezone string * * @return Timezone * * @throws DomainException When the string is invalid */ public static function fromString($timezone) { assert(Test::isString($timezone), sprintf('%s expects $timezone to be a string; received (%s) %s', __METHOD__, gettype($timezone), VarPrinter::toString($timezone))); return new self($timezone); }
/** * Creates instance from a locale string * * @param string $locale The locale string * * @return LocaleFormatter */ public static function fromLocale($locale) { assert(Test::isString($locale), sprintf('%s expects $locale to be a string; received (%s) %s', __METHOD__, gettype($locale), VarPrinter::toString($locale))); $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); return new self($formatter); }
/** * Creates instance from a byte string * * @param string $bytes The UUID bytes * * @return Uuid * * @throws DomainException When the bytes string is not valid */ public static function fromBytes($bytes) { assert(Test::isString($bytes), sprintf('%s expects $bytes to be a string; received (%s) %s', __METHOD__, gettype($bytes), VarPrinter::toString($bytes))); if (strlen($bytes) !== 16) { $message = sprintf('%s expects $bytes to be a 16-byte string', __METHOD__); throw DomainException::create($message); } $steps = []; foreach (range(0, 15) as $step) { $steps[] = sprintf('%02x', ord($bytes[$step])); if (in_array($step, [3, 5, 7, 9])) { $steps[] = '-'; } } return self::parse(implode('', $steps)); }
/** * Creates instance from a time string * * @param string $time The time string * * @return Time * * @throws DomainException When the string is invalid */ public static function fromString($time) { assert(Test::isString($time), sprintf('%s expects $time to be a string; received (%s) %s', __METHOD__, gettype($time), VarPrinter::toString($time))); $pattern = '/\\A(?P<hour>[\\d]{2}):(?P<minute>[\\d]{2}):(?P<second>[\\d]{2}).(?P<micro>[\\d]{6})\\z/'; if (!preg_match($pattern, $time, $matches)) { $message = sprintf('%s expects $time in "H:i:s.u" format', __METHOD__); throw DomainException::create($message); } $hour = (int) $matches['hour']; $minute = (int) $matches['minute']; $second = (int) $matches['second']; $micro = (int) $matches['micro']; return new self($hour, $minute, $second, $micro); }
/** * Creates instance from a date string * * @param string $date The date string * * @return Date * * @throws DomainException When the string is invalid */ public static function fromString($date) { assert(Test::isString($date), sprintf('%s expects $date to be a string; received (%s) %s', __METHOD__, gettype($date), VarPrinter::toString($date))); $pattern = '/\\A(?P<year>[\\d]{4})-(?P<month>[\\d]{2})-(?P<day>[\\d]{2})\\z/'; if (!preg_match($pattern, $date, $matches)) { $message = sprintf('%s expects $date in "Y-m-d" format', __METHOD__); throw DomainException::create($message); } $year = (int) $matches['year']; $month = (int) $matches['month']; $day = (int) $matches['day']; return new self($year, $month, $day); }