RSI helpers  0.1
RSI helpers
Ini.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi;
4 
5 class Ini{
6 
7  /**
8  * Parse a configuration string.
9  * @param string $ini The contents of the ini file being parsed.
10  * @param bool $sections Include the sections names in the keys.
11  * @param int $mode INI_SCANNER_NORMAL, INI_SCANNER_RAW, or INI_SCANNER_TYPED.
12  * @see http://php.net/parse_ini_string
13  */
14  public static function fromString($ini,$sections = true,$mode = INI_SCANNER_TYPED){
15  return parse_ini_string($filename,$sections,$mode);
16  }
17  /**
18  * Parse a configuration file.
19  * @param string $filename The filename of the ini file being parsed.
20  * @param bool $sections Include the sections names in the keys.
21  * @param int $mode INI_SCANNER_NORMAL, INI_SCANNER_RAW, or INI_SCANNER_TYPED.
22  * @see http://php.net/parse_ini_file
23  */
24  public static function fromFile($filename,$sections = true,$mode = INI_SCANNER_TYPED){
25  return parse_ini_file($filename,$sections,$mode);
26  }
27  /**
28  * Encode a configuration value.
29  * @param string $prefix Key prefix.
30  * @param mixed $data Value (single or array).
31  * @param int $mode INI_SCANNER_NORMAL, INI_SCANNER_RAW, or INI_SCANNER_TYPED.
32  * @return string
33  */
34  protected static function stringEncode($prefix,$data,$mode){
35  if(!is_array($value = $data)){
36  if($mode == INI_SCANNER_TYPED){
37  if($value === null) $value = 'null';
38  elseif(is_bool($value)) $value = Str::bool($value);
39  }
40  return "$prefix = $value\n";
41  }
42  $str = '';
43  foreach($data as $key => $value)
44  $str .= self::stringEncode($prefix ? $prefix . "[$key]" : $key,$value,$mode);
45  return $str;
46  }
47  /**
48  * Write a configuration string.
49  * @param array $data Multidimensional array.
50  * @param bool $sections Use sections (first evel of data).
51  * @param int $mode INI_SCANNER_NORMAL, INI_SCANNER_RAW, or INI_SCANNER_TYPED.
52  * @return string
53  */
54  public static function toString($data,$sections = true,$mode = INI_SCANNER_TYPED){
55  if(!$sections) return self::stringEncode(null,$data,$mode);
56  $str = '';
57  foreach($data as $section => $data){
58  $str .= ($str ? "\n" : '') . "[$section]\n";
59  if($data) foreach($data as $key => $value) $str .= self::stringEncode($key,$value,$mode);
60  }
61  return $str;
62  }
63  /**
64  * Write a configuration file.
65  * @param string $filename The filename of the ini file.
66  * @param array $data Multidimensional array.
67  * @param bool $sections Use sections (first evel of data).
68  * @param int $mode INI_SCANNER_NORMAL, INI_SCANNER_RAW, or INI_SCANNER_TYPED.
69  * @return int Bytes written (false on failure).
70  */
71  public static function toFile($filename,$data,$sections = true,$mode = INI_SCANNER_TYPED){
72  return file_put_contents($filename,self::toString($data,$sections,$mode));
73  }
74 
75 }
static fromString($ini, $sections=true, $mode=INI_SCANNER_TYPED)
Parse a configuration string.
Definition: Ini.php:14
static fromFile($filename, $sections=true, $mode=INI_SCANNER_TYPED)
Parse a configuration file.
Definition: Ini.php:24
static toString($data, $sections=true, $mode=INI_SCANNER_TYPED)
Write a configuration string.
Definition: Ini.php:54
Definition: Ini.php:5
Definition: Color.php:3
static toFile($filename, $data, $sections=true, $mode=INI_SCANNER_TYPED)
Write a configuration file.
Definition: Ini.php:71
static stringEncode($prefix, $data, $mode)
Encode a configuration value.
Definition: Ini.php:34