|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2018 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Configurator\Collections; |
|
9
|
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use s9e\TextFormatter\Configurator\Helpers\RegexpParser; |
|
12
|
|
|
use s9e\TextFormatter\Configurator\Items\AttributePreprocessor; |
|
13
|
|
|
use s9e\TextFormatter\Configurator\Items\Regexp; |
|
14
|
|
|
use s9e\TextFormatter\Configurator\JavaScript\RegexpConvertor; |
|
15
|
|
|
use s9e\TextFormatter\Configurator\Validators\AttributeName; |
|
16
|
|
|
|
|
17
|
|
|
class AttributePreprocessorCollection extends Collection |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Add an attribute preprocessor |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $attrName Original name |
|
23
|
|
|
* @param string $regexp Preprocessor's regexp |
|
24
|
|
|
* @return AttributePreprocessor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function add($attrName, $regexp) |
|
27
|
|
|
{ |
|
28
|
|
|
$attrName = AttributeName::normalize($attrName); |
|
29
|
|
|
|
|
30
|
|
|
$k = serialize([$attrName, $regexp]); |
|
31
|
|
|
$this->items[$k] = new AttributePreprocessor($regexp); |
|
32
|
|
|
|
|
33
|
|
|
return $this->items[$k]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string Name of the attribute the attribute processor uses as source |
|
38
|
|
|
*/ |
|
39
|
|
|
public function key() |
|
40
|
|
|
{ |
|
41
|
|
|
list($attrName) = unserialize(key($this->items)); |
|
42
|
|
|
|
|
43
|
|
|
return $attrName; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Merge a set of attribute preprocessors into this collection |
|
48
|
|
|
* |
|
49
|
|
|
* @param array|AttributePreprocessorCollection $attributePreprocessors Instance of AttributePreprocessorCollection or 2D array of [[attrName,regexp|AttributePreprocessor]] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function merge($attributePreprocessors) |
|
52
|
|
|
{ |
|
53
|
|
|
$error = false; |
|
54
|
|
|
|
|
55
|
|
|
if ($attributePreprocessors instanceof AttributePreprocessorCollection) |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($attributePreprocessors as $attrName => $attributePreprocessor) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->add($attrName, $attributePreprocessor->getRegexp()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
elseif (is_array($attributePreprocessors)) |
|
63
|
|
|
{ |
|
64
|
|
|
// This should be a list where each element is a [attrName,regexp] pair, or |
|
65
|
|
|
// [attrName,AttributePreprocessor] |
|
66
|
|
|
foreach ($attributePreprocessors as $values) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!is_array($values)) |
|
69
|
|
|
{ |
|
70
|
|
|
$error = true; |
|
71
|
|
|
break; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
list($attrName, $value) = $values; |
|
75
|
|
|
|
|
76
|
|
|
if ($value instanceof AttributePreprocessor) |
|
77
|
|
|
{ |
|
78
|
|
|
$value = $value->getRegexp(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->add($attrName, $value); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
else |
|
85
|
|
|
{ |
|
86
|
|
|
$error = true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($error) |
|
90
|
|
|
{ |
|
91
|
|
|
throw new InvalidArgumentException('merge() expects an instance of AttributePreprocessorCollection or a 2D array where each element is a [attribute name, regexp] pair'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
public function asConfig() |
|
99
|
|
|
{ |
|
100
|
|
|
$config = []; |
|
101
|
|
|
|
|
102
|
|
|
foreach ($this->items as $k => $ap) |
|
103
|
|
|
{ |
|
104
|
|
|
list($attrName) = unserialize($k); |
|
105
|
|
|
$config[] = [$attrName, $ap, $ap->getCaptureNames()]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $config; |
|
109
|
|
|
} |
|
110
|
|
|
} |