在数组中存储变量

时间:2017-01-10 19:24:22

标签: php csv oop

我正在尝试创建一个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 个答案:

答案 0 :(得分:1)

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

您还需要创建用于访问$ mapping所持数据的方法。

也许更好的方法是创建返回你想要获得的任何字段的方法? E.g:

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];
}
相关问题