setReaders($this->getOption(self::CONFIG_SOURCE)); $this->setWriters($this->getOption(self::CONFIG_DESTINATION)); } /** * Read all values from readers and store it into array of $name => reader $value * Throw exception if at least one reader cannot read value * * @param array $dataSource * @return array All collected data from $this->readers * @throws MetadataInjectorReadException */ public function read(array $dataSource) { $data = $errors = []; foreach ($this->readers as $name => $reader) { try { $data[$name] = $reader->getValue($dataSource); } catch (MetadataReaderNotFoundException $e) { $errors[$name] = $e->getMessage(); } } if (! empty($errors)) { foreach ($errors as $name => $error) { \common_Logger::d('Error on injector "' . __CLASS__ . '" with reader "' . $name . '" : ' . $error); } throw new MetadataInjectorReadException( 'Injector "' . __CLASS__ . '" cannot read all required values from readers: ' . implode(', ', array_keys($errors)) ); } return $data; } /** * Write $data values using $this->writers * * @param \core_kernel_classes_Resource $resource * @param array $data * @param bool $dryrun * @return bool * @throws MetadataInjectorWriteException */ public function write(\core_kernel_classes_Resource $resource, array $data, $dryrun = false) { $writers = $errors = []; foreach ($this->writers as $name => $writer) { try { $value = $writer->format($data); if ($writer->validate($value)) { $writers[$name] = $writer; } else { $errors[$name] = 'Writer "' . $name . '" cannot validate value.'; } } catch (MetadataReaderNotFoundException $e) { $errors[$name] = 'Writer "' . $name . '" cannot format value: ' . $e->getMessage(); } } foreach ($writers as $name => $writer) { if (! $writer instanceof OntologyWriter) { $errors[$name] = __CLASS__ . ' must implements ' . OntologyWriter::class; continue; } try { $writer->write($resource, $data, $dryrun); } catch (MetadataWriterException $e) { $errors[$name] = $e->getMessage(); } } if (! empty($errors)) { foreach ($errors as $name => $error) { \common_Logger::d('Error on injector "' . __CLASS__ . '" with writer "' . $name . '" : ' . $error); } throw new MetadataInjectorWriteException( 'Injector "' . __CLASS__ . '" cannot write values from writers: ' . implode(', ', array_keys($errors)) ); } return true; } /** * Set $this->readers with Reader instance * * @param array $readers * @throws InconsistencyConfigException */ protected function setReaders(array $readers) { foreach ($readers as $name => $options) { $this->readers[$name] = new KeyReader($options); } } /** * Set $this->writers with OntologyWriter instance * * @param array $writers */ protected function setWriters(array $writers) { foreach ($writers as $name => $destination) { $this->writers[$name] = $this->buildService($destination); } } /** * To configuration serialization * * @return string */ public function __toPhpCode() { $source = ''; if (! empty($this->readers)) { foreach ($this->readers as $reader) { $source .= \common_Utils::toHumanReadablePhpString($reader, 2) . PHP_EOL; } } $destination = ''; if (! empty($this->writers)) { foreach ($this->writers as $writer) { $destination .= \common_Utils::toHumanReadablePhpString($writer, 2) . PHP_EOL; } } $params = [self::CONFIG_SOURCE => $this->readers, self::CONFIG_DESTINATION => $this->writers]; return 'new ' . get_class($this) . '(' . \common_Utils::toHumanReadablePhpString($params, 1) . '),'; } }