|
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\Exception\KonfigException as Exception; |
|
20
|
|
|
use Exen\Konfig\Exception\ParseException; |
|
21
|
|
|
use Exen\Konfig\Utils; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Xml |
|
25
|
|
|
* Konfig's XML parser class. |
|
26
|
|
|
* |
|
27
|
|
|
* @category FileParser |
|
28
|
|
|
* @package Konfig |
|
29
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
|
30
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
|
31
|
|
|
* @link https://xeriab.github.io/projects/konfig |
|
32
|
|
|
* |
|
33
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
|
34
|
|
|
*/ |
|
35
|
|
|
class Xml extends AbstractFileParser |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Loads a XML file as an array. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $path File path |
|
41
|
|
|
* |
|
42
|
|
|
* @throws ParseException If there is an error parsing XML file |
|
43
|
|
|
* |
|
44
|
|
|
* @return array The parsed data |
|
45
|
|
|
* |
|
46
|
|
|
* @since 0.1.0 |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function parse($path) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$data = $this->loadFile($path); |
|
51
|
|
|
|
|
52
|
6 |
|
if ($data === false) { |
|
53
|
3 |
|
$lastError = libxml_get_last_error(); |
|
54
|
|
|
|
|
55
|
3 |
|
if ($lastError !== false) { |
|
56
|
3 |
|
throw new ParseException( |
|
57
|
|
|
[ |
|
58
|
3 |
|
'message' => $lastError->message, |
|
59
|
3 |
|
'type' => $lastError->level, |
|
60
|
3 |
|
'code' => $lastError->code, |
|
61
|
3 |
|
'file' => $lastError->file, |
|
62
|
3 |
|
'line' => $lastError->line, |
|
63
|
|
|
] |
|
64
|
2 |
|
); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
return json_decode(json_encode($data), true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
* |
|
74
|
|
|
* @return array Supported extensions |
|
75
|
|
|
* |
|
76
|
|
|
* @since 0.1.0 |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public function getSupportedFileExtensions() |
|
79
|
|
|
{ |
|
80
|
3 |
|
return ['xml']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Loads in the given file and parses it. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $file File to load |
|
87
|
|
|
* |
|
88
|
|
|
* @return array The parsed file data |
|
89
|
|
|
* |
|
90
|
|
|
* @since 0.2.4 |
|
91
|
|
|
* @codeCoverageIgnore |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function loadFile($file = null) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->file = $file; |
|
96
|
|
|
$contents = $this->parseVars(Utils::getContent($file)); |
|
97
|
|
|
|
|
98
|
|
|
return simplexml_load_string( |
|
|
|
|
|
|
99
|
|
|
$contents, |
|
100
|
|
|
'SimpleXMLElement', |
|
101
|
|
|
LIBXML_NOWARNING | LIBXML_NOERROR |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the formatted configuration file contents. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $contents configuration array |
|
109
|
|
|
* |
|
110
|
|
|
* @return string formatted configuration file contents |
|
111
|
|
|
* |
|
112
|
|
|
* @since 0.2.4 |
|
113
|
|
|
* @codeCoverageIgnore |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function exportFormat($contents = null) |
|
116
|
|
|
{ |
|
117
|
|
|
throw new Exception( |
|
118
|
|
|
'Saving configuration to `XML` is not supported at this time' |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// END OF ./src/FileParser/Xml.php FILE |
|
124
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.