/** * @param TypeSchema $type * @param array $data * * @return Metadata * @throws HydrationException */ public function hydrate(TypeSchema $type, array $data = []) : Metadata { if (!array_key_exists('id', $data)) { throw HydrationException::missingId(); } $id = new MetadataId($data['id']); unset($data['id']); $fieldValues = []; foreach ($data as $key => $value) { $field = $type->getFieldDefinition($key); $value = $field->deserialize($value); if ($value instanceof Association) { $value = $this->associate($value); } $fieldValues[$key] = $value; } foreach ($type->getDefinitions(array_keys($data)) as $key => $field) { $fieldValues[$key] = $field->defaultValue(); } return new Metadata($id, $type->name(), $fieldValues); }