|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jumilla\Generators\Php; |
|
4
|
|
|
|
|
5
|
|
|
class ConfigGenerator |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Generate php source text. |
|
9
|
|
|
* |
|
10
|
|
|
* @param array $config |
|
11
|
|
|
* @param string $namespace |
|
12
|
|
|
* |
|
13
|
|
|
* @return static |
|
14
|
|
|
*/ |
|
15
|
3 |
|
public static function generateText(array $config, $namespace = null) |
|
16
|
|
|
{ |
|
17
|
3 |
|
$instance = new static(); |
|
18
|
|
|
|
|
19
|
3 |
|
return $instance->generate($config, $namespace); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $text; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $indent; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generate php source text. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $config |
|
36
|
|
|
* @param string $namespace |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function generate(array $config, $namespace = null) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->writeLine('<?php'.PHP_EOL); |
|
43
|
|
|
|
|
44
|
5 |
|
if ($namespace) { |
|
|
|
|
|
|
45
|
|
|
$this->writeLine("namespace $namespace;".PHP_EOL); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
$this->indent = 0; |
|
49
|
|
|
|
|
50
|
5 |
|
$this->writeLine('return ['); |
|
51
|
|
|
|
|
52
|
5 |
|
$this->generateArray($config); |
|
53
|
|
|
|
|
54
|
5 |
|
$this->writeLine('];'); |
|
55
|
|
|
|
|
56
|
5 |
|
return $this->text; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Generate php array elements |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $config |
|
63
|
|
|
*/ |
|
64
|
5 |
|
private function generateArray(array $config) |
|
65
|
|
|
{ |
|
66
|
5 |
|
++$this->indent; |
|
67
|
|
|
|
|
68
|
5 |
|
foreach ($config as $key => $value) { |
|
69
|
4 |
|
if (is_null($value)) { |
|
70
|
2 |
|
if (is_string($key)) { |
|
71
|
1 |
|
$this->writeLine(sprintf("'%s' => %s,", $key, 'null')); |
|
72
|
1 |
|
} else { |
|
73
|
1 |
|
$this->writeLine(sprintf('%s,', 'null')); |
|
74
|
|
|
} |
|
75
|
4 |
|
} elseif (is_bool($value)) { |
|
76
|
2 |
|
if (is_string($key)) { |
|
77
|
1 |
|
$this->writeLine(sprintf("'%s' => %s,", $key, $value ? 'true' : 'false')); |
|
78
|
1 |
|
} else { |
|
79
|
1 |
|
$this->writeLine(sprintf('%s,', $value ? 'true' : 'false')); |
|
80
|
|
|
} |
|
81
|
4 |
View Code Duplication |
} elseif (is_string($value)) { |
|
|
|
|
|
|
82
|
4 |
|
if (is_string($key)) { |
|
83
|
3 |
|
$this->writeLine(sprintf("'%s' => '%s',", $key, $value)); |
|
84
|
3 |
|
} else { |
|
85
|
1 |
|
$this->writeLine(sprintf("'%s',", $value)); |
|
86
|
|
|
} |
|
87
|
4 |
|
} elseif (is_array($value)) { |
|
88
|
2 |
|
if (is_string($key)) { |
|
89
|
1 |
|
$this->writeLine(sprintf("'%s' => [", $key)); |
|
90
|
1 |
|
} else { |
|
91
|
1 |
|
$this->writeLine('['); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
$this->generateArray($value); |
|
95
|
|
|
|
|
96
|
2 |
|
$this->writeLine('],'); |
|
97
|
2 |
View Code Duplication |
} elseif ($value instanceof ClassName) { |
|
|
|
|
|
|
98
|
2 |
|
if (is_string($key)) { |
|
99
|
1 |
|
$this->writeLine(sprintf("'%s' => %s,", $key, (string) $value)); |
|
100
|
1 |
|
} else { |
|
101
|
1 |
|
$this->writeLine(sprintf('%s,', (string) $value)); |
|
102
|
|
|
} |
|
103
|
2 |
|
} else { |
|
104
|
2 |
|
if (is_string($key)) { |
|
105
|
1 |
|
$this->writeLine(sprintf("'%s' => %s,", $key, $value)); |
|
106
|
1 |
|
} else { |
|
107
|
1 |
|
$this->writeLine($value.','); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
5 |
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
--$this->indent; |
|
113
|
5 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Write a source line. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $line |
|
119
|
|
|
*/ |
|
120
|
5 |
|
private function writeLine($line) |
|
121
|
|
|
{ |
|
122
|
5 |
|
$this->text .= str_repeat(' ', $this->indent * 4); |
|
123
|
5 |
|
$this->text .= $line; |
|
124
|
5 |
|
$this->text .= PHP_EOL; |
|
125
|
5 |
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: