AbstractFileParser::prepVars()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.439
cc 6
eloc 16
nc 9
nop 1
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig\FileParser;
18
19
use Exen\Konfig\FileParser;
20
21
/**
22
 * Konfig's abstract file parser class.
23
 *
24
 * @category FileParser
25
 * @package  Konfig
26
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
27
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
28
 * @link     https://xeriab.github.io/projects/konfig
29
 *
30
 * @implements Exen\Konfig\FileParser
31
 */
32
abstract class AbstractFileParser implements FileParser
33
{
34
    /**
35
     * Configuration file.
36
     *
37
     * @var string $file
38
     *
39
     * @since 0.1.0
40
     */
41
    protected $file;
42
43
    /**
44
     * The configuration variables.
45
     *
46
     * @var array $variables
47
     *
48
     * @since 0.1.0
49
     */
50
    protected $variables = [];
51
52
    // PROTECTED METHODS
53
54
    /**
55
     * Parses a string using all of the previously set variables.
56
     * Allows you to use something like %ENV% in non-PHP files.
57
     *
58
     * @param string $string String to parse
59
     *
60
     * @return             string
61
     * @since              0.1.0
62
     * @codeCoverageIgnore
63
     */
64
    protected function parseVars($string = null)
65
    {
66
        foreach ($this->variables as $var => $value) {
67
            $string = str_replace("%$var%", $value, $string);
68
        }
69
70
        return $string;
71
    }
72
73
    /**
74
     * Replaces given constants to their string counterparts.
75
     *
76
     * @param array $array Array to be prepped
77
     *
78
     * @return             array Prepped array
79
     * @since              0.1.0
80
     * @codeCoverageIgnore
81
     */
82
    protected function prepVars(array &$array)
83
    {
84
        static $replace = false;
85
86
        if ($replace === false) {
87
            foreach ($this->variables as $key => $value) {
88
                $replace['#^('.
89
                    preg_quote($value).
90
                    '){1}(.*)?#'
91
                ] = '%'.$key.'%$2';
92
            }
93
        }
94
95
        foreach ($array as $key => $value) {
96
            if (is_string($value)) {
97
                $array[$key] = preg_replace(
98
                    array_keys($replace),
99
                    array_values($replace),
100
                    $value
101
                );
102
            } elseif (is_array($value)) {
103
                $this->prepVars($array[$key]);
104
            }
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     *
111
     * @param string $file File path
112
     *
113
     * @return array The parsed data
114
     *
115
     * @since 0.1.0
116
     */
117
    abstract public function parse($file);
118
119
    /**
120
     * {@inheritdoc}
121
     *
122
     * @return array Supported extensions
123
     *
124
     * @since 0.1.0
125
     */
126
    abstract public function getSupportedFileExtensions();
127
128
    /**
129
     * Must be implemented by child class. Gets called for each file to load.
130
     *
131
     * @param string $file File path
132
     *
133
     * @return array The parsed file data
134
     *
135
     * @since              0.2.4
136
     * @codeCoverageIgnore
137
     */
138
    abstract protected function loadFile($file = null);
139
140
    /**
141
     * Must be impletmented by child class. Gets called when saving a config file.
142
     *
143
     * @param array $contents config array to save
144
     *
145
     * @return string formatted output
146
     *
147
     * @since              0.2.4
148
     * @codeCoverageIgnore
149
     */
150
    abstract protected function exportFormat($contents = null);
151
152
    /**
153
     * Gets the default group name.
154
     *
155
     * @return string
156
     *
157
     * @since              0.2.4
158
     * @codeCoverageIgnore
159
     */
160
    public function group()
161
    {
162
        return $this->file;
163
    }
164
165
    /**
166
     * __toString.
167
     *
168
     * @return             string
169
     * @since              0.1.2
170
     * @codeCoverageIgnore
171
     */
172
    public function __toString()
173
    {
174
        return 'Exen\Konfig\FileParser\AbstractFileParser' . PHP_EOL;
175
    }
176
}
177
178
// END OF ./src/FileParser/AbstractFileParser.php FILE
179