2017-01-10 6 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

あなたはそれ以外の場合は$行が何であるかを知ることができません、引数として行を取り、あなたのFeedxクラスのコンストラクタを作成する必要があります。

また、$ mappingが保持するデータにアクセスするためのメソッドを作成する必要があります。

もっと良い方法は、取得しようとしているフィールドを返すメソッドを作成することでしょうか?例:

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

あなたは、このように呼び出すことができます。

$feedObject->getStore(); 

をしかし、それはすべてのデータを保持する1つの属性を持つのではなく、多分オブジェクトを、だから、多分それぞれがそれ自身の属性である可能性があり。そのコードの読み取りする必要が誰にとっても明確になり

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

(あなたの将来の自己を含むが、そして私たちのSOの人々):次に、あなたのコンストラクタで、あなたがこれを行うことができます。

あなたがそれをしているなら、あなたは本当に2つのクラスを必要としませんか?彼らは同じ属性と方法で同じことをしています。あなたが必要とするのは、違いを分ける方法です。たぶん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]; 
} 
関連する問題