item = $item; } /** * Add column to export * Check if core_kernel_classes_Property exists into $config * * @param $column * @param array $config * @throws ExtractorException * @return Extractor $this */ public function addColumn($column, array $config) { if (!isset($config['property'])) { throw new ExtractorException('Property config is missing.'); } $property = new \core_kernel_classes_Property($config['property']); if (!$property->exists()) { throw new ExtractorException('Property config is not a valid property uri.'); } $config['property'] = $property; $this->columns[$column] = $config; return $this; } /** * Get ontology values of requested item properties * * @return $this * @throws \Exception */ public function run() { $this->data = []; if (empty($this->item) || !($this->item instanceof \core_kernel_classes_Resource)) { throw new ExtractorException('Export item not set.'); } $properties = []; foreach ($this->columns as $config) { $properties[] = $config['property']; } $values = $this->item->getPropertiesValues($properties); foreach ($this->columns as $column => $config) { try { $data = []; foreach ($values[$config['property']->getUri()] as $itemValue) { if (is_array($itemValue)) { array_walk($itemValue, function (&$value) { $resource = new \core_kernel_classes_Resource($value); $value = $resource->getLabel(); }); if (isset($config['delimiter'])) { $delimiter = $config['delimiter']; } else { $delimiter = self::DEFAULT_PROPERTY_DELIMITER; } $data[] = explode($delimiter, $itemValue); continue; } $data[] = ($itemValue instanceof \core_kernel_classes_Resource) ? $itemValue->getLabel() : str_replace('"', '""', (string) $itemValue); } } catch (\Exception $e) { \common_Logger::e('ERROR on column ' . $column . ' : ' . $e->getMessage()); $data = ['N/A']; } $this->data[$column] = implode(self::DEFAULT_PROPERTY_DELIMITER, $data); } $this->columns = []; return $this; } /** * Return formatted output * @return array */ public function getData() { return $this->data; } /** * Get human readable declaration class * @return string */ public function __toPhpCode() { return 'new ' . get_class($this) . '()'; } }