mardi 10 avril 2018

Class of multiple items

I have a class Company, and a class Tax. Depending on the country where the company is being created, I create multiple Tax Objects for this Company. For example, if the country is Norway, I need to create 4 tax objects by default.

Now to have this structure, I firstly created a class for each distinct Tax that I have. Those for example are two taxes that I have:

class TaxNorway11 extends Tax {

    public function __construct($companyID, $config = []) {
        $this->name = 'Tax 11.00%';
        $this->percentage = 11;
        $this->company = $companyID;
        parent::__construct($config); 
    }
}

class TaxNorway12 extends Tax {

    public function __construct($companyID, $config = []) {
        $this->name = 'Tax 12%';
        $this->percentage = 12;
        $this->company = $companyID;
        parent::__construct($config); 
    }
} 

Then I created a TaxFactory that checks the $country and creates the Tax Objects accordingly:

class TaxFactory {

    public function build() {

        switch ($country) {
            case self::NORWAY_ID:
                $tax11 = new TaxNorway11($this->companyID);
                $tax15 = new TaxNorway15($this->companyID);
                $tax12 = new TaxNorway12($this->companyID);
                $this->taxes = array($tax11, $tax12, $tax15);
                break;

        }

        return $this->taxes;
    }
}

I have multiple countries though, and thus I will end up creating many classes, one for each Tax Object. My Question is, is there a way by which I can create a class made of a list, for example NorwayTaxes that will have all the taxes for Norway. This is specially since every time I create a company there I will have to use the full list, so it seems that its a better way to organize the code?

Aucun commentaire:

Enregistrer un commentaire