<?php

class ImportHelper
{

    public static function getCsvRelevantModelAttributes($modelName)
    {
        $allAttributes = ImportHelper::getImportRelevantAttributes($modelName);
        foreach ($allAttributes as $key => $value) {
            $allAttributes[$key] = array('length' => 255);
        }
        $attributes = ImportHelper::getCsvAttributesWithRules($modelName, $allAttributes);
        return $attributes;
    }

    public static function getImportRelevantAttributes($modelName)
    {
        return ExportHelper::getExportRelevantModelAttributes($modelName);
    }

    private static function getCsvAttributesWithRules($modelName, $attributes)
    {
        $rules = $modelName::model()->rules();
        foreach ($rules as $rule) {
            $attributes = ImportHelper::processAttributeRules($attributes, $rule);
        }
        return $attributes;
    }

    private static function processAttributeRules($attributes, $rule)
    {
        $keys = explode(',', $rule[0]);
        if ($rule[1] === 'length' && isset($rule['max'])) {
            foreach ($keys as $key) {
                $key = trim($key);
                if (isset($attributes[$key])) {
                    $attributes[$key] = array('length' => $rule['max']);
                }
            }
        }
        return $attributes;
    }
}
