1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rs\XmlFilter\Filter\Behavior; |
6
|
|
|
|
7
|
|
|
use Rs\XmlFilter\Document\Element; |
8
|
|
|
use Rs\XmlFilter\Document\SimpleXmlElement; |
9
|
|
|
use Rs\XmlFilter\Filter\Manager\FilterManager; |
10
|
|
|
|
11
|
|
|
trait FilterContextTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var FilterManager |
15
|
|
|
*/ |
16
|
|
|
protected $manager; |
17
|
|
|
|
18
|
14 |
|
protected function filter(Element $element, string $filter, array $currentOptions, array $filterOptions) |
19
|
|
|
{ |
20
|
14 |
|
return $this->manager->filter($this->getSearchElement($element, $currentOptions), $filter, $filterOptions); |
21
|
|
|
} |
22
|
|
|
|
23
|
36 |
|
protected function preparePath(string $path, $options) : string |
24
|
|
|
{ |
25
|
36 |
|
if ($options['context'] instanceof Element && false !== strpos($path, '=./')) { |
26
|
|
|
//replace the search condition with the value from the context element |
27
|
1 |
|
preg_match('/=(\.\/[^]]+)]/', $path, $matches); |
28
|
1 |
|
$contextPath = $matches[1] ?? null; |
29
|
|
|
|
30
|
1 |
|
if ($contextPath !== null && $contextValue = $options['context']->find($contextPath)) { |
31
|
1 |
|
$contextValue = $contextValue[0] instanceof SimpleXmlElement ? (string) $contextValue[0] : $contextValue[0]->wholeText; |
32
|
|
|
|
33
|
1 |
|
return preg_replace('/=(\.\/[^]]+)]/', '="' . $contextValue . '"]', $path); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
36 |
|
if ($options['context'] instanceof Element) { |
38
|
7 |
|
return $path; |
39
|
|
|
} |
40
|
|
|
|
41
|
36 |
|
return sprintf($path, $options['context']); |
42
|
|
|
} |
43
|
|
|
|
44
|
17 |
|
protected function getSearchElement(Element $element, $options) |
45
|
|
|
{ |
46
|
17 |
|
if (!isset($options['context']) || !$options['context'] instanceof Element) { |
47
|
14 |
|
return $element; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
if (isset($options['path']) && 0 !== strpos($options['path'], '.')) { |
51
|
|
|
return $element; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return $options['context']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|