Completed
Push — master ( d0ff07...576091 )
by Xeriab
04:55
created

AbstractFileParser::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Konfig
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
9
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
10
 * @link    https://xeriab.github.io/projects/konfig
11
 */
12
13
namespace Exen\Konfig\FileParser;
14
15
use Exen\Konfig\FileParser;
16
17
abstract class AbstractFileParser implements FileParser
18
{
19
    /**
20
     * Configuration file
21
     *
22
     * @var $file string
23
     * @since 0.1.0
24
     */
25
    protected $file;
26
27
    /**
28
     * The configuration variables
29
     *
30
     * @var $vars array
31
     * @since 0.1.0
32
     */
33
    protected $vars = [];
34
35
    #: Protected Methods
36
37
    /**
38
     * Parses a string using all of the previously set variables.
39
     * Allows you to use something like %ENV% in non-PHP files.
40
     *
41
     * @param string $string String to parse
42
     * @return string
43
     * @since 0.1.0
44
     */
45
    protected function parseVars($string = null)
46
    {
47
        foreach ($this->vars as $var => $val) {
48
            $string = str_replace("%$var%", $val, $string);
49
        }
50
51
        return $string;
52
    }
53
54
    /**
55
     * Replaces given constants to their string counterparts.
56
     *
57
     * @param array $array Array to be prepped
58
     * @return array Prepped array
59
     * @since 0.1.0
60
     */
61
    protected function prepVars(array &$array)
62
    {
63
        static $replace = false;
64
65
        if ($replace === false) {
66
            foreach ($this->vars as $x => $v) {
67
                $replace['#^(' . preg_quote($v) . '){1}(.*)?#'] = '%' . $x . '%$2';
68
            }
69
        }
70
71
        foreach ($array as $x => $value) {
72
            if (is_string($value)) {
73
                $array[$x] = preg_replace(
74
                        array_keys($replace), array_values($replace), $value
75
                );
76
            } elseif (is_array($value)) {
77
                $this->prepVars($array[$x]);
78
            }
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    abstract public function parse($file);
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    abstract public function getSupportedFileExtensions();
91
92
    /**
93
     * @return string
94
     * @since 0.1.2
95
     */
96
    public function __toString()
97
    {
98
        return 'Exen\Konfig\FileParser\AbstractFileParser' . PHP_EOL;
99
    }
100
101
}
102
103
#: END OF ./src/FileParser/AbstractFileParser.php FILE
104