ORDER

The ORDER class provides the functioanallity to create a XML, which is validatable with the MDXI.xsd.

Defined (1)

The class is defined in the following location(s).

/MPay24/orderXML.php  
  1. class ORDER { 
  2. /** 
  3. * The DOMDocument, which the MDXI XML will be build on 
  4. * @var DOMDocument 
  5. */ 
  6. private $doc; 
  7. /** 
  8. * A DOMNode from the MDXI XML, or the whole MDXI XML, represented as DOMDocument 
  9. * @var DOMDocument|DOMNode 
  10. */ 
  11. private $node; 
  12.  
  13. /** 
  14. * Create a DOMDocument or a ORDER-Object with root $doc 
  15. * @param DOMNode $doc The root DOMNode of an XML tree 
  16. * @param DOMNode $node The child DOMNode 
  17. */ 
  18. public function ORDER($doc=null, $node=null) { 
  19. if ($doc) 
  20. $this->doc = $doc; 
  21. else { 
  22. $this->doc = new DOMDocument("1.0", "UTF-8"); 
  23. $this->doc->formatOutput = true; 
  24.  
  25. if ($node) 
  26. $this->node = $node; 
  27. else 
  28. $this->node = $this->doc; 
  29.  
  30. /** 
  31. * Generic call-Method instead of numerous setter methods 
  32. * @param string $method The name of the method, which is called for the Item-Object 
  33. * @param array $args The arguments with them the method is called - minOccurance = 0, maxOccurance = 2: 
  34. * The first argument must be a positive integer (will be used as a index) 
  35. * The second argument is optional and would be used as value for the DOMNode 
  36. */ 
  37. public function __call($method, $args) { 
  38. if(substr($method, 0, 3) == "set" && $args[0] != '') { 
  39. $attributeName = substr($method, 3); 
  40.  
  41. $value = $args[0]; 
  42.  
  43. if(preg_match('/[0-9]+, [0-9]+/', $value, $match)) 
  44. $value = str_replace(', ', '.', $match[0]); 
  45.  
  46. if(preg_match('/[0-9]+.[0-9]+/', $value, $match) && $value == $match[0] && $attributeName != 'shippingCosts' && (is_int(strpos($attributeName, 'price')) || is_int(strpos($attributeName, 'Price')) || is_int(strpos($attributeName, 'Tax')) || is_int(strpos($attributeName, 'cost')) || is_int(strpos($attributeName, 'Cost')))) 
  47. $value = number_format(floatval($match[0]), 2, '.', ''); 
  48.  
  49. $this->node->setAttribute($attributeName, $value); 
  50. } elseif($args[0] != '') { 
  51. if(sizeof($args)>2) 
  52. die("It is not allowed to set more than 2 arguments for the node '$method'!"); 
  53. if(!is_int($args[0]) || $args[0] < 1) 
  54. die("The first argument for the node '$method' must be whole number, bigger than 0!"); 
  55.  
  56. $name = $method . '[' . $args[0] . ']'; 
  57.  
  58. $xpath = new DOMXPath($this->doc); 
  59. $qry = $xpath->query($name, $this->node); 
  60.  
  61. if ($qry->length > 0) 
  62. return new ORDER($this->doc, $qry->item(0)); 
  63. else { 
  64. if(array_key_exists(1, $args)) { 
  65. $value = $args[1]; 
  66.  
  67. if(preg_match('/[0-9]+, [0-9]+/', $value, $match)) 
  68. $value = str_replace(', ', '.', $match[0]); 
  69.  
  70. if(preg_match('/[0-9]+.[0-9]+/', $value, $match) && $value == $match[0] && $name != 'shippingCosts' && (is_int(strpos($name, 'price')) || is_int(strpos($name, 'Price')) || is_int(strpos($name, 'Tax')) || is_int(strpos($name, 'cost')) || is_int(strpos($name, 'Cost')))) 
  71. $value = number_format(floatval($match[0]), 2, '.', ''); 
  72.  
  73. $node = $this->doc->createElement($method, $value); 
  74. } else 
  75. $node = $this->doc->createElement($method); 
  76.  
  77. $node = $this->node->appendChild($node); 
  78.  
  79. return new ORDER($this->doc, $node); 
  80.  
  81. /** 
  82. * Get the value of a ORDER-Variable 
  83. * @param string $name The name of the method, which is called for the Item-Object 
  84. * @return ORDER 
  85. */ 
  86. public function __get($name) { 
  87. $xpath = new DOMXPath($this->doc); 
  88. $qry = $xpath->query($name, $this->node); 
  89.  
  90. if ($qry->length > 0) 
  91. return new ORDER($this->doc, $qry->item(0)); 
  92. else { 
  93. $node = $this->doc->createElement($name); 
  94. $node = $this->node->appendChild($node); 
  95. return new ORDER($this->doc, $node); 
  96.  
  97. /** 
  98. * Set the value of a ORDER-Variable 
  99. * @param string $name The name of the Node you want to set 
  100. * @param mixed $value The value of the Node you want to set 
  101. */ 
  102. public function __set($name, $value) { 
  103. $xpath = new DOMXPath($this->doc); 
  104. $qry = $xpath->query($name, $this->node); 
  105.  
  106. $value = str_replace('&', '&', $value); 
  107.  
  108. if(preg_match('/[0-9]+, [0-9]+/', $value, $match)) 
  109. $value = str_replace(', ', '.', $match[0]); 
  110.  
  111. if(preg_match('/[0-9]+.[0-9]+/', $value, $match) && $value == $match[0] && $name != 'shippingCosts' && (is_int(strpos($name, 'price')) || is_int(strpos($name, 'Price')) || is_int(strpos($name, 'Tax')) || is_int(strpos($name, 'cost')) || is_int(strpos($name, 'Cost')))) 
  112. $value = number_format(floatval($match[0]), 2, '.', ''); 
  113.  
  114. if(strpos($value, "<") || strpos($value, ">")) 
  115. $value = "<![CDATA[" . $this->xmlencode($value) . "]]>"; 
  116.  
  117. if ($qry->length > 0) 
  118. $qry->item(0)->nodeValue = $value; 
  119. else { 
  120. $node = $this->doc->createElement($name, $value); 
  121. $this->node = $this->node->appendChild($node); 
  122.  
  123. /** 
  124. * Create a XML-Object from the ORDER-Object and return it 
  125. * @return XML 
  126. */ 
  127. public function toXML() { 
  128. return $this->doc->saveXML(); 
  129.  
  130. /** 
  131. * Validate the ORDER with the schema, defined in the constant MDXI_SCHEMA and return TRUE if the validation was successful or FALSE 
  132. * @return bool 
  133. */ 
  134. public function validate() { 
  135. $mdxi = "/MDXI.xsd"; 
  136.  
  137. if($this->olderThanOneWeek(__DIR__ . $mdxi)) { 
  138. set_time_limit(0); 
  139. ini_set('display_errors', true); 
  140.  
  141. $fp = fopen (__DIR__ . '/MDXInew.xsd', 'w'); 
  142. $ch = curl_init(MDXI_SCHEMA); 
  143. curl_setopt($ch, CURLOPT_FILE, $fp); 
  144. curl_setopt($ch, CURLOPT_HEADER, 0); 
  145. curl_setopt($ch, CURLOPT_CAINFO, __DIR__.'/cacert.pem'); 
  146. $result = curl_exec($ch); 
  147.  
  148. if(curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200 && file_exists(__DIR__ . '/MDXInew.xsd')) { 
  149. unlink(__DIR__ . $mdxi); 
  150. fclose($fp); 
  151. rename(__DIR__ . "/MDXInew.xsd", __DIR__ . $mdxi); 
  152. } else 
  153. fclose($fp); 
  154.  
  155. curl_close($ch); 
  156.  
  157. return $this->doc->schemaValidate(__DIR__ . $mdxi); 
  158.  
  159. /** 
  160. * Encode the XML-characters in a string 
  161. * @param string $txt A string to be encoded 
  162. * @return string 
  163. */ 
  164. private function xmlencode($txt) { 
  165. $txt = str_replace('<', '<', $txt); 
  166. $txt = str_replace('>', '>', $txt); 
  167. $txt = str_replace('&apos;', "'", $txt); 
  168. $txt = str_replace('&quot;', '"', $txt); 
  169. return $txt; 
  170.  
  171. /** 
  172. * Check whether the MDXI.xsd is older than one week 
  173. * @param string $filename The path to the file (MDXI.xsd) 
  174. * @return bool 
  175. */ 
  176. private function olderThanOneWeek($filename) { 
  177. $year = date ("Y", filemtime($filename)); 
  178. $month = date ("m", filemtime($filename)); 
  179. $day = date ("d", filemtime($filename)); 
  180.  
  181. $tyear = date ("Y"); 
  182. $tmonth = date ("m"); 
  183. $tday = date ("d"); 
  184.  
  185. if($tyear > $year) 
  186. return true; 
  187. else { 
  188. if($tmonth > $month) { 
  189. if($tday > 7) 
  190. return true; 
  191. else { 
  192. if($tday-7+30 < $day) 
  193. return false; 
  194. else 
  195. return true; 
  196. } else { 
  197. if($tday-7 > $day) 
  198. return true; 
  199. else 
  200. return false;