2017-01-10 1 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 클래스의 생성자를 작성해야합니다.

또한 $ 매핑이 보유한 데이터에 액세스하기위한 메소드를 만들어야합니다.

더 나은 방법은 얻고 자하는 필드를 반환하는 메소드를 만드는 것입니까? 예 :

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

는 다음과 같이 호출 할 수

$feedObject->getStore(); 

을하지만 모든 데이터를 보유 하나의 속성을하는 대신 어쩌면 객체를, 이후, 아마도 각각이 자신의 속성이 될 수 . 그 코드를 읽을 필요로하는 사람을위한 명확한 것

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

(미래의 자신을 포함하여, 우리 SO 사람) : 그런 다음 생성자에서이 작업을 수행 할 수있다.

하지만 그렇게한다면 두 클래스가 필요하지 않습니까? 그들은 같은 속성과 방법으로 같은 일을하고 있습니다. 차이점을 구분하는 방법은 필요한 것뿐입니다. 아마 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]; 
}