Overview

Namespaces

  • PHP
  • PhpOptions
    • Types

Classes

  • AType
  • CharType
  • DatetimeType
  • DateType
  • DirectoryType
  • EmailType
  • EnumType
  • FileType
  • InifileType
  • IntegerType
  • RealType
  • SeriesType
  • StringType
  • TimeType
  • Types
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * PhpOptions
  5:  * @link https://github.com/masicek/PhpOptions
  6:  * @author Viktor Mašíček <viktor@masicek.net>
  7:  * @license "New" BSD License
  8:  */
  9: 
 10: namespace PhpOptions\Types;
 11: 
 12: require_once __DIR__ . '/FileType.php';
 13: 
 14: /**
 15:  * Inifile type
 16:  *
 17:  * @author Viktor Mašíček <viktor@masicek.net>
 18:  */
 19: class InifileType extends FileType
 20: {
 21: 
 22:     /**
 23:      * Flag for use of sections
 24:      *
 25:      * @var bool
 26:      */
 27:     private $sections = TRUE;
 28: 
 29:     /**
 30:      * List of delimiters for explode input value into array
 31:      *
 32:      * @var string
 33:      */
 34:     private $delimiters = ';|';
 35: 
 36: 
 37:     /**
 38:      * Set object
 39:      * 'notSection' => read all values together and do not use sections
 40:      *
 41:      * @param array $setting Array of setting of object
 42:      */
 43:     public function __construct($settings = array())
 44:     {
 45:         parent::__construct($settings);
 46:         if ($this->settingsHasFlag('notSections', $settings))
 47:         {
 48:             $this->sections = FALSE;
 49:         }
 50:     }
 51: 
 52: 
 53:     /**
 54:      * Return modified value
 55:      *
 56:      * @param mixed $value Filtered value
 57:      *
 58:      * @return mixed
 59:      */
 60:     protected function useFilter($value)
 61:     {
 62:         $file = parent::useFilter($value);
 63:         $content = parse_ini_file($file, $this->sections);
 64:         if ($this->sections)
 65:         {
 66:             $content = $this->mergeParent($content);
 67:             $contentTmp = array();
 68:             foreach ($content as $section => $values)
 69:             {
 70:                 $contentTmp[$section] = $this->makeSubArray($values);
 71:             }
 72:             $content = $contentTmp;
 73:         }
 74:         else
 75:         {
 76:             $content = $this->makeSubArray($content);
 77:         }
 78:         return $content;
 79:     }
 80: 
 81: 
 82:     /**
 83:      * If some section is childe of some another section, merge theese sesctions
 84:      *
 85:      * @param array $content Contentn of all ini file
 86:      *
 87:      * @return array
 88:      */
 89:     private function mergeParent($content)
 90:     {
 91:         $contentTmp = array();
 92:         foreach ($content as $section => $values)
 93:         {
 94:             $position = strpos($section, '<');
 95:             if ($position !== FALSE)
 96:             {
 97:                 $child = trim(substr($section, 0, $position - 1));
 98:                 $parent = trim(substr($section, $position + 1));
 99: 
100:                 if ($child && $parent && isset($contentTmp[$parent]))
101:                 {
102:                     $section = $child;
103:                     $values = array_merge($contentTmp[$parent], $values);
104:                 }
105:             }
106:             $contentTmp[$section] = $values;
107:         }
108: 
109:         return $contentTmp;
110:     }
111: 
112: 
113:     /**
114:      * Make subarray from values type of "foo.bar1"
115:      *
116:      * Example:
117:      * from "array('foo.bar1' => 'a', 'foo.bar2' = 'b')"
118:      * make "array('foo' =>array('bar1' => 'a', 'bar1' => 'b'))"
119:      *
120:      * @param array $content Content of all ini file
121:      *
122:      * @return array
123:      */
124:     private function makeSubArray($content)
125:     {
126:         $contentNew = array();
127:         foreach ($content as $key => $value)
128:         {
129:             $keysNew = explode('.', $key);
130:             $contentNewPointer = &$contentNew;
131:             foreach ($keysNew as $keyNew)
132:             {
133:                 if (!isset($contentNewPointer[$keyNew]))
134:                 {
135:                     $contentNewPointer[$keyNew] = array();
136:                 }
137:                 $contentNewPointer = &$contentNewPointer[$keyNew];
138:             }
139: 
140:             // explode array
141:             $value = preg_replace('/[' . $this->delimiters . ']+/', $this->delimiters[0], $value);
142:             $value = explode($this->delimiters[0], $value);
143:             $contentNewPointer = (count($value) == 1) ? $value[0] : $value;
144:         }
145: 
146:         return $contentNew;
147:     }
148: 
149: 
150: }
151: 
PhpOptions API documentation generated by ApiGen.
Generated using the TokenReflection library.