-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoSectionsConfigParser.php
165 lines (150 loc) · 5.12 KB
/
NoSectionsConfigParser.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* This file is part of NoiseLabs-PHP-ToolKit
*
* NoiseLabs-PHP-ToolKit is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with NoiseLabs-PHP-ToolKit; if not, see
* <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011 Vítor Brandão <[email protected]>
*
*
* @category NoiseLabs
* @package ConfigParser
* @version 0.1.1
* @author Vítor Brandão <[email protected]>
* @copyright (C) 2011 Vítor Brandão <[email protected]>
*/
namespace NoiseLabs\ToolKit\ConfigParser;
use NoiseLabs\ToolKit\ConfigParser\File;
use NoiseLabs\ToolKit\ConfigParser\Exception\NoOptionException;
/**
* This class is a version of the ConfigParser class meant to be used for
* configuration files that don't have sections.
*
* @author Vítor Brandão <[email protected]>
*/
class NoSectionsConfigParser extends BaseConfigParser implements NoSectionsConfigParserInterface
{
const HAS_SECTIONS = false;
/**
* Return a list of options available
*/
public function options()
{
return array_keys($this->_sections);
}
/**
* If the given option exists, return TRUE; otherwise return FALSE.
*/
public function hasOption($option)
{
return isset($this->_sections[$option]);
}
/**
* Get an option value for the named section.
* If the option doesn't exist in the configuration $defaults is used.
* If $defaults doesn't have this option too then we look for the
* $fallback parameter.
* If everything fails throw a NoOptionException.
*
* @param $option Option name
* @param $fallback A fallback value to use if the option isn't found in
* the configuration.
*
* @return Option value (if available)
* @throws NoOptionException Couldn't find the desired option in the
* configuration or as a fallback value.
*/
public function get($option, $fallback = null)
{
if ($this->hasOption($option)) {
return $this->_sections[$option];
}
// try $fallback
elseif (isset($fallback)) {
return $fallback;
} else {
if ($this->_throwExceptions()) {
throw new NoOptionException('<None>', $option);
} else {
error_log(sprintf("Option '%s' wasn't found", $option));
return null;
}
}
}
/**
* A convenience method which coerces the option value to an integer.
*/
public function getInt($option, $fallback = null)
{
return (int) $this->get($option);
}
/**
* A convenience method which coerces the option value to a floating
* point number.
*/
public function getFloat($option, $fallback = null)
{
return (float) $this->get($option);
}
/**
* A convenience method which coerces the option value to a Boolean value.
* Note that the accepted values for the option are '1', 'yes', 'true',
* and 'on', which cause this method to return TRUE, and '0', 'no',
* 'false', and 'off', which cause it to return FALSE.
* These string values are checked in a case-insensitive manner. Any
* other value will cause it to raise ValueException.
*/
public function getBoolean($option, $fallback = null)
{
if (is_string($value = $this->get($option, $fallback))) {
$value = strtolower($value);
}
if (isset($this->_boolean_states[$value])) {
return $this->_boolean_states[$value];
} else {
$error_msg = "Option '".$option."' is not a boolean";
if ($this->_throwExceptions()) {
throw new \UnexpectedValueException($error_msg);
} else {
error_log($error_msg);
return null;
}
}
}
public function _buildOutputString()
{
$output = '';
foreach ($this->_sections as $key => $value) {
// option name
$line = $key;
// space before delimiter?
if ($this->settings->get('space_around_delimiters') &&
$this->settings->get('delimiter') != ':') {
$line .= ' ';
}
// insert delimiter
$line .= $this->settings->get('delimiter');
// space after delimiter?
if ($this->settings->get('space_around_delimiters')) {
$line .= ' ';
}
// and finally, option value
$line .= $value;
// record it for eternity
$output .= $line.$this->settings->get('linebreak');
}
return $output;
}
}