/** * @param $data array * @param $properties string[] */ public function __construct($data = null, $properties = null) { if (isset($properties)) { $this->properties = $properties; } if (isset($data)) { $this->data = $data; $constants = Import_Array::getConstantsFromArray($data); $row = current($data); if (!isset($this->properties)) { foreach ($row as $column_number => $property_path) { if (!isset($constants[$property_path])) { $this->properties[$column_number] = $property_path; } } next($data); } // next row is the first row (in 1..n keys instead of 0..n of the $data array) $this->first_row = key($data) + 1; $this->last_row = min($this->first_row + 10, count($this->data)); $this->last_column = count($this->properties); } }
/** * Builds import settings using a data array * * First line must contain the class name (can be a short name, namespace will automatically be found) * Second line must contain the fields paths, relative to the class * Other liens contain data, and are not used * * @param $array array two dimensional array (keys are row, col) * @param $class_name string default class name (if not found into array) * @return Import_Settings */ public static function buildArray(&$array, $class_name = null) { $class_name = Import_Array::getClassNameFromArray($array) ?: $class_name; $settings = new Import_Settings($class_name); /** @var $classes Import_Class[] */ $classes = []; $properties_path = Import_Array::getPropertiesFromArray($array, $class_name); $auto_identify = self::autoIdentify($class_name, $properties_path); foreach ($properties_path as $property_path) { $sub_class = $class_name; $last_identify = false; $class_path = ''; $property_path_for_class = []; foreach (explode(DOT, $property_path) as $pos => $property_name) { $identify = substr($property_name, -1) !== '*'; if (!$identify) { $property_name = substr($property_name, 0, -1); } $class_key = join(DOT, $property_path_for_class); if (!isset($classes[$class_key])) { $classes[$class_key] = new Import_Class($sub_class, $property_path_for_class, $last_identify ? 'tell_it_and_stop_import' : 'create_new_value'); } $class = $classes[$class_key]; $import_property = new Import_Property($sub_class, $property_name); try { $property = new Reflection_Property($sub_class, $property_name); if ($identify && !$auto_identify || isset($auto_identify[$property_path]) && isset($auto_identify[$property_path][$pos])) { $class->identify_properties[$property_name] = $import_property; } else { $class->write_properties[$property_name] = $import_property; } $sub_class = $property->getType()->getElementTypeAsString(); $class_path .= $sub_class . DOT; } catch (ReflectionException $exception) { $class->ignore_properties[$property_name] = $import_property; $class->unknown_properties[$property_name] = $import_property; } $last_identify = $identify; $property_path_for_class[] = $property_name; } } $settings->classes = $classes; $settings->setConstants(Import_Array::getConstantsFromArray($array)); return $settings; }