简体   繁体   English

在数组中存储变量

[英]Storing Variables in Array

I'm trying to create a CSV converter which will take a CSV, parse the data, and output it into a standard format. 我正在尝试创建一个CSV转换器,该转换器将采用CSV,解析数据并将其输出为标准格式。

This requires created somewhat of a data map for each type of CSV that will be converted. 这需要为每种将转换的CSV类型创建一些数据映射。

What I want to do is store each of the row mappings into a respective class for that CSV and then use it within the converters class when using a foreach loop. 我要做的是将每个行映射存储到该CSV的相应类中,然后在使用foreach循环时在converters类中使用它。

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]
    ]
}

You need to create a constructor for your Feedx classes that takes the row as an argument, otherwise they won't know what $row is. 您需要为Feedx类创建一个构造函数,以行作为参数,否则它们将不知道$ row是什么。

You also need to create methods for accessing the data held by $mapping. 您还需要创建访问$ mapping所保存数据的方法。

Maybe a better way would be to create methods that return whatever field you're trying to get? 也许更好的方法是创建返回您要获取的任何字段的方法? Eg: 例如:

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

Which you could then call like this: 然后可以这样调用:

$feedObject->getStore();

But since it's an object, maybe instead of having a single attribute that holds all the data, maybe each could be it's own attribute. 但是由于它是一个对象,所以也许不是拥有一个包含所有数据的单一属性,而是每个属性可能都是它自己的属性。 Then in your constructor you could do this: 然后可以在构造函数中执行以下操作:

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

Which would be clearer for anyone needing to read that code (including your future self, and us SO folks). 对于需要阅读该代码的任何人(包括您未来的自我以及我们这样的亲朋好友),这将更加清楚。

But if you're doing that, then you don't really need two classes, do you? 但是,如果您这样做,那么您真的不需要两个课程,对吗? They're both doing the same thing, with the same attributes and methods. 他们都用相同的属性和方法做相同的事情。 All you need is a way to separate the differences. 您需要的只是一种区分差异的方法。 Maybe an init method? 也许是一个初始化方法?

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.

... or maybe each method just does that on the fly, without using class attributes: ...或者也许每个方法都只是在不使用类属性的情况下即时完成的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM