/src/jubianchi/PhpSwitch/Config/Configuration.php
PHP | 142 lines | 77 code | 24 blank | 41 comment | 5 complexity | 54f86d990874ffcf4b8d0c4cc69e6160 MD5 | raw file
- <?php
- namespace jubianchi\PhpSwitch\Config;
- class Configuration implements \IteratorAggregate
- {
- const ROOT = 'phpswitch';
- /** @var array */
- private $configuration = array();
- /** @var \jubianchi\PhpSwitch\Config\Dumper */
- private $dumper;
- /**
- * @param string $offset
- *
- * @throws \InvalidArgumentException
- *
- * @return array
- */
- public function get($offset, $default = null)
- {
- $offset = str_replace('-', '_', $offset);
- $offset = preg_split('/(?<!\\\)\./', $offset);
- $reference = $this->configuration;
- $current = $sep = '';
- foreach ($offset as $key) {
- $key = preg_replace('/\\\\./', '.', $key);
- $current .= $sep . $key;
- if (false === array_key_exists($key, $reference)) {
- if(null === $default) {
- throw new \InvalidArgumentException(sprintf('Offset %s does not exist', $current));
- } else {
- return $default;
- }
- }
- $reference = & $reference[$key];
- $sep = '.';
- }
- return $reference;
- }
- /**
- * @param string $offset
- * @param mixed $value
- *
- * @throws \InvalidArgumentException
- *
- * @return \jubianchi\PhpSwitch\Config\Configuration
- */
- public function set($offset, $value)
- {
- $offset = str_replace('-', '_', $offset);
- $offset = preg_split('/(?<!\\\)\./', $offset);
- $reference = & $this->configuration;
- $current = $sep = '';
- foreach ($offset as $key) {
- $key = preg_replace('/\\\\./', '.', $key);
- $current .= $sep . $key;
- if (false === isset($reference[$key])) {
- $reference[$key] = null;
- }
- $reference = & $reference[$key];
- $sep = '.';
- }
- $reference = $value;
- return $this;
- }
- /**
- * @param array $values
- *
- * @return \jubianchi\PhpSwitch\Config\Configuration
- */
- public function setValues(array $values)
- {
- $this->configuration = $values;
- return $this;
- }
- /**
- * @return array
- */
- public function getValues()
- {
- return $this->configuration;
- }
- /**
- * @return \RecursiveArrayIterator
- */
- public function getIterator()
- {
- return new \RecursiveArrayIterator($this->configuration);
- }
- /**
- * @throws \RuntimeException
- *
- * @return \jubianchi\PhpSwitch\Config\Configuration
- */
- public function dump()
- {
- if (null === $this->dumper) {
- throw new \RuntimeException('No dumper available');
- }
- $this->dumper->dump('.phpswitch.yml', $this);
- return $this;
- }
- /**
- * @param \jubianchi\PhpSwitch\Config\Dumper $dumper
- *
- * @return Configuration
- */
- public function setDumper(Dumper $dumper)
- {
- $this->dumper = $dumper;
- return $this;
- }
- /**
- * @return \jubianchi\PhpSwitch\Config\Dumper
- */
- public function getDumper()
- {
- return $this->dumper;
- }
- }