-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementWithAttributeStrategy.php
More file actions
97 lines (85 loc) · 3.28 KB
/
ElementWithAttributeStrategy.php
File metadata and controls
97 lines (85 loc) · 3.28 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* Class ElementWithAttributeStrategy.
*
* Used when the element in the condition is in the format: element.attribute
*/
class ElementWithAttributeStrategy extends BaseConditionStrategy
{
/**
* @param SimpleXMLElement $element
*
* @return bool
* @throws \InvalidQueryException
*/
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 and it has the wanted property(attribute with the value in this case)
if ($name === $this->query->getSelectElement()->getValue()) {
$elementNameFromCondition = $this->getElementName($this->query->getConditionLeft()->getValue());
if ($elementNameFromCondition === $name
&& ElementUtils::hasAttribute($element, $this->getAttributeName())
&& $this->query->evaluateQuery($this->getAttributeValue($element))
) {
$this->selectedElements[] = $element;
return true;
} else {
//go deeper
$thisStrategy = $this;
$decisionMaker = function (SimpleXMLElement $rootElement, $attributes) use ($thisStrategy) {
return $thisStrategy->meetsCondition($rootElement);
};
//now, look deeper and find the element from the where clause(if present)
$this->lookDeeper($decisionMaker, $element, true);
}
//find subelement of the element which meets condition
} else {
$thisStrategy = $this;
$decisionMaker = function (SimpleXMLElement $rootElement, $attributes) use ($thisStrategy) {
return $thisStrategy->meetsCondition($rootElement);
};
if ($name === $this->getElementName($this->query->getConditionLeft()->getValue())
&& ElementUtils::hasAttribute($element, $this->getAttributeName())
&& $this->query->evaluateQuery($this->getAttributeValue($element))
) {
return true;
}
return $this->lookDeeper($decisionMaker, $element, false);
}
}
return false;
}
/**
* @return string
*/
protected function getAttributeName()
{
$conditionElementName = $this->query->getConditionLeft()->getValue();
return mb_substr($conditionElementName, mb_strpos($conditionElementName, '.') + 1);
}
/**
* @param SimpleXMLElement $element
*
* @return mixed
*/
protected function getAttributeValue(SimpleXMLElement $element)
{
return (string)$element->attributes()[$this->getAttributeName()][0];
}
/**
* @param $name string Element with attribute name, e.g. element.attribute
*
* @return string element
*/
protected function getElementName($name)
{
return mb_substr($name, 0, mb_strpos($name, '.'));
}
}