/** * 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 $value * @throws Exception\InvalidTypeException * @return AbstractDictionary */ public static function fromJsonDecodedData($value) { $prototypes = static::getPropertyPrototypes(); $propertyNames = array_keys($prototypes); if (!is_array($value)) { throw InvalidTypeException::fromMessageAndPrototype("Value must be an array", static::prototype()); } $valueKeys = array_keys($value); try { if ($valueKeys != $propertyNames) { foreach (array_keys($value) as $propertyName) { Assertion::inArray($propertyName, $propertyNames); } foreach ($propertyNames as $propertyName) { Assertion::inArray($propertyName, array_keys($value)); } } foreach ($value as $propertyName => $encodedProperty) { $propertyPrototype = $prototypes[$propertyName]; $propertyClass = $propertyPrototype->of(); try { $value[$propertyName] = $propertyClass::fromJsonDecodedData($encodedProperty); } catch (\Exception $ex) { throw InvalidTypeException::fromMessageAndPrototype(sprintf('Failed to create property %s from json. Error: %s', $propertyName, $ex->getMessage()), static::prototype()); } } return new static($value); } catch (\InvalidArgumentException $ex) { throw InvalidTypeException::fromInvalidArgumentExceptionAndPrototype($ex, static::prototype()); } }