1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Variable; |
4
|
|
|
|
5
|
|
|
use Pageon\Html\Image\ImageFactory; |
6
|
|
|
use Pageon\Lib\Markdown\MarkdownParser; |
7
|
|
|
use Stitcher\DynamicFactory; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
use TypeError; |
10
|
|
|
|
11
|
|
|
class VariableFactory extends DynamicFactory |
12
|
|
|
{ |
13
|
|
|
/** @var \Symfony\Component\Yaml\Yaml */ |
14
|
|
|
private $yamlParser; |
15
|
|
|
|
16
|
|
|
/** @var \Pageon\Lib\Markdown\MarkdownParser */ |
17
|
|
|
private $markdownParser; |
18
|
|
|
|
19
|
|
|
/** @var \Pageon\Html\Image\ImageFactory */ |
20
|
|
|
private $imageParser; |
21
|
|
|
|
22
|
|
|
/** @var \Stitcher\Variable\VariableParser */ |
23
|
|
|
private $variableParser; |
24
|
|
|
|
25
|
3 |
|
public function __construct() |
26
|
|
|
{ |
27
|
3 |
|
$this->setJsonRule(); |
28
|
3 |
|
$this->setYamlRule(); |
29
|
3 |
|
$this->setMarkdownRule(); |
30
|
3 |
|
$this->setImageRule(); |
31
|
3 |
|
$this->setDirectoryRule(); |
32
|
3 |
|
$this->setHtmlRule(); |
33
|
3 |
|
} |
34
|
|
|
|
35
|
3 |
|
public static function make(): VariableFactory |
36
|
|
|
{ |
37
|
3 |
|
return new self(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setYamlParser(Yaml $yamlParser): VariableFactory |
41
|
|
|
{ |
42
|
|
|
$this->yamlParser = $yamlParser; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setMarkdownParser(MarkdownParser $markdownParser): VariableFactory |
48
|
|
|
{ |
49
|
|
|
$this->markdownParser = $markdownParser; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setImageParser(ImageFactory $imageParser): VariableFactory |
55
|
|
|
{ |
56
|
|
|
$this->imageParser = $imageParser; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
public function setVariableParser(VariableParser $variableParser): VariableFactory |
62
|
|
|
{ |
63
|
3 |
|
$this->variableParser = $variableParser; |
64
|
|
|
|
65
|
3 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function create($value): AbstractVariable |
69
|
|
|
{ |
70
|
|
|
foreach ($this->getRules() as $rule) { |
71
|
|
|
try { |
72
|
|
|
$variable = $rule($value); |
73
|
|
|
} catch (TypeError $e) { |
|
|
|
|
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($variable instanceof AbstractVariable) { |
78
|
|
|
return $variable; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return DefaultVariable::make($value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
private function setJsonRule(): DynamicFactory |
|
|
|
|
86
|
|
|
{ |
87
|
3 |
|
return $this->setRule(JsonVariable::class, function (string $value) { |
88
|
|
|
if (pathinfo($value, PATHINFO_EXTENSION) !== 'json') { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return JsonVariable::make($value); |
93
|
3 |
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function setYamlRule(): void |
97
|
|
|
{ |
98
|
3 |
|
$this->setRule(YamlVariable::class, function (string $value) { |
99
|
|
|
if (! $this->yamlParser) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$extension = pathinfo($value, PATHINFO_EXTENSION); |
104
|
|
|
|
105
|
|
|
if (! \in_array($extension, ['yaml', 'yml'])) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return YamlVariable::make($value, $this->yamlParser, $this->variableParser); |
110
|
3 |
|
}); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
|
|
private function setMarkdownRule(): void |
114
|
|
|
{ |
115
|
3 |
|
$this->setRule(MarkdownVariable::class, function (string $value) { |
116
|
|
|
if (! $this->markdownParser) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (pathinfo($value, PATHINFO_EXTENSION) !== 'md') { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return MarkdownVariable::make($value, $this->markdownParser); |
125
|
3 |
|
}); |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
private function setHtmlRule(): void |
|
|
|
|
129
|
|
|
{ |
130
|
3 |
|
$this->setRule(HtmlVariable::class, function (string $value) { |
131
|
|
|
if (pathinfo($value, PATHINFO_EXTENSION) !== 'html') { |
132
|
|
|
return null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new HtmlVariable($value); |
136
|
3 |
|
}); |
137
|
3 |
|
} |
138
|
|
|
|
139
|
|
|
private function setImageRule(): void |
140
|
|
|
{ |
141
|
3 |
|
$this->setRule(ImageVariable::class, function ($value) { |
142
|
|
|
if (! $this->imageParser) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$srcPath = \is_array($value) ? $value['src'] ?? null : $value; |
147
|
|
|
|
148
|
|
|
$extension = pathinfo($srcPath, PATHINFO_EXTENSION); |
149
|
|
|
|
150
|
|
|
if (! \in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) { |
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return ImageVariable::make($value, $this->imageParser); |
155
|
3 |
|
}); |
156
|
3 |
|
} |
157
|
|
|
|
158
|
|
|
private function setDirectoryRule(): void |
159
|
|
|
{ |
160
|
3 |
|
$this->setRule(DirectoryVariable::class, function ($value) { |
161
|
|
|
if (!is_string($value) || substr($value, -1) !== '/') { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (strpos($value, 'http') !== null) { |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return new DirectoryVariable($value, $this->variableParser); |
170
|
3 |
|
}); |
171
|
3 |
|
} |
172
|
|
|
} |
173
|
|
|
|