openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Config.class.php (4173B)


      1 <?php
      2 
      3 namespace configuration;
      4 
      5 /**
      6  * Reading configuration values.
      7  */
      8 class Config
      9 {
     10 	/**
     11 	 * The actual configuraton values.
     12 	 * @var array
     13 	 */
     14     private $config;
     15 
     16 
     17     /**
     18      * Config constructor.
     19      * @param array $config
     20      */
     21     public function __construct($config)
     22     {
     23         $this->config = $config;
     24     }
     25 
     26 
     27 	/**
     28 	 * Returns a list of all subsets.
     29 	 *
     30 	 * @return Config[] subsets
     31 	 */
     32     public function subsets() {
     33 
     34     	return array_map( function( $value ) {
     35 			if (is_array($value))
     36 				return new Config($value);
     37 			else
     38 				return new Config(array());
     39 		}, array_filter($this->config, function($value) {
     40 			// All non-arrays are removed.
     41 			return is_array($value);
     42 		}) );
     43 
     44 	}
     45 
     46 
     47 	/**
     48      * Giving the child configuration with a fluent interface.
     49      *
     50      * @param $names string|array
     51      * @return Config
     52      */
     53     public function subset($names)
     54     {
     55     	if   ( !is_array($names) )
     56     		$names = [$names];
     57 
     58     	$config = $this->config;
     59     	foreach($names as $key )
     60 			if (isset($config[$key]) && is_array($config[$key]))
     61 	    		$config = $config[$key];
     62 			else
     63 				return new Config( [] );
     64 
     65         return new Config( $config );
     66     }
     67 
     68 
     69 	/**
     70 	 * Gets the configuration value in seconds for this key.
     71 	 *
     72 	 * @param $name
     73 	 * @param null $default
     74 	 * @return int
     75 	 */
     76     public function getSeconds( $name, $default = 0 ) {
     77     	return $this->parseSuffix( $this->get( $name, (string)$default ),[
     78     		's' => 1,            // seconds
     79     		'm' => 60,           // minutes
     80     		'h' => 60*60,        // hours
     81     		'd' => 60*60*24,     // days
     82     		'y' => 60*60*24*365, // years
     83 		] );
     84 	}
     85 
     86 	/**
     87 	 * Gets the configuration value in bytes for this key.
     88 	 *
     89 	 * @param $name
     90 	 * @param null $default
     91 	 * @return int
     92 	 */
     93     public function getBytes( $name, $default = 0 ) {
     94     	return $this->parseSuffix( $this->get( $name, (string)$default ),[
     95 			'b' => 1,                        // bytes
     96 			'k' => 1024,                     // kibibyte
     97 			'm' => 1024*1024,                // mibibyte
     98 			'g' => 1024*1024*1024,           // gibibyte
     99 			't' => 1024*1024*1024*1024,      // tebibyte
    100 			'p' => 1024*1024*1024*1024*1024, // pebibyte
    101 			// exabyte, zettabyte, yottabyte, google, buuum...
    102 		] );
    103 	}
    104 
    105     /**
    106      * Gets the configuration value for this key.
    107      *
    108      * @param $name
    109      * @param null $default
    110      * @return mixed|null
    111      */
    112     public function get($name, $default = null)
    113     {
    114         if (isset($this->config[$name])) {
    115             $value = $this->config[$name];
    116 
    117             // if default-value is given, the type of the default-value is forced.
    118             if (!is_null($default))
    119                 settype($value, gettype($default));
    120             return $value;
    121         } else {
    122             return $default;
    123         }
    124     }
    125 
    126 
    127     /**
    128      * Is the Config key present?
    129      *
    130      * @param $name
    131      * @return bool
    132      */
    133     public function has($name)
    134     {
    135         return isset($this->config[$name]);
    136     }
    137 
    138 
    139     /**
    140      * Is the boolean Value true?
    141      *
    142      * @param $name
    143      * @param bool $default false
    144      * @return bool
    145      */
    146     public function is($name, $default = false)
    147     {
    148         if (isset($this->config[$name]))
    149         	// This filter accepts 'true' and 'yes' for true and 'false' and 'no' for false.
    150             return filter_var( $this->config[$name],FILTER_VALIDATE_BOOLEAN );
    151         else
    152             return (bool) $default;
    153     }
    154 
    155 
    156     /**
    157      * Gets the underlying configuration settings as a bare array.
    158      *
    159      * @return array
    160      */
    161     public function getConfig()
    162     {
    163 
    164         return $this->config;
    165     }
    166 
    167 
    168 	/**
    169 	 * Merges another Configuration
    170 	 * @param $otherConfig Config
    171 	 */
    172     public function merge( $otherConfig ) {
    173     	$this->config = array_merge( $this->config, $otherConfig->config );
    174     	return $this;
    175 	}
    176 
    177 	public function isEmpty()
    178 	{
    179 		return (boolean) $this->config;
    180 	}
    181 
    182 	public function hasContent()
    183 	{
    184 		return !$this->isEmpty();
    185 	}
    186 
    187 	private function parseSuffix( $value, $map)
    188 	{
    189 		$suffix = substr($value,-1,1);
    190 		$value  = substr($value,1);
    191 		if   ( isset($map[$suffix] ) )
    192 			return $value * $map[$suffix];
    193 		else
    194 			return (int) $value;
    195 	}
    196 }