-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementConditionStrategy.php
More file actions
75 lines (66 loc) · 2.34 KB
/
ElementConditionStrategy.php
File metadata and controls
75 lines (66 loc) · 2.34 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Class ElementConditionStrategy
*
* Used when the element in the condition is in the format: element
*/
class ElementConditionStrategy extends BaseConditionStrategy
{
/**
* Check if the element meets the condition from the query.
*
* @param SimpleXMLElement $element
*
* @return bool
*
* @throws InvalidInputFileFormatException
*/
public function meetsCondition(SimpleXMLElement $element)
{
$name = $element->getName();
//no condition
if ($this->query->getConditionLeft() === null) {
if ($name === $this->query->getSelectElement()->getValue()) {
$this->selectedElements[] = $element;
return true;
} else {
return $this->goDeeper($element);
}
} else {
//the select element is in the condition
if ($name === $this->query->getConditionLeft()->getValue()) {
// the element in condition can not have any subelement!
if (count($element->children()) > 0) {
throw new InvalidInputFileFormatException(
"The element $name contains other elements! Thus it cannot be used in the condition."
);
} else {
$value = (string)$element;
}
return $this->query->evaluateQuery($value); //perform query evaluation
//find subelement of the element which meets condition
} else {
return $this->goDeeper($element);
}
}
}
/**
* @param SimpleXMLElement $element
*
* @return bool
*/
protected function goDeeper(SimpleXMLElement $element)
{
$thisStrategy = $this;
$decisionMaker = function (SimpleXMLElement $rootElement, $attributes) use ($thisStrategy) {
return $thisStrategy->meetsCondition($rootElement);
};
//did we found the element we are searching for?
if ($element->getName() === $this->query->getSelectElement()->getValue()) {
//now, look deeper and find the element from the where clause(if present)
return $this->lookDeeper($decisionMaker, $element, true);
} else {
return $this->lookDeeper($decisionMaker, $element, false);
}
}
}