protected function guardId($id) { if (!preg_match('/\\A[0-9]{3}-[0-9]{10}\\z/', $id)) { $message = sprintf('Invalid ISBN: %s', $id); throw DomainException::create($message); } }
/** * Constructs Timezone * * @internal * * @param mixed $value The timezone value * * @throws DomainException When the value is not a valid timezone */ private function __construct($value) { if (!Test::isTimezone($value)) { $message = sprintf('Invalid timezone: %s', VarPrinter::toString($value)); throw DomainException::create($message); } if ($value instanceof DateTimeZone) { $value = $value->getName(); } $this->value = (string) $value; }
public static function fromString($name) { $parts = explode(' ', trim($name)); if (count($parts) < 2) { $message = sprintf('%s expects at least first and last name', __METHOD__); throw DomainException::create($message); } if (count($parts) === 2) { return new self($parts[0], $parts[1]); } return new self($parts[0], $parts[1], $parts[2]); }
/** * 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)); }
/** * Validates and normalizes the host * * @param string|null $host The host * * @return string|null * * @throws DomainException When the host is invalid */ protected static function normalizeHost($host) { if ($host === null) { return null; } if ($host === '') { return ''; } if (!static::isValidHost($host)) { $message = sprintf('Invalid host: %s', VarPrinter::toString($host)); throw DomainException::create($message); } // Although host is case-insensitive, producers and normalizers should // use lowercase for registered names and hexadecimal addresses for the // sake of uniformity, while only using uppercase letters for // percent-encodings. $host = mb_strtolower($host, 'UTF-8'); return static::encodeHost(static::decode($host, static::UNRESERVED_SET)); }
/** * Validates another monetary value uses the same currency * * @param Money $other The other monetary value * * @return void * * @throws DomainException When the other money uses a different currency */ private function guardCurrency(Money $other) { if (!$this->isSameCurrency($other)) { throw DomainException::create('Math and comparison operations require the same currency'); } }
/** * Validates a node hexadecimal string * * @param string $node The node * * @return void * * @throws DomainException When the node is not 6 hexadecimal octets */ private static function guardNode($node) { if (!preg_match('/\\A[a-f0-9]{12}\\z/', $node)) { $message = 'Node must be a 12 character hexadecimal string'; throw DomainException::create($message); } }
/** * Validates the time * * @param int $hour The hour * @param int $minute The minute * @param int $second The second * @param int $micro The microsecond * * @return void * * @throws DomainException When the time is not valid */ private function guardTime($hour, $minute, $second, $micro) { if ($hour < self::MIN_HOUR || $hour > self::MAX_HOUR) { $message = sprintf('Hour (%d) out of range[%d, %d]', $hour, self::MIN_HOUR, self::MAX_HOUR); throw DomainException::create($message); } if ($minute < self::MIN_MINUTE || $minute > self::MAX_MINUTE) { $message = sprintf('Minute (%d) out of range[%d, %d]', $minute, self::MIN_MINUTE, self::MAX_MINUTE); throw DomainException::create($message); } if ($second < self::MIN_SECOND || $second > self::MAX_SECOND) { $message = sprintf('Second (%d) out of range[%d, %d]', $second, self::MIN_SECOND, self::MAX_SECOND); throw DomainException::create($message); } if ($micro < self::MIN_MICRO || $micro > self::MAX_MICRO) { $message = sprintf('Microsecond (%d) out of range[%d, %d]', $micro, self::MIN_MICRO, self::MAX_MICRO); throw DomainException::create($message); } }
/** * Validates the date * * @param int $year The year * @param int $month The month * @param int $day The day * * @return void * * @throws DomainException When the date is not valid */ private function guardDate($year, $month, $day) { if (!checkdate($month, $day, $year)) { $message = sprintf('Invalid date: %04d-%02d-%02d', $year, $month, $day); throw DomainException::create($message); } }