public function test_that_to_string_returns_expected_string_for_resource() { $expected = 'Resource(stream)'; $resource = fopen(__FILE__, 'r'); $this->assertSame($expected, VarPrinter::toString($resource)); fclose($resource); }
/** * {@inheritdoc} */ public static function deserialize(array $data) : QueryMessage { $keys = ['id', 'type', 'timestamp', 'meta_data', 'payload_type', 'payload']; foreach ($keys as $key) { if (!isset($data[$key])) { $message = sprintf('Invalid serialization data: %s', VarPrinter::toString($data)); throw new DomainException($message); } } if ($data['type'] !== MessageType::QUERY) { $message = sprintf('Invalid message type: %s', $data['type']); throw new DomainException($message); } /** @var MessageId $id */ $id = MessageId::fromString($data['id']); /** @var DateTime $timestamp */ $timestamp = DateTime::fromString($data['timestamp']); /** @var MetaData $metaData */ $metaData = MetaData::create($data['meta_data']); /** @var Type $payloadType */ $payloadType = Type::create($data['payload_type']); /** @var string $payloadClass */ $payloadClass = $payloadType->toClassName(); assert(Validate::implementsInterface($payloadClass, Query::class), sprintf('Unable to deserialize: %s', $payloadClass)); /** @var Query $payload */ $payload = $payloadClass::fromArray($data['payload']); return new static($id, $timestamp, $payload, $metaData); }
/** * {@inheritdoc} */ public function get($id) { if (!isset($this->services[$id])) { $message = sprintf('Entry (%s) is not defined', VarPrinter::toString($id)); throw EntryNotFoundException::create($message); } return $this->services[$id]($this); }
/** * {@inheritdoc} */ public function get($key) { $node = $this->nodeGet($key, $this->root); if ($node === null) { $message = sprintf('Key not found: %s', VarPrinter::toString($key)); throw new KeyException($message); } return $node->value(); }
public static function fromArray(array $data) : UserByEmailQuery { $keys = ['email']; foreach ($keys as $key) { if (!array_key_exists($key, $data)) { $message = sprintf('Invalid serialization format: %s', VarPrinter::toString($data)); throw new DomainException($message); } } return new static($data['email']); }
/** * Constructs Timezone * * @param mixed $value The timezone value * * @throws DomainException When the value is not a valid timezone */ public function __construct($value) { if (!Validate::isTimezone($value)) { $message = sprintf('Invalid timezone: %s', VarPrinter::toString($value)); throw new DomainException($message); } if ($value instanceof DateTimeZone) { $value = $value->getName(); } $this->value = (string) $value; }
public static function fromArray(array $data) : UserRegisteredEvent { $keys = ['prefix', 'first_name', 'middle_name', 'last_name', 'suffix', 'email']; foreach ($keys as $key) { if (!array_key_exists($key, $data)) { $message = sprintf('Invalid serialization format: %s', VarPrinter::toString($data)); throw new DomainException($message); } } return new static($data['email'], $data['first_name'], $data['last_name'], $data['middle_name'], $data['prefix'], $data['suffix']); }
/** * Retrieves the fully qualified class name of an object * * @param object|string $object An object, fully qualified class name, or * canonical class name * * @return string * * @throws TypeException When $object is not a string or object */ public static function full($object) : string { if (is_string($object)) { return str_replace('.', '\\', $object); } if (is_object($object)) { return trim(get_class($object), '\\'); } $message = sprintf('%s expects $object to be an object or string; received (%s) %s', __METHOD__, gettype($object), VarPrinter::toString($object)); throw new TypeException($message); }
/** * Created StdString instance * * @param mixed $value The string value * * @return StdString * * @throws DomainException When the value is not valid */ function stdString($value) : StdString { if (!Validate::isStringCastable($value)) { $message = sprintf('Invalid string value: %s', VarPrinter::toString($value)); throw new DomainException($message); } if ($value instanceof StdString) { return $value; } /** @var StdString $string */ $string = StdString::create((string) $value); return $string; }
/** * Removes a key/value pair * * @param string $key The key * * @return void */ public function offsetUnset($key) { assert(Validate::isString($key), sprintf('Invalid metadata key: (%s) %s', gettype($key), VarPrinter::toString($key))); $this->remove($key); }
/** * {@inheritdoc} */ public function toString() : string { return sprintf('{%s}', Collection::create()->push(sprintf('id:%s', $this->id->toString()))->push(sprintf('type:%s', $this->type->value()))->push(sprintf('timestamp:%s', $this->timestamp->toString()))->push(sprintf('meta_data:{%s}', Collection::create($this->metaData->toArray())->implode(',', function ($value, $key) { return sprintf('%s:%s', $key, VarPrinter::toString($value)); })))->push(sprintf('payload_type:%s', $this->payloadType->toString()))->push(sprintf('payload:{%s}', Collection::create($this->payload->toArray())->implode(',', function ($value, $key) { return sprintf('%s:%s', $key, VarPrinter::toString($value)); })))->implode(',')); }
/** * 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); }
/** * {@inheritdoc} */ public function offsetUnset($index) { assert(Validate::isInt($index), sprintf('Invalid list index: %s', VarPrinter::toString($index))); $this->remove($index); }
/** * Validates enum constants * * @param ReflectionClass $reflection The reflection instance * * @return void * * @throws DomainException When more than one constant has the same value */ private static final function guardConstants(ReflectionClass $reflection) { $constants = $reflection->getConstants(); $duplicates = []; foreach ($constants as $value) { $names = array_keys($constants, $value, true); if (count($names) > 1) { $duplicates[VarPrinter::toString($value)] = $names; } } if (!empty($duplicates)) { $list = array_map(function ($names) use($constants) { return sprintf('(%s)=%s', implode('|', $names), VarPrinter::toString($constants[$names[0]])); }, $duplicates); $message = sprintf('Duplicate enum values: %s', implode(', ', $list)); throw new DomainException($message); } }
/** * Retrieves a value by key * * @param string $key The key * * @return mixed * * @throws KeyException When the key is not defined */ public function get($key) { if (!array_key_exists($key, $this->data)) { $message = sprintf('Key not found: %s', VarPrinter::toString($key)); throw KeyException::create($message); } return $this->data[$key]; }
/** * 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)); }
/** * Retrieves a value by key * * @param mixed $key The key * * @return mixed * * @throws KeyException When the key is not found */ public function get($key) { $bucket = $this->locate($key); if ($bucket === null) { $message = sprintf('Key not found: %s', VarPrinter::toString($key)); throw new KeyException($message); } return $bucket->value(); }
/** * 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 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)); }
/** * Validates monetary operand is an integer or float * * @param mixed $operand The operand * * @return void * * @throws DomainException When operand is not an int or float */ protected function guardOperand($operand) { if (!is_int($operand) && !is_float($operand)) { $message = sprintf('Operand must be an integer or float; received (%s) %s', gettype($operand), VarPrinter::toString($operand)); throw new DomainException($message); } }
/** * 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)); }
/** * Retrieves the value type error message * * @param string $method The calling method * @param mixed $value The value * * @return string */ protected function valueTypeError(string $method, $value) : string { $valueType = is_object($value) ? get_class($value) : gettype($value); return sprintf('%s::%s expects value type (%s); received (%s) %s', static::class, $method, $this->valueType(), $valueType, VarPrinter::toString($value)); }
/** * Retrieves the item type error message * * @param string $method The calling method * @param mixed $item The item * * @return string */ protected function itemTypeError(string $method, $item) : string { $itemType = is_object($item) ? get_class($item) : gettype($item); return sprintf('%s::%s expects item type (%s); received (%s) %s', static::class, $method, $this->itemType(), $itemType, VarPrinter::toString($item)); }
/** * {@inheritdoc} */ public function get($key) { $hash = Hasher::hash($key); if (!isset($this->buckets[$hash])) { $message = sprintf('Key not found: %s', VarPrinter::toString($key)); throw new KeyException($message); } return $this->buckets[$hash]->get($key); }
/** * 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); }
/** * {@inheritdoc} */ public function offsetExists($index) : bool { assert(Validate::isInt($index), sprintf('Invalid character index: %s', VarPrinter::toString($index))); return $this->has($index); }
/** * Validates monetary operand is an integer or float * * @param mixed $operand The operand * * @return void * * @throws TypeException When the operand is not an integer or float */ private function guardOperand($operand) { if (!is_int($operand) && !is_float($operand)) { $message = sprintf('Operand must be an integer or float; received (%s) %s', gettype($operand), VarPrinter::toString($operand)); throw TypeException::create($message); } }
/** * Validates and normalizes the scheme * * @param string|null $scheme The scheme * * @return string * * @throws DomainException When the scheme is invalid */ protected static function normalizeScheme(string $scheme = null) : string { if (!static::isValidScheme($scheme)) { $message = sprintf('Invalid URI scheme: %s', VarPrinter::toString($scheme)); throw new DomainException($message); } return strtolower($scheme); }
/** * 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); }