/** * @param TaxCodeEntity $taxCode * @return TaxCollection */ public function parseTaxCode(TaxCodeEntity $taxCode) { $taxes = new TaxCollection(); $description = $taxCode->getDescriptionEnglish(); $remainingDescription = null; if (substr(strrev($description), 0, 1) !== '%') { $remainingDescription = explode('%', strrev($description), 2); $remainingDescription = strrev($remainingDescription[0]); } preg_match_all("/([^%]*%)/", $description, $matches); if (is_array($matches[0])) { foreach ($matches[0] as $key => $part) { $taxArray = $this->parseTaxCodePart($part, isset($matches[0][$key + 1]) ? $matches[0][$key + 1] : $remainingDescription); if ($taxArray) { $tax = new TaxEntity(); $tax->setTaxCode($taxCode); $tax->setName($taxArray['name']); $tax->setRate($taxArray['rate']); $tax->setIsCompound($taxArray['isCompound']); $taxes->add($tax); } } } return $taxes; }
/** * @dataProvider getTaxCodesAndExpectedResults * @param array $taxCodes */ public function testTaxCodeParser(array $taxCodes) { $taxCodeParser = new TaxCodeParser(); foreach ($taxCodes as $taxCodeString => $expectedResults) { $taxCode = new TaxCodeEntity(); $taxCode->setDescriptionEnglish($taxCodeString); $results = $taxCodeParser->parseTaxCode($taxCode); $this->assertCount(count($expectedResults), $results); if (count($expectedResults)) { foreach ($expectedResults as $key => $expectedResult) { /** @var TaxEntity $result */ $result = $results[$key]; $this->assertEquals($expectedResult[0], $result->getName()); $this->assertEquals($expectedResult[1], $result->getRate()); if (isset($expectedResult[2])) { $this->assertEquals($expectedResult[2], $result->isCompound()); } else { $this->assertFalse($result->isCompound()); } } } } }