PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/vendor/phpunit/phpunit/src/Util/XML.php

https://gitlab.com/Georgiy.Zhegusov/museum_documents
PHP | 255 lines | 145 code | 33 blank | 77 comment | 29 complexity | d934855c4e2b01eca68401f2b56502da MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of PHPUnit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * XML helpers.
  12. *
  13. * @since Class available since Release 3.2.0
  14. */
  15. class PHPUnit_Util_XML
  16. {
  17. /**
  18. * Load an $actual document into a DOMDocument. This is called
  19. * from the selector assertions.
  20. *
  21. * If $actual is already a DOMDocument, it is returned with
  22. * no changes. Otherwise, $actual is loaded into a new DOMDocument
  23. * as either HTML or XML, depending on the value of $isHtml. If $isHtml is
  24. * false and $xinclude is true, xinclude is performed on the loaded
  25. * DOMDocument.
  26. *
  27. * Note: prior to PHPUnit 3.3.0, this method loaded a file and
  28. * not a string as it currently does. To load a file into a
  29. * DOMDocument, use loadFile() instead.
  30. *
  31. * @param string|DOMDocument $actual
  32. * @param bool $isHtml
  33. * @param string $filename
  34. * @param bool $xinclude
  35. * @param bool $strict
  36. *
  37. * @return DOMDocument
  38. *
  39. * @since Method available since Release 3.3.0
  40. */
  41. public static function load($actual, $isHtml = false, $filename = '', $xinclude = false, $strict = false)
  42. {
  43. if ($actual instanceof DOMDocument) {
  44. return $actual;
  45. }
  46. if (!is_string($actual)) {
  47. throw new PHPUnit_Framework_Exception('Could not load XML from ' . gettype($actual));
  48. }
  49. if ($actual === '') {
  50. throw new PHPUnit_Framework_Exception('Could not load XML from empty string');
  51. }
  52. // Required for XInclude on Windows.
  53. if ($xinclude) {
  54. $cwd = getcwd();
  55. @chdir(dirname($filename));
  56. }
  57. $document = new DOMDocument;
  58. $document->preserveWhiteSpace = false;
  59. $internal = libxml_use_internal_errors(true);
  60. $message = '';
  61. $reporting = error_reporting(0);
  62. if ('' !== $filename) {
  63. // Necessary for xinclude
  64. $document->documentURI = $filename;
  65. }
  66. if ($isHtml) {
  67. $loaded = $document->loadHTML($actual);
  68. } else {
  69. $loaded = $document->loadXML($actual);
  70. }
  71. if (!$isHtml && $xinclude) {
  72. $document->xinclude();
  73. }
  74. foreach (libxml_get_errors() as $error) {
  75. $message .= "\n" . $error->message;
  76. }
  77. libxml_use_internal_errors($internal);
  78. error_reporting($reporting);
  79. if ($xinclude) {
  80. @chdir($cwd);
  81. }
  82. if ($loaded === false || ($strict && $message !== '')) {
  83. if ($filename !== '') {
  84. throw new PHPUnit_Framework_Exception(
  85. sprintf(
  86. 'Could not load "%s".%s',
  87. $filename,
  88. $message != '' ? "\n" . $message : ''
  89. )
  90. );
  91. } else {
  92. if ($message === '') {
  93. $message = 'Could not load XML for unknown reason';
  94. }
  95. throw new PHPUnit_Framework_Exception($message);
  96. }
  97. }
  98. return $document;
  99. }
  100. /**
  101. * Loads an XML (or HTML) file into a DOMDocument object.
  102. *
  103. * @param string $filename
  104. * @param bool $isHtml
  105. * @param bool $xinclude
  106. * @param bool $strict
  107. *
  108. * @return DOMDocument
  109. *
  110. * @since Method available since Release 3.3.0
  111. */
  112. public static function loadFile($filename, $isHtml = false, $xinclude = false, $strict = false)
  113. {
  114. $reporting = error_reporting(0);
  115. $contents = file_get_contents($filename);
  116. error_reporting($reporting);
  117. if ($contents === false) {
  118. throw new PHPUnit_Framework_Exception(
  119. sprintf(
  120. 'Could not read "%s".',
  121. $filename
  122. )
  123. );
  124. }
  125. return self::load($contents, $isHtml, $filename, $xinclude, $strict);
  126. }
  127. /**
  128. * @param DOMNode $node
  129. *
  130. * @since Method available since Release 3.3.0
  131. */
  132. public static function removeCharacterDataNodes(DOMNode $node)
  133. {
  134. if ($node->hasChildNodes()) {
  135. for ($i = $node->childNodes->length - 1; $i >= 0; $i--) {
  136. if (($child = $node->childNodes->item($i)) instanceof DOMCharacterData) {
  137. $node->removeChild($child);
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Escapes a string for the use in XML documents
  144. * Any Unicode character is allowed, excluding the surrogate blocks, FFFE,
  145. * and FFFF (not even as character reference).
  146. * See http://www.w3.org/TR/xml/#charsets
  147. *
  148. * @param string $string
  149. *
  150. * @return string
  151. *
  152. * @since Method available since Release 3.4.6
  153. */
  154. public static function prepareString($string)
  155. {
  156. return preg_replace(
  157. '/[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]/',
  158. '',
  159. htmlspecialchars(
  160. PHPUnit_Util_String::convertToUtf8($string),
  161. ENT_QUOTES,
  162. 'UTF-8'
  163. )
  164. );
  165. }
  166. /**
  167. * "Convert" a DOMElement object into a PHP variable.
  168. *
  169. * @param DOMElement $element
  170. *
  171. * @return mixed
  172. *
  173. * @since Method available since Release 3.4.0
  174. */
  175. public static function xmlToVariable(DOMElement $element)
  176. {
  177. $variable = null;
  178. switch ($element->tagName) {
  179. case 'array':
  180. $variable = [];
  181. foreach ($element->getElementsByTagName('element') as $element) {
  182. $item = $element->childNodes->item(0);
  183. if ($item instanceof DOMText) {
  184. $item = $element->childNodes->item(1);
  185. }
  186. $value = self::xmlToVariable($item);
  187. if ($element->hasAttribute('key')) {
  188. $variable[(string) $element->getAttribute('key')] = $value;
  189. } else {
  190. $variable[] = $value;
  191. }
  192. }
  193. break;
  194. case 'object':
  195. $className = $element->getAttribute('class');
  196. if ($element->hasChildNodes()) {
  197. $arguments = $element->childNodes->item(1)->childNodes;
  198. $constructorArgs = [];
  199. foreach ($arguments as $argument) {
  200. if ($argument instanceof DOMElement) {
  201. $constructorArgs[] = self::xmlToVariable($argument);
  202. }
  203. }
  204. $class = new ReflectionClass($className);
  205. $variable = $class->newInstanceArgs($constructorArgs);
  206. } else {
  207. $variable = new $className;
  208. }
  209. break;
  210. case 'boolean':
  211. $variable = $element->textContent == 'true' ? true : false;
  212. break;
  213. case 'integer':
  214. case 'double':
  215. case 'string':
  216. $variable = $element->textContent;
  217. settype($variable, $element->tagName);
  218. break;
  219. }
  220. return $variable;
  221. }
  222. }