2017-01-10 69 views
0

我试图创建一个CSV转换器,它将采用CSV,解析数据并将其输出到标准格式。在数组中存储变量

这需要为将要转换的每种类型的CSV创建一些数据映射。

我想要做的是将每个行映射存储到该CSV的相应类中,然后在使用foreach循环时在转换器类中使用它。

class CsvImporter { 
    public function exportCSV() 
    { 
     $headers = ['store','websites','attribute_set','type','category_ids','sku','has_options','name','image','small_image','thumbnail','cost','upc','price','special_price','weight','msrp','status','visibility','tax_class_id','description','short_description','qty','is_in_stock','product_name','store_id','product_type_id','manufacturer','pla_category','pla_stock','condition','mpn']; 

     $data = $this->parseCSV(); 

     if ($this->feed = 'feed1'){ 
      //return feed1 mapping 
     } else{ 
      //return feed2 mapping 
     } 

     foreach($data as $row) { 
      $importData['store']  = //return values from class mapping here 
      $importData['website']  = //return values from class mapping here 
      $importData['attribute_set'] = //return values from class mapping here 
      $importData['type']   = //return values from class mapping here 
      $importData['category_ids'] = //return values from class mapping here 
      $importData['sku']   = //return values from class mapping here 
      $importData['has_options'] = //return values from class mapping here 
      $importData['name']   = //return values from class mapping here 
      $importData['image']  = //return values from class mapping here 
      $importData['small_image'] = //return values from class mapping here 
      $importData['thumbnail'] = //return values from class mapping here 
      $importData['cost']   = //return values from class mapping here 
      $importData['upc']   = //return values from class mapping here 
      $importData['price']  = //return values from class mapping here 
      $importData['special_price'] = //return values from class mapping here 
      $importData['weight']  = //return values from class mapping here 
      $importData['msrp']   = //return values from class mapping here 
      $importData['status']  = //return values from class mapping here 
      $importData['visibility'] = //return values from class mapping here 
      $importData['tax_class_id'] = //return values from class mapping here 
      $importData['description'] = //return values from class mapping here 
      $importData['short_description'] = //return values from class mapping here 
      $importData['qty']   = //return values from class mapping here 
      $importData['is_in_stock'] = //return values from class mapping here 
      $importData['product_name'] = //return values from class mapping here 
      $importData['store_id']  = //return values from class mapping here 
      $importData['product_type_id'] = //return values from class mapping here 
      $importData['manufacturer'] = //return values from class mapping here 
      $importData['pla_category'] = //return values from class mapping here 
      $importData['pla_stock'] = //return values from class mapping here 
      $importData['condition'] = //return values from class mapping here 
      $importData['mpn']   = //return values from class mapping here 
     } 

     fclose($handle); 
    } 
} 

class Feed1{ 
    const feed = 'feed1'; 
    const db = ''; //initialize db connection 

    private static $mapping =[ 
     $this->store, 
     $this->website, 
     'Default', 
     'simple', 
     '', 
     $row['4'], 
     '0', 
     $row[8] . " " . $row[15], 
     '', 
     ''; 
     '', 
     $row[9], 
     $row[6], 
     ($row[9]/0.85) + $row[14], 
     '', 
     $row[14], 
     '', 
     'Enabled', 
     '"Catalog, Search"', 
     'Taxable Goods', 
     $row[16], 
     '', 
     $row[1], 
     ($row[1] > 0) ? 1 : 0, 
     $row[15], 
     '', 
     'simple', 
     $row[8], 
     '285', 
     ($row[1] > 0) ? 'in stock' : 'out of stock', 
     'new', 
     $row[4], 
    ] 
} 
class Feed2{ 
    const feed = 'feed2'; 
    const db = ''; //initialize db connection 

    private static $mapping =[ 
     $this->store, 
     $this->website, 
     'Default', 
     'simple', 
     '', 
     $row['0'], 
     '0', 
     $row[5] . " " . $row[1], 
     '', 
     ''; 
     '', 
     $row[6], 
     $row[7], 
     $row[8], 
     '', 
     $row[9], 
     '', 
     'Enabled', 
     '"Catalog, Search"', 
     'Taxable Goods', 
     $row[12], 
     '', 
     $row[11], 
     ($row[10] > 0) ? 1 : 0, 
     $row[3], 
     '', 
     'simple', 
     $row[4], 
     '285', 
     ($row[17] > 0) ? 'in stock' : 'out of stock', 
     'new', 
     $row[15] 
    ] 
} 

回答

1

您需要为您的Feeds类创建一个构造函数,将该行作为参数,否则他们将不知道$ row是什么。

您还需要创建方法来访问由$ mapping持有的数据。

也许更好的方法是创建方法返回你试图获得的任何字段?例如:

public function getStore() { 
    return $this->mapping['store']; 
} 

然后你可以这样调用:

$feedObject->getStore(); 

但因为它是一个对象,也许反而具有保持所有数据的单一属性的,也许每个可能是它自己的属性。然后,在你的构造函数,你可以这样做:

function __construct($row) { 
    $this->store = 'Default' 
    ... 
    $this->cost = $row[9]; 
    ... etc. 

这将是任何人都需要读取的代码更清晰(包括你的未来的自己,和我们这么人)。

但是,如果你这样做,那么你真的不需要两节课,是吗?他们都在做同样的事情,具有相同的属性和方法。所有你需要的是一种分离差异的方法。也许一个init方法?

public function __construct($type, $row) { 
    $this->type = $type; 
    $this->row = $row; 
} 

public function init() { 
    $this->store = 'Default'; 
    ... etc. 
    $this->cost = ($this->type == 'feed1') ? $this->row[9] : $this->row[7]; 
    $this->price = ($this->type == 'feed1') ? $this->row[11] : $this->row[15]; 
    .... etc. 

...也许每个方法只是做的是在飞行中,不使用类属性:

function getPrice() { 
    return ($this->type == 'feed1') ? $this->row[9] : $this->row[10]; 
}