/** * 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; }