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

/src/jubianchi/PhpSwitch/Config/Configuration.php

https://github.com/horechek/phpswitch
PHP | 142 lines | 77 code | 24 blank | 41 comment | 5 complexity | 54f86d990874ffcf4b8d0c4cc69e6160 MD5 | raw file
  1. <?php
  2. namespace jubianchi\PhpSwitch\Config;
  3. class Configuration implements \IteratorAggregate
  4. {
  5. const ROOT = 'phpswitch';
  6. /** @var array */
  7. private $configuration = array();
  8. /** @var \jubianchi\PhpSwitch\Config\Dumper */
  9. private $dumper;
  10. /**
  11. * @param string $offset
  12. *
  13. * @throws \InvalidArgumentException
  14. *
  15. * @return array
  16. */
  17. public function get($offset, $default = null)
  18. {
  19. $offset = str_replace('-', '_', $offset);
  20. $offset = preg_split('/(?<!\\\)\./', $offset);
  21. $reference = $this->configuration;
  22. $current = $sep = '';
  23. foreach ($offset as $key) {
  24. $key = preg_replace('/\\\\./', '.', $key);
  25. $current .= $sep . $key;
  26. if (false === array_key_exists($key, $reference)) {
  27. if(null === $default) {
  28. throw new \InvalidArgumentException(sprintf('Offset %s does not exist', $current));
  29. } else {
  30. return $default;
  31. }
  32. }
  33. $reference = & $reference[$key];
  34. $sep = '.';
  35. }
  36. return $reference;
  37. }
  38. /**
  39. * @param string $offset
  40. * @param mixed $value
  41. *
  42. * @throws \InvalidArgumentException
  43. *
  44. * @return \jubianchi\PhpSwitch\Config\Configuration
  45. */
  46. public function set($offset, $value)
  47. {
  48. $offset = str_replace('-', '_', $offset);
  49. $offset = preg_split('/(?<!\\\)\./', $offset);
  50. $reference = & $this->configuration;
  51. $current = $sep = '';
  52. foreach ($offset as $key) {
  53. $key = preg_replace('/\\\\./', '.', $key);
  54. $current .= $sep . $key;
  55. if (false === isset($reference[$key])) {
  56. $reference[$key] = null;
  57. }
  58. $reference = & $reference[$key];
  59. $sep = '.';
  60. }
  61. $reference = $value;
  62. return $this;
  63. }
  64. /**
  65. * @param array $values
  66. *
  67. * @return \jubianchi\PhpSwitch\Config\Configuration
  68. */
  69. public function setValues(array $values)
  70. {
  71. $this->configuration = $values;
  72. return $this;
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function getValues()
  78. {
  79. return $this->configuration;
  80. }
  81. /**
  82. * @return \RecursiveArrayIterator
  83. */
  84. public function getIterator()
  85. {
  86. return new \RecursiveArrayIterator($this->configuration);
  87. }
  88. /**
  89. * @throws \RuntimeException
  90. *
  91. * @return \jubianchi\PhpSwitch\Config\Configuration
  92. */
  93. public function dump()
  94. {
  95. if (null === $this->dumper) {
  96. throw new \RuntimeException('No dumper available');
  97. }
  98. $this->dumper->dump('.phpswitch.yml', $this);
  99. return $this;
  100. }
  101. /**
  102. * @param \jubianchi\PhpSwitch\Config\Dumper $dumper
  103. *
  104. * @return Configuration
  105. */
  106. public function setDumper(Dumper $dumper)
  107. {
  108. $this->dumper = $dumper;
  109. return $this;
  110. }
  111. /**
  112. * @return \jubianchi\PhpSwitch\Config\Dumper
  113. */
  114. public function getDumper()
  115. {
  116. return $this->dumper;
  117. }
  118. }