1 : <?php
2 : /**
3 : * This file is part of NoiseLabs-PHP-ToolKit
4 : *
5 : * NoiseLabs-PHP-ToolKit is free software; you can redistribute it
6 : * and/or modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 3 of the License, or (at your option) any later version.
9 : *
10 : * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
11 : * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 : * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with NoiseLabs-PHP-ToolKit; if not, see
17 : * <http://www.gnu.org/licenses/>.
18 : *
19 : * Copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
20 : *
21 : *
22 : * @category NoiseLabs
23 : * @package ConfigParser
24 : * @version 0.1.1
25 : * @author Vítor Brandão <noisebleed@noiselabs.org>
26 : * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
27 : */
28 :
29 : namespace NoiseLabs\ToolKit\ConfigParser;
30 :
31 : use NoiseLabs\ToolKit\ConfigParser\File;
32 : use NoiseLabs\ToolKit\ConfigParser\Exception\NoOptionException;
33 :
34 : /**
35 : * This class is a version of the ConfigParser class meant to be used for
36 : * configuration files that don't have sections.
37 : *
38 : * @author Vítor Brandão <noisebleed@noiselabs.org>
39 : */
40 0 : class NoSectionsConfigParser extends BaseConfigParser implements NoSectionsConfigParserInterface
41 : {
42 : const HAS_SECTIONS = false;
43 :
44 : /**
45 : * Return a list of options available
46 : */
47 : public function options()
48 : {
49 0 : return array_keys($this->_sections);
50 : }
51 :
52 : /**
53 : * If the given option exists, return TRUE; otherwise return FALSE.
54 : */
55 : public function hasOption($option)
56 : {
57 0 : return isset($this->_sections[$option]);
58 : }
59 :
60 : /**
61 : * Get an option value for the named section.
62 : * If the option doesn't exist in the configuration $defaults is used.
63 : * If $defaults doesn't have this option too then we look for the
64 : * $fallback parameter.
65 : * If everything fails throw a NoOptionException.
66 : *
67 : * @param $option Option name
68 : * @param $fallback A fallback value to use if the option isn't found in
69 : * the configuration.
70 : *
71 : * @return Option value (if available)
72 : * @throws NoOptionException Couldn't find the desired option in the
73 : * configuration or as a fallback value.
74 : */
75 : public function get($option, $fallback = null)
76 : {
77 0 : if ($this->hasOption($option)) {
78 0 : return $this->_sections[$option];
79 : }
80 : // try $fallback
81 0 : elseif (isset($fallback)) {
82 0 : return $fallback;
83 : }
84 : else {
85 0 : if ($this->_throwExceptions()) {
86 0 : throw new NoOptionException('<None>', $option);
87 : }
88 : else {
89 0 : error_log(sprintf("Option '%s' wasn't found", $option));
90 0 : return null;
91 : }
92 : }
93 : }
94 :
95 : /**
96 : * A convenience method which coerces the option value to an integer.
97 : */
98 : public function getInt($option, $fallback = null)
99 : {
100 0 : return (int) $this->get($option);
101 : }
102 :
103 : /**
104 : * A convenience method which coerces the option value to a floating
105 : * point number.
106 : */
107 : public function getFloat($option, $fallback = null)
108 : {
109 0 : return (float) $this->get($option);
110 : }
111 :
112 : /**
113 : * A convenience method which coerces the option value to a Boolean value.
114 : * Note that the accepted values for the option are '1', 'yes', 'true',
115 : * and 'on', which cause this method to return TRUE, and '0', 'no',
116 : * 'false', and 'off', which cause it to return FALSE.
117 : * These string values are checked in a case-insensitive manner. Any
118 : * other value will cause it to raise ValueException.
119 : */
120 : public function getBoolean($option, $fallback = null)
121 : {
122 0 : if (is_string($value = $this->get($option, $fallback))) {
123 0 : $value = strtolower($value);
124 0 : }
125 :
126 0 : if (isset($this->_boolean_states[$value])) {
127 0 : return $this->_boolean_states[$value];
128 : }
129 : else {
130 0 : $error_msg = "Option '".$option."' is not a boolean";
131 0 : if ($this->_throwExceptions()) {
132 0 : throw new \UnexpectedValueException($error_msg);
133 : }
134 : else {
135 0 : error_log($error_msg);
136 0 : return null;
137 : }
138 : }
139 : }
140 :
141 : public function _buildOutputString()
142 : {
143 0 : $output = '';
144 :
145 0 : foreach ($this->_sections as $key => $value) {
146 : // option name
147 0 : $line = $key;
148 : // space before delimiter?
149 0 : if ($this->settings->get('space_around_delimiters') &&
150 0 : $this->settings->get('delimiter') != ':') {
151 0 : $line .= ' ';
152 0 : }
153 : // insert delimiter
154 0 : $line .= $this->settings->get('delimiter');
155 : // space after delimiter?
156 0 : if ($this->settings->get('space_around_delimiters')) {
157 0 : $line .= ' ';
158 0 : }
159 : // and finally, option value
160 0 : $line .= $value;
161 : // record it for eternity
162 0 : $output .= $line.$this->settings->get('linebreak');
163 0 : }
164 :
165 0 : return $output;
166 : }
|