/** * Performs assertions and sets the internal value property on success * * @param mixed $value * @throws Exception\InvalidTypeException * @return void */ protected function setValue($value) { if (!is_bool($value)) { throw InvalidTypeException::fromMessageAndPrototype("Value must be a boolean", static::prototype()); } $this->value = $value; }
/** * Non accessible construct * * Use static factory methods to construct a SingleValue * * @param mixed $value * @throws Exception\InvalidTypeException */ protected function __construct($value) { try { $this->setValue($value); } catch (\InvalidArgumentException $ex) { throw InvalidTypeException::fromInvalidArgumentExceptionAndPrototype($ex, static::prototype()); } }
/** * @param mixed $value * @throws Exception\InvalidTypeException If value is not an array containing only items of related Prooph\Done\Process\Type * @return CollectionType */ public static function fromNativeValue($value) { if (is_array($value)) { $value = new \ArrayIterator($value); } if (!$value instanceof \Traversable) { throw InvalidTypeException::fromMessageAndPrototype("Value must be traversable", static::prototype()); } return new static($value); }
/** * Performs assertions and sets the internal value property on success * * @param mixed $value * @throws Exception\InvalidTypeException * @return void */ protected function setValue($value) { if (!is_string($value)) { throw InvalidTypeException::fromMessageAndPrototype("Value must be a string", static::prototype()); } if (!mb_check_encoding($value, 'UTF-8')) { throw InvalidTypeException::fromMessageAndPrototype("Value must be a UTF-8 encoded", static::prototype()); } $this->value = $value; }
/** * This method checks recursively if the value only consists of arrays and scalar types * * @param $value * @throws Exception\InvalidTypeException */ private function assertIsScalarOrArray($value) { if (!is_scalar($value) && !is_array($value)) { throw InvalidTypeException::fromMessageAndPrototype("An unknown type must at least be a scalar or array value", static::prototype()); } if (is_array($value)) { foreach ($value as $item) { $this->assertIsScalarOrArray($item); } } }
/** * @param array $value * @throws Exception\InvalidTypeException */ protected function __construct(array $value) { $prototypes = static::getPropertyPrototypes(); $properties = array(); foreach ($value as $propertyName => $propertyTypeOrNativeValue) { try { $propertyPrototype = $prototypes[$propertyName]; $propertyTypeClass = $propertyPrototype->of(); if (!$propertyTypeOrNativeValue instanceof $propertyTypeClass) { $propertyTypeOrNativeValue = $propertyTypeClass::fromNativeValue($propertyTypeOrNativeValue); } Assertion::isInstanceOf($propertyTypeOrNativeValue, $propertyTypeClass); $properties[$propertyName] = new Property($propertyName, $propertyTypeOrNativeValue); $value[$propertyName] = $propertyTypeOrNativeValue; } catch (\InvalidArgumentException $ex) { throw InvalidTypeException::fromMessageAndPrototype(sprintf('Failed to create property %s with message %s', $propertyName, $ex->getMessage()), static::prototype()); } } $this->value = $value; $this->properties = $properties; }