PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PayPal/Core/PPXmlMessage.php

https://gitlab.com/CORP-RESELLER/sdk-core-php
PHP | 195 lines | 151 code | 20 blank | 24 comment | 49 complexity | 61724e0adc47b6a1a2a2f0e1fbb75109 MD5 | raw file
  1. <?php
  2. namespace PayPal\Core;
  3. use PayPal\Exception\PPTransformerException;
  4. /**
  5. * @author
  6. */
  7. abstract class PPXmlMessage
  8. {
  9. /**
  10. * @return string
  11. */
  12. public function toSOAP()
  13. {
  14. return $this->toXMLString();
  15. }
  16. /**
  17. * @return string
  18. */
  19. public function toXMLString()
  20. {
  21. $attributes = array();
  22. $properties = get_object_vars($this);
  23. foreach (array_keys($properties) as $property) {
  24. if (($annots = PPUtils::propertyAnnotations($this, $property)) && isset($annots['attribute'])) {
  25. if (($propertyValue = $this->{$property}) === null || $propertyValue == null) {
  26. $attributes[] = null;
  27. continue;
  28. }
  29. $attributes[] = $property . '="' . PPUtils::escapeInvalidXmlCharsRegex($propertyValue) . '"';
  30. }
  31. }
  32. $attrs = implode(' ', $attributes) . (count($attributes) > 0 ? ">" : "");
  33. $xml = array();
  34. foreach ($properties as $property => $defaultValue) {
  35. if (($propertyValue = $this->{$property}) === null || $propertyValue == null) {
  36. continue;
  37. }
  38. if (($annots = PPUtils::propertyAnnotations($this, $property)) && isset($annots['attribute'])) {
  39. continue;
  40. }
  41. if (isset($annots['value'])) {
  42. $xml[] = PPUtils::escapeInvalidXmlCharsRegex($propertyValue);
  43. break;
  44. }
  45. if (is_array($defaultValue) || is_array($propertyValue)) {
  46. foreach ($propertyValue as $item) {
  47. if (!is_object($item)) {
  48. $xml[] = $this->buildProperty($property, $item);
  49. } else {
  50. $xml[] = $this->buildProperty($property, $item);
  51. }
  52. }
  53. } else {
  54. $xml[] = $this->buildProperty($property, $propertyValue);
  55. }
  56. }
  57. return $attrs . implode($xml);
  58. }
  59. /**
  60. * @param string $property
  61. * @param PPXmlMessage|string $value
  62. * @param string $namespace
  63. *
  64. * @return string
  65. */
  66. private function buildProperty($property, $value, $namespace = 'ebl')
  67. {
  68. $annotations = PPUtils::propertyAnnotations($this, $property);
  69. if (!empty($annotations['namespace'])) {
  70. $namespace = $annotations['namespace'];
  71. }
  72. if (!empty($annotations['name'])) {
  73. $property = $annotations['name'];
  74. }
  75. if ($namespace === true) {
  76. $el = '<' . $property;
  77. } else {
  78. $el = '<' . $namespace . ':' . $property;
  79. }
  80. if (!is_object($value)) {
  81. $el .= '>' . PPUtils::escapeInvalidXmlCharsRegex($value);
  82. } else {
  83. if (substr($value = $value->toXMLString(), 0, 1) === '<' || $value == '') {
  84. $el .= '>' . $value;
  85. } else {
  86. $el .= ' ' . $value;
  87. }
  88. }
  89. if ($namespace === true) {
  90. return $el . '</' . $property . '>';
  91. } else {
  92. return $el . '</' . $namespace . ':' . $property . '>';
  93. }
  94. }
  95. /**
  96. * @param array $map intermediate array representation of XML message to deserialize
  97. * @param string $isRoot true if this is a root class for SOAP deserialization
  98. */
  99. public function init(array $map = array(), $isRoot = true)
  100. {
  101. if ($isRoot) {
  102. if (stristr($map[0]['name'], ":fault")) {
  103. throw new PPTransformerException("soapfault");
  104. } else {
  105. $map = $map[0]['children'];
  106. }
  107. }
  108. if (empty($map)) {
  109. return;
  110. }
  111. if (($first = reset($map)) && !is_array($first) && !is_numeric(key($map))) {
  112. parent::init($map, false);
  113. return;
  114. }
  115. $propertiesMap = PPUtils::objectProperties($this);
  116. $arrayCtr = array();
  117. foreach ($map as $element) {
  118. if (empty($element) || empty($element['name'])) {
  119. continue;
  120. } elseif (!array_key_exists($property = strtolower($element['name']), $propertiesMap)) {
  121. if (!preg_match('~^(.+)[\[\(](\d+)[\]\)]$~', $property, $m)) {
  122. continue;
  123. }
  124. $element['name'] = $m[1];
  125. $element['num'] = $m[2];
  126. }
  127. $element['name'] = $propertiesMap[strtolower($element['name'])];
  128. if (PPUtils::isPropertyArray($this, $element['name'])) {
  129. $arrayCtr[$element['name']] = isset($arrayCtr[$element['name']]) ? ($arrayCtr[$element['name']] + 1) : 0;
  130. $element['num'] = $arrayCtr[$element['name']];
  131. }
  132. if (!empty($element["attributes"]) && is_array($element["attributes"])) {
  133. foreach ($element["attributes"] as $key => $val) {
  134. $element["children"][] = array(
  135. 'name' => $key,
  136. 'text' => $val,
  137. );
  138. }
  139. if (isset($element['text'])) {
  140. $element["children"][] = array(
  141. 'name' => 'value',
  142. 'text' => $element['text'],
  143. );
  144. }
  145. $this->fillRelation($element['name'], $element);
  146. } elseif (isset($element['text']) && !is_null($element['text'])) {
  147. if (isset($element['num'])) {
  148. $this->{$element['name']}[$element['num']] = $element['text'];
  149. } else {
  150. $this->{$element['name']} = $element['text'];
  151. }
  152. } elseif (!empty($element["children"]) && is_array($element["children"])) {
  153. $this->fillRelation($element['name'], $element);
  154. }
  155. }
  156. }
  157. /**
  158. * @param string $property
  159. * @param array $element
  160. */
  161. private function fillRelation($property, array $element)
  162. {
  163. if (!class_exists($type = PPUtils::propertyType($this, $property))) {
  164. trigger_error("Class $type not found.", E_USER_NOTICE);
  165. return; // just ignore
  166. }
  167. if (isset($element['num'])) { // array of objects
  168. $this->{$property}[$element['num']] = $item = new $type();
  169. $item->init($element['children'], false);
  170. } else {
  171. $this->{$property} = new $type();
  172. $this->{$property}->init($element["children"], false);
  173. }
  174. }
  175. }