getProperty(GenerisRdf::PROPERTY_ALIAS); $classProperties = $class->getProperties(true); $classUri = $class->getUri(); $metaDataWithAlias = $this->getMetadataWithAlias($metadata); $errors = []; if (empty($metaDataWithAlias)) { return $metadata; } foreach ($classProperties as $property) { $aliasName = (string)$property->getOnePropertyValue($aliasProperty); if (empty($aliasName)) { continue; } foreach ($metaDataWithAlias as $metaDataItem) { if ($metaDataItem->getAlias() === $aliasName) { $propertyUri = $this->getPropertyUri($property, $classUri, $aliasName); $metaDataItem->setPropertyUri($propertyUri); $errors = $this->validate($property, $aliasName, $metaDataItem->getValue(), $errors); $result[$propertyUri] = $metaDataItem->getValue(); break; } } } if (empty($errors)) { return $result; } $messages = []; foreach ($errors as $alias => $message) { $messages[] = '"' . $alias . '" ' . $message; } //@TODO In the future, create one separate error for each metadata. Requires new translations throw new AggregatedValidationException( [ new ErrorValidationException( 'Metadata are not correct: %s', [ implode('. ', $messages) ] ) ], [] ); } private function validate( core_kernel_classes_Property $property, string $alias, string $value, array $errors ): array { $element = $this->getElementMapFactory()->create($property); $element->setValue($value); if (!$element->validate()) { $errors[$alias] = $element->getError(); } return $errors; } private function isEmptyAlias(ParsedMetadatum $metadata): bool { return empty($metadata->getPropertyUri()); } /** * @param ParsedMetadatum[] $metadata * * @return ParsedMetadatum[] */ private function getMetadataWithAlias(array $metadata): array { return array_filter($metadata, [$this, 'isEmptyAlias']); } private function add(string $classUri, string $aliasName, string $aliasUri): void { $this->cache[$classUri . $aliasName] = $aliasUri; if (count($this->cache) > self::MAX_CACHE_SIZE) { array_shift($this->cache); } } private function getCached(string $classUri, string $aliasName): ?string { return $this->cache[$classUri . $aliasName] ?? null; } private function getPropertyUri(core_kernel_classes_Property $property, string $classUri, string $aliasName): ?string { $cachedPropertyUri = $this->getCached($classUri, $aliasName); if (null !== $cachedPropertyUri) { return $cachedPropertyUri; } $this->add($classUri, $aliasName, $property->getUri()); return $property->getUri(); } private function getElementMapFactory(): ElementMapFactory { return $this->getServiceManager()->get(ElementMapFactory::class); } }