/** * {@inheritdoc} */ public function build(array $defaults = []) { $builtConfig = $defaults; foreach ($this->data as $key => $value) { $builtConfig[$key] = $value instanceof BuilderInterface ? $value->build(isset($defaults[$key]) ? $defaults[$key] : [])->unwrap() : $value; } if (!$this->schema) { return Ok::unit($builtConfig); } $validationResult = $this->schema->validate($builtConfig); return $validationResult instanceof Error ? $validationResult : Ok::unit($builtConfig); }
/** * @param \Heystack\Core\DataObjectSchema\SchemaInterface $schema * @return void */ public function addSchema(SchemaInterface $schema) { $identifier = strtolower($schema->getIdentifier()->getFull()); if ($schema->getReference()) { $this->referenceSchemas[$identifier] = $schema; } else { if ($this->hasSchema($identifier) && !$schema->getReplace()) { $this->schemas[$identifier]->mergeSchema($schema); } else { $this->schemas[$identifier] = $schema; } } }
protected function validateValue($key, $value) { if (self::isValidValue($value)) { if ($this->schema) { if ($this->schema->has($key)) { $schema_value = $this->schema->get($key); if (!Schema::isValidType($value, $schema_value)) { throw new InvalidValueException("Data has a value at `" . $key . "` which is not of the expected type."); } } else { throw new InvalidKeyException("Data has a key `" . $key . "` that is not in the schema."); } } } else { throw new InvalidValueException("Value at key `" . $key . "` is not valid."); } }