/** * Turns a passed Article class into the required markup for interacting * with Twinfield. * * This method doesn't return anything, instead just adds the Article to * this DOMDOcument instance for submission usage. * * @access public * @param \Pronamic\Twinfield\Article\Article $article * @return void | [Adds to this instance] */ public function addArticle(Article $article) { // Article->header elements and their methods $articleTags = array('office' => 'getOffice', 'code' => 'getCode', 'type' => 'getType', 'name' => 'getName', 'shortname' => 'getShortName', 'unitnamesingular' => 'getUnitNameSingular', 'unitnameplural' => 'getUnitNamePlural', 'vatcode' => 'getVatCode', 'allowchangevatcode' => 'getAllowChangeVatCode', 'allowdiscountorpremium' => 'getAllowDiscountorPremium', 'allowchangeunitsprice' => 'getAllowChangeUnitsPrice', 'allowdecimalquantity' => 'getAllowDecimalQuantity', 'performancetype' => 'getPerformanceType', 'percentage' => 'getPercentage'); // Make header element $headerElement = $this->createElement('header'); $this->articleElement->appendChild($headerElement); $status = $article->getStatus(); if (!empty($status)) { $headerElement->setAttribute('status', $status); } // Go through each Article element and use the assigned method foreach ($articleTags as $tag => $method) { // Make text node for method value $node = $this->createTextNode($article->{$method}()); // Make the actual element and assign the node $element = $this->createElement($tag); $element->appendChild($node); // Add the full element $headerElement->appendChild($element); } $lines = $article->getLines(); if (!empty($lines)) { // Element tags and their methods for lines $lineTags = ['unitspriceexcl' => 'getUnitsPriceExcl', 'unitspriceinc' => 'getUnitsPriceInc', 'units' => 'getUnits', 'name' => 'getName', 'shortname' => 'getShortName', 'subcode' => 'getSubCode', 'freetext1' => 'getFreeText1']; // Make addresses element $linesElement = $this->createElement('lines'); $this->articleElement->appendChild($linesElement); // Go through each line assigned to the article foreach ($lines as $line) { // Makes new articleLine element $lineElement = $this->createElement('line'); $linesElement->appendChild($lineElement); $status = $line->getStatus(); $id = $line->getID(); $inUse = $line->getInUse(); if (!empty($status)) { $lineElement->setAttribute('status', $status); } if (!empty($id)) { $lineElement->setAttribute('id', $id); } if (!empty($inUse)) { $lineElement->setAttribute('inuse', $inUse); } // Go through each line element and use the assigned method foreach ($lineTags as $tag => $method) { // Make the text node for the method value $node = $this->createTextNode($line->{$method}()); // Make the actual element and assign the text node $element = $this->createElement($tag); $element->appendChild($node); // Add the completed element $lineElement->appendChild($element); } } } }
/** * Maps a Response object to a clean Article entity. * * @access public * @param \Pronamic\Twinfield\Response\Response $response * @return \Pronamic\Twinfield\Article\Article */ public static function map(Response $response) { // Generate new Article object $article = new Article(); // Gets the raw DOMDocument response. $responseDOM = $response->getResponseDocument(); // Set the status attribute $dimensionElement = $responseDOM->getElementsByTagName('header')->item(0); $article->setStatus($dimensionElement->getAttribute('status')); // Article elements and their methods $articleTags = ['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 ($articleTags 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)) { $article->{$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 article foreach ($linesDOM->getElementsByTagName('line') as $lineDOM) { // Make a new tempory ArticleLine class $articleLine = new ArticleLine(); // Set the attributes ( id,status,inuse) $articleLine->setID($lineDOM->getAttribute('id'))->setStatus($lineDOM->getAttribute('status'))->setInUse($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)) { $articleLine->{$method}($_tag->textContent); } } // Add the bank to the customer $article->addLine($articleLine); // Clean that memory! unset($ArticleLine); } } return $article; }