Element.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Shipping\Model\Simplexml;
  7. /**
  8. * Extends SimpleXML to add valuable functionality to \SimpleXMLElement class
  9. *
  10. */
  11. class Element extends \Magento\Framework\Simplexml\Element
  12. {
  13. /**
  14. * Adds an attribute to the SimpleXML element
  15. *
  16. * @param string $name The name of the attribute to add.
  17. * @param string $value If specified, the value of the attribute.
  18. * @param string $namespace If specified, the namespace to which the attribute belongs.
  19. * @return void
  20. */
  21. public function addAttribute($name, $value = null, $namespace = null)
  22. {
  23. if ($value !== null) {
  24. $value = $this->xmlentities($value);
  25. }
  26. parent::addAttribute($name, $value, $namespace);
  27. }
  28. /**
  29. * Adds a child element to the XML node
  30. *
  31. * @param string $name The name of the child element to add.
  32. * @param string $value If specified, the value of the child element.
  33. * @param string $namespace If specified, the namespace to which the child element belongs.
  34. * @return \Magento\Shipping\Model\Simplexml\Element
  35. */
  36. public function addChild($name, $value = null, $namespace = null)
  37. {
  38. if ($value !== null) {
  39. $value = $this->xmlentities($value);
  40. }
  41. return parent::addChild($name, $value, $namespace);
  42. }
  43. /**
  44. * Converts meaningful xml characters to xml entities
  45. *
  46. * @param string|null $value
  47. * @return string
  48. */
  49. public function xmlentities($value = null)
  50. {
  51. $value = str_replace('&amp;', '&', $value);
  52. $value = str_replace('&', '&amp;', $value);
  53. return $value;
  54. }
  55. }