1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
namespace Spiral\Stempler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Exported has to export (obviously) specified blocks into content. Every exporter should |
12
|
|
|
* define it's own pattern to initiate export. |
13
|
|
|
* |
14
|
|
|
* Most of spiral exporters will provide ability to specify list of elements to be included or |
15
|
|
|
* excluded from exporting. In addition you can use custom prefix for some of your elements. |
16
|
|
|
* |
17
|
|
|
* Pattern: include:element,elementB;exclude:elementC,elementD,patten-*;prefix:my-prefix; |
18
|
|
|
* |
19
|
|
|
* Prefix will allow you to match some attributes to specific spot, use exclude pattern (with star) |
20
|
|
|
* to remove attributes like that from other places. |
21
|
|
|
*/ |
22
|
|
|
abstract class ConditionalExporter implements ExporterInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Get blocks matching specified condition. |
26
|
|
|
* |
27
|
|
|
* @param string $condition |
28
|
|
|
* @param array $blocks |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
protected function filterBlocks($condition = null, array $blocks) |
32
|
|
|
{ |
33
|
|
|
if (empty($condition)) { |
34
|
|
|
return $blocks; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$conditions = []; |
38
|
|
|
foreach (explode(';', $condition) as $condition) { |
39
|
|
|
if (strpos($condition, ':') === false) { |
40
|
|
|
//Invalid |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
list($name, $value) = explode(":", $condition); |
45
|
|
|
$conditions[$name] = $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$result = $blocks; |
49
|
|
|
if (isset($conditions['prefix'])) { |
50
|
|
|
$result = []; |
51
|
|
|
foreach ($blocks as $name => $value) { |
52
|
|
|
if (strpos($name, $conditions['prefix']) === 0) { |
53
|
|
|
//Prefix plus "-" sign |
54
|
|
|
$result[substr($name, strlen($conditions['prefix']) + 1)] = $value; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($conditions['include'])) { |
60
|
|
|
$include = explode(',', $conditions['include']); |
61
|
|
|
|
62
|
|
|
foreach ($blocks as $name => $value) { |
63
|
|
|
if (!in_array($name, $include)) { |
64
|
|
|
unset($result[$name]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($conditions['exclude'])) { |
70
|
|
|
$exclude = explode(',', $conditions['exclude']); |
71
|
|
|
|
72
|
|
|
foreach ($blocks as $name => $value) { |
73
|
|
|
if (in_array($name, $exclude)) { |
74
|
|
|
unset($result[$name]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($exclude as $pattern) { |
78
|
|
|
if (strpos($pattern, '*') === false) { |
79
|
|
|
//Not pattern |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$pattern = '/^' . str_replace('*', '.+', $pattern) . '/i'; |
84
|
|
|
|
85
|
|
|
if (preg_match($pattern, $name)) { |
86
|
|
|
unset($result[$name]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
} |