-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeConditionStrategy.php
More file actions
68 lines (59 loc) · 2.25 KB
/
AttributeConditionStrategy.php
File metadata and controls
68 lines (59 loc) · 2.25 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
<?php
/**
* Class AttributeConditionStrategy
*
* Used when the element in the condition is in the format: .attribute
*/
class AttributeConditionStrategy extends BaseConditionStrategy
{
public function meetsCondition(SimpleXMLElement $element)
{
$name = $element->getName();
//no condition
if ($this->query->getConditionLeft() === null) {
if ($name === $this->query->getSelectElement()->getValue()) {
return true;
}
} else {
//the actual element is the select element
if ($name === $this->query->getSelectElement()->getValue()) {
if ($this->hasAttribute($element) && $this->query->evaluateQuery($this->getAttributeValue($element))) {
$this->selectedElements[] = $element;
return true;
}
return false;
//find subelement of the element which meets condition
} else {
$thisStrategy = $this;
$decisionMaker = function (SimpleXMLElement $rootElement, $attributes) use ($thisStrategy) {
return $thisStrategy->meetsCondition($rootElement);
};
//did we found the element we are searching for?
if ($name === $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);
}
}
}
return false;
}
/**
* @param SimpleXMLElement $element
* @return bool
*/
protected function hasAttribute(SimpleXMLElement $element)
{
return ElementUtils::hasAttribute($element, $this->query->getConditionLeft()->getValue());
}
/**
* @param SimpleXMLElement $element
* @return mixed
*/
protected function getAttributeValue(SimpleXMLElement $element)
{
//todo: this may return the SimpleXMLElement instance!!!
return ElementUtils::getAttributeValue($element, $this->query->getConditionLeft()->getValue());
}
}