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