/** * Maps a Response object to a clean Match entity. * * @access public * @param \Pronamic\Twinfield\Response\Response $response * @return \Pronamic\Twinfield\Match\Match */ public static function map(Response $response) { // Generate new Match object $Match = new Match(); // Gets the raw DOMDocument response. $responseDOM = $response->getResponseDocument(); // Set the status attribute $dimensionElement = $responseDOM->getElementsByTagName('header')->item(0); $Match->setDate($dimensionElement->getAttribute('status')); // Match elements and their methods $MatchTags = ['code' => 'setCode', 'office' => 'setOffice', 'type' => 'setType', 'name' => 'setName', 'shortname' => 'setShortName', 'unitnamesingular' => 'setUnitNameSingular', 'unitnameplural' => 'setUnitNamePlural', 'vatcode' => 'setVatCode', 'allowchangevatcode' => 'setAllowChangeVatCode', 'performancetype' => 'setPerformanceType', 'allowchangeperformancetype' => 'setAllowChangePerformanceType', 'percentage' => 'setPercentage', 'allowdiscountorpremium' => 'setAllowDiscountorPremium', 'allowchangeunitsprice' => 'setAllowChangeUnitsPrice', 'allowdecimalquantity' => 'setAllowDecimalQuantity']; // Loop through all the tags foreach ($MatchTags as $tag => $method) { // Get the dom element $_tag = $responseDOM->getElementsByTagName($tag)->item(0); // If it has a value, set it to the associated method if (isset($_tag) && isset($_tag->textContent)) { $Match->{$method}($_tag->textContent); } } $linesDOMTag = $responseDOM->getElementsByTagName('lines'); if (isset($linesDOMTag) && $linesDOMTag->length > 0) { // Element tags and their methods for lines $lineTags = ['unitspriceexcl' => 'setUnitsPriceExcl', 'unitspriceinc' => 'setUnitsPriceInc', 'units' => 'setUnits', 'name' => 'setName', 'shortname' => 'setShortName', 'subcode' => 'setSubCode', 'freetext1' => 'setFreeText1']; $linesDOM = $linesDOMTag->item(0); // Loop through each returned line for the Match foreach ($linesDOM->getElementsByTagName('line') as $lineDOM) { // Make a new tempory MatchLine class $MatchLine = new MatchLine(); // Set the attributes ( id,status,inuse) $MatchLine->setID($lineDOM->getAttribute('id'))->setTransCode($lineDOM->getAttribute('status'))->setTransNumber($lineDOM->getAttribute('inuse')); // Loop through the element tags. Determine if it exists and set it if it does foreach ($lineTags as $tag => $method) { // Get the dom element $_tag = $lineDOM->getElementsByTagName($tag)->item(0); // Check if the tag is set, and its content is set, to prevent DOMNode errors if (isset($_tag) && isset($_tag->textContent)) { $MatchLine->{$method}($_tag->textContent); } } // Add the bank to the customer $Match->addLine($MatchLine); // Clean that memory! unset($MatchLine); } } return $Match; }
/** * @test */ public function matchRecordGeneratesProperXml() { $match = new Match(); $match->setOffice('001'); $match->setCode('170'); // fixed $match->setDate('20160908'); $i = 0; $line = new MatchLine(); $line->setTransCode('INCASSO-OW'); $line->setTransNumber('1000'); // Twinfield generated ID for memorial transaction $line->setTransLine(2); $match->addLine($line); $line = new MatchLine(); $line->setTransCode('VRK'); $line->setTransNumber('20160001'); // Twinfield generated ID for invoice transaction $line->setTransLine(1); $line->setMatchValue(100); $match->addLine($line); $matchDocument = new MatchDocument(); $matchDocument->addMatch($match); $generatedXml = $matchDocument; $expectedXml = new DOMDocument(); $expectedXml->loadXml('<match> <set> <office>001</office> <matchcode>170</matchcode> <matchdate>20160908</matchdate> <lines> <line> <transcode>INCASSO-OW</transcode> <transnumber>1000</transnumber> <transline>2</transline> </line> <line> <transcode>VRK</transcode> <transnumber>20160001</transnumber> <transline>1</transline> <matchvalue>100.00</matchvalue> </line> </lines> </set> </match>'); $this->assertEqualXMLStructure($expectedXml->documentElement, $generatedXml->documentElement); return $match; }