* @package tao */ class CsvBasicImporter extends CsvAbstractImporter { const OPTION_POSTFIX = '_O'; public function import($class, $options) { return parent::importFile($class, $options); } /** * @param \core_kernel_classes_Class $class * @param string $file * @param array $options * @return array */ public function getCsvMapping($class, $file, $options) { $properties = $this->getClassProperties($class); $csv_data = new \tao_helpers_data_CsvFile($options); $csv_data->load($file); $firstRowAsColumnNames = (isset($options[\tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES])) ? $options[\tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] : false; $headers = $this->getColumnMapping($csv_data, $firstRowAsColumnNames); $modifiedHeader = $headers; array_walk($modifiedHeader, function (&$value) { $value = str_replace(' ', '', strtolower($value)); }); $properties[] = new \core_kernel_classes_Property(OntologyRdfs::RDFS_LABEL); $map = []; /** @var \core_kernel_classes_Property $property */ foreach ($properties as $property) { if (!in_array($property->getUri(), $this->getExludedProperties())) { $propertiesMap[$property->getUri()] = $property->getLabel(); //map properties in many ways //look for label (lower case without spaces) //look for uri (without namespace) if ( ($index = array_search(str_replace(' ', '', strtolower($property->getLabel())), $modifiedHeader)) !== false || ($index = array_search(substr(strtolower($property->getUri()), strpos($property->getUri(), '#') + 1), $modifiedHeader)) !== false ) { $map[$property->getUri()] = $index; //look for label or uri with eventually one error } else { $maximumError = 1; $closest = null; foreach ($modifiedHeader as $index => $header) { $levLabel = levenshtein(strtolower($property->getLabel()), $header); $levUri = levenshtein(substr(strtolower($property->getUri()), strpos($property->getUri(), '#') + 1), $header); if ($levLabel <= $maximumError || $levUri <= $maximumError) { $closest = $index; break; } } if (!is_null($closest)) { $map[$property->getUri()] = $closest; } } } } $csvMap = [ 'classProperties' => $propertiesMap, 'headerList' => $headers, 'mapping' => $map ]; return $csvMap; } public function getDataSample($file, $options = [], $size = 5, $associative = true) { $csv_data = new \tao_helpers_data_CsvFile($options); $csv_data->load($file); $count = min($size, $csv_data->count()); $data = []; for ($i = 0; $i < $count; $i++) { $data[] = $csv_data->getRow($i, $associative); } return $data; } }