Overview

Namespaces

  • Faulancer
    • Console
      • Handler
    • Controller
    • Exception
    • Form
      • Validator
        • Type
    • Helper
      • Reflection
    • Http
    • ORM
    • Service
      • Factory
    • ServiceLocator
    • View
      • Helper

Classes

  • Config
  • ORM
  • Overview
  • Namespace
  • Class
 1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 
<?php
/**
 * Config.php
 *
 * @package Faulancer\Service
 * @author Florian Knapp <office@florianknapp.de>
 */
namespace Faulancer\Service;

use Faulancer\Exception\ConfigInvalidException;

/**
 * Represents a configuration array
 */
class Config
{

    /**
     * Holds the configuration data
     * @var array
     */
    protected $_config = [];

    /**
     * Set configuration value by key
     *
     * @param mixed $key
     * @param mixed $value
     * @param boolean $force
     * @return boolean
     * @throws ConfigInvalidException
     */
    public function set($key, $value = null, $force = false)
    {
        if (is_array($key) && $value === null) {
            foreach ($key as $k => $v) {
                $this->set($k, $v);
            }
            return true;
        }

        if ($force || empty($this->_config[$key])) {
            $this->_config[$key] = $value;
            return true;
        }

        throw new ConfigInvalidException();
    }

    /**
     * Get configuration value by key
     *
     * @param string $key
     * @return mixed
     * @throws ConfigInvalidException
     */
    public function get($key)
    {
        if (strpos($key, ':') !== false) {
            return $this->recursive($key);
        }

        if (empty($this->_config[$key])) {
            throw new ConfigInvalidException('No value for key "' . $key . '" found.');
        }

        return $this->_config[$key];
    }

    /**
     * Iterate through configuration till given key is found
     *
     * @param $key
     * @return array|mixed
     * @throws ConfigInvalidException
     */
    private function recursive($key)
    {
        $parts  = explode(':', $key);
        $result = $this->_config;

        foreach ($parts as $part) {

            if (empty($result[$part])) {
                return '';
            }

            $result = $result[$part];
        }

        return $result;
    }

}
API documentation generated by ApiGen