AbstractConfig.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 8qaWRX4fmVzeIuE/7ypLWRJuk20Z8OMS91Wg6xSA2K0=
  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 object
  14. */
  15. namespace Cube\Config;
  16. abstract class AbstractConfig
  17. {
  18. /**
  19. *
  20. * the data that the config object holds
  21. *
  22. * @var mixed
  23. */
  24. protected $_data;
  25. /**
  26. *
  27. * a node of the config array
  28. *
  29. * @var string
  30. */
  31. protected $_node;
  32. /**
  33. *
  34. * class constructor
  35. * accepts a file, the location of the config file, and a node, in case we want to only hold a
  36. * part of the configuration array in the config object
  37. *
  38. * @param string $data
  39. * @param string $node
  40. */
  41. public function __construct($data = null, $node = null)
  42. {
  43. if ($data !== null) {
  44. $this->setData($data);
  45. }
  46. $this->setNode($node);
  47. }
  48. /**
  49. *
  50. * get the data held in the config container as an array
  51. * if a node is not found, return an empty array rather than the whole data array
  52. *
  53. * @param string $node
  54. *
  55. * @return array
  56. */
  57. public function getData($node = null)
  58. {
  59. if ($node !== null) {
  60. $this->setNode($node);
  61. }
  62. if ($this->_node !== null) {
  63. $iterator = new \RecursiveIteratorIterator(
  64. new \RecursiveArrayIterator($this->_data),
  65. \RecursiveIteratorIterator::SELF_FIRST);
  66. foreach ($iterator as $key => $array) {
  67. if ($key == $this->_node) {
  68. return $array;
  69. }
  70. }
  71. return array();
  72. }
  73. return (array)$this->_data;
  74. }
  75. /**
  76. *
  77. * set the config object data
  78. *
  79. * @param mixed $data
  80. *
  81. * @return $this
  82. */
  83. public function setData($data)
  84. {
  85. $this->clearData();
  86. $this->addData($data);
  87. return $this;
  88. }
  89. /**
  90. *
  91. * clear data
  92. *
  93. * @return $this
  94. */
  95. public function clearData()
  96. {
  97. $this->_data = array();
  98. return $this;
  99. }
  100. /**
  101. *
  102. * get the node name
  103. *
  104. * @return string
  105. */
  106. public function getNode()
  107. {
  108. return $this->_node;
  109. }
  110. /**
  111. *
  112. * set the node for the config array
  113. *
  114. * @param string $node
  115. */
  116. public function setNode($node)
  117. {
  118. $this->_node = $node;
  119. }
  120. /**
  121. *
  122. * add data
  123. *
  124. * @param array $data
  125. *
  126. * @return $this
  127. */
  128. abstract public function addData($data);
  129. }