/** * {@inheritDoc} */ public function create($data) { if (!is_array($data) && !$data instanceof \Traversable) { throw new InvalidValueException('Invalid data to create a map. Only traversable structures allowed'); } $map = new StrictMap($this->keyType->getTypeCheck(), $this->valueType->getTypeCheck()); foreach ($data as $entry) { if (is_object($entry)) { $entry = (array) $entry; } else { if (!is_array($entry)) { throw new InvalidValueException('Invalid data to create a map. Each entry must be an array or object'); } } if (!array_key_exists('key', $entry)) { throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "key" key'); } if (!array_key_exists('value', $entry)) { throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "value" key'); } try { $key = $this->keyType->create($entry['key']); } catch (\Exception $e) { throw new InvalidValueException('One of entry of map contains invalid "key"', 0, $e); } try { $value = $this->valueType->create($entry['value']); } catch (\Exception $e) { throw new InvalidValueException('One of entry of map contains invalid "value"', 0, $e); } $map->add($key, $value); } return $map; }
/** * Sets default value of argument and marks it as "has default value" * * @param mixed $defaultValue * @throws \BadMethodCallException when argument is nullable * @return self */ public function setDefaultValue($defaultValue) { if ($this->isNullable) { throw new \BadMethodCallException('Cannot set default value for argument since it\'s nullable'); } $this->hasDefaultValue = true; if ($this->type->isTargetType($defaultValue)) { $defaultValue = $this->type->create($defaultValue); } $this->defaultValue = $defaultValue; return $this; }
/** * {@inheritDoc} */ public function create($data) { if (!$this->isValidData($data)) { throw new InvalidValueException('Invalid data to create collection. Array and Traversable objects allowed'); } $collection = array(); foreach ($data as $value) { if (!$this->type->isTargetType($value)) { $value = $this->type->create($value); } $collection[] = $value; } return $collection; }
/** * {@inheritDoc} */ public function create($data) { if ($data === null) { if ($this->isNullable) { return null; } if ($this->hasDefaultValue) { return $this->defaultValue; } } return $this->type->create($data); }