Xml.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ vLIXPoaVMg2aIqmzTgPyiLeJsbhF0GO+uddsjGtVr9Q=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. /**
  13. * config xml generator class
  14. */
  15. namespace Cube\Config;
  16. class Xml extends AbstractConfig
  17. {
  18. /**
  19. *
  20. * convert input into \SimpleXMLElement, then process the xml into an array
  21. *
  22. * @param mixed $data the input variable, it can be a path to an xml file, a string in xml format or an object of type \SimpleXMLElement
  23. *
  24. * @return $this
  25. */
  26. public function addData($data)
  27. {
  28. $xml = $this->_processInput($data);
  29. $data = json_decode(json_encode((array)$xml), true);
  30. $this->_data = array_replace_recursive(
  31. array_merge_recursive($this->_data, $data), $data);
  32. return $this;
  33. }
  34. /**
  35. *
  36. * process input
  37. *
  38. * @param $input
  39. *
  40. * @return null|\SimpleXMLElement
  41. */
  42. protected function _processInput($input)
  43. {
  44. $xml = null;
  45. if ($input instanceof \SimpleXMLElement) {
  46. $xml = $input;
  47. } else if (file_exists($input)) {
  48. $xml = simplexml_load_file($input);
  49. } else {
  50. $xml = simplexml_load_string($input);
  51. }
  52. return $xml;
  53. }
  54. }