/
AbstractRegularExpressionRoutine.php
45 lines (39 loc) · 1.28 KB
/
AbstractRegularExpressionRoutine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Concrete\Core\Backup\ContentImporter\ValueInspector\InspectionRoutine;
abstract class AbstractRegularExpressionRoutine implements RoutineInterface
{
abstract public function getRegularExpression();
abstract public function getItem($identifier);
public function match($content)
{
$items = array();
if (is_scalar($content)) {
if (preg_match_all($this->getRegularExpression(), $content, $matches)) {
if ($matches[1]) {
foreach ($matches[1] as $identifier) {
$items[] = $this->getItem($identifier);
}
}
}
}
return $items;
}
public function replaceContent($content)
{
$routine = $this;
if (is_scalar($content)) {
$content = preg_replace_callback(
$this->getRegularExpression(),
function ($matches) use ($routine) {
if (isset($matches[1])) {
$identifier = $matches[1];
$item = $routine->getItem($identifier);
return $item->getContentValue();
}
},
$content
);
}
return $content;
}
}