/** * @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); }
/** * @param TypeSchema $typeSchema */ public function add(TypeSchema $typeSchema) { $this->types[$typeSchema->name()] = $typeSchema; }
/** * @param DBALSchema $schema * @param string $schemaName * @param TypeSchema $type * * @throws DoctrineStorageException */ private function createTable(DBALSchema $schema, string $schemaName, TypeSchema $type) { $table = $schema->createTable($this->tableName($schemaName, $type->name())); $table->addColumn('id', 'guid'); $table->setPrimaryKey(['id']); foreach ($type->getDefinitions(['id']) as $field => $definition) { $this->typeRegistry->map($schemaName, $table, $field, $definition); } }
/** * @param TypeSchema $definition * @return bool * @throws InvalidArgumentException */ public function isValid(TypeSchema $definition) : bool { if (mb_strtolower($definition->name()) !== mb_strtolower($this->typeSchemaName())) { throw InvalidArgumentException::unexpectedModel($definition->name(), $this->typeSchemaName()); } foreach ($this->fields as $fieldName => $value) { $field = $definition->getFieldDefinition($fieldName); if (!$field->isValid($value)) { return false; } } return true; }
/** * @param TypeSchema $schema * @return Builder */ public function addType(TypeSchema $schema) : Builder { $this->types[$schema->name()] = $schema; return $this; }
/** * @param Metadata $metadata * @param TypeSchema $model * @return InvalidArgumentException */ public static function invalidMetadata(Metadata $metadata, TypeSchema $model) : InvalidArgumentException { return new self(sprintf("Metadata \"%s\":\"%s\" does not match model \"%s\".", $metadata->typeSchemaName(), (string) $metadata->id(), $model->name())); }