| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast\Selector\Node\Part; |
| 11 |
|
| 12 |
use Peast\Syntax\Node\Node; |
| 13 |
use Peast\Syntax\Utils; |
| 14 |
|
| 15 |
/** |
| 16 |
* Selector part attribute class |
| 17 |
* |
| 18 |
* @author Marco Marchiò <[email protected]> |
| 19 |
*/ |
| 20 |
class Attribute extends Part |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Priority |
| 24 |
* |
| 25 |
* @var int |
| 26 |
*/ |
| 27 |
protected $priority = 4; |
| 28 |
|
| 29 |
/** |
| 30 |
* Attribute names |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $names = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Attribute match operator |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $operator = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* Attribute value |
| 45 |
* |
| 46 |
* @var mixed |
| 47 |
*/ |
| 48 |
protected $value = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Case insensitive flag |
| 52 |
* |
| 53 |
* @var bool |
| 54 |
*/ |
| 55 |
protected $caseInsensitive = false; |
| 56 |
|
| 57 |
/** |
| 58 |
* Regex flag |
| 59 |
* |
| 60 |
* @var bool |
| 61 |
*/ |
| 62 |
protected $regex = false; |
| 63 |
|
| 64 |
/** |
| 65 |
* Adds a name |
| 66 |
* |
| 67 |
* @param string $name Name |
| 68 |
* |
| 69 |
* @return $this |
| 70 |
*/ |
| 71 |
public function addName($name) |
| 72 |
{ |
| 73 |
$this->names[] = $name; |
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sets the operator |
| 79 |
* |
| 80 |
* @param string $operator Operator |
| 81 |
* |
| 82 |
* @return $this |
| 83 |
*/ |
| 84 |
public function setOperator($operator) |
| 85 |
{ |
| 86 |
$this->operator = $operator; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the value |
| 92 |
* |
| 93 |
* @param mixed $value Value |
| 94 |
* |
| 95 |
* @return $this |
| 96 |
*/ |
| 97 |
public function setValue($value) |
| 98 |
{ |
| 99 |
$this->value = $value; |
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Sets the case insensitive flag |
| 105 |
* |
| 106 |
* @param bool $caseInsensitive Case insensitive flag |
| 107 |
* |
| 108 |
* @return $this |
| 109 |
*/ |
| 110 |
public function setCaseInsensitive($caseInsensitive) |
| 111 |
{ |
| 112 |
$this->caseInsensitive = $caseInsensitive; |
| 113 |
return $this; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Sets the regex flag |
| 118 |
* |
| 119 |
* @param bool $regex Regex flag |
| 120 |
* |
| 121 |
* @return $this |
| 122 |
*/ |
| 123 |
public function setRegex($regex) |
| 124 |
{ |
| 125 |
$this->regex = $regex; |
| 126 |
return $this; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns true if the selector part matches the given node, |
| 131 |
* false otherwise |
| 132 |
* |
| 133 |
* @param Node $node Node |
| 134 |
* @param Node $parent Parent node |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function check(Node $node, $parent = null) |
| 139 |
{ |
| 140 |
$attr = $node; |
| 141 |
foreach ($this->names as $name) { |
| 142 |
$attrFound = false; |
| 143 |
if ($attr instanceof Node) { |
| 144 |
$props = Utils::getNodeProperties($attr); |
| 145 |
foreach ($props as $prop) { |
| 146 |
if ($prop["name"] === $name) { |
| 147 |
$attrFound = true; |
| 148 |
$attr = $attr->{$prop["getter"]}(); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
if (!$attrFound) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
$bothStrings = is_string($attr) && is_string($this->value); |
| 158 |
switch ($this->operator) { |
| 159 |
case "=": |
| 160 |
if ($bothStrings) { |
| 161 |
if ($this->regex) { |
| 162 |
return preg_match($this->value, $attr); |
| 163 |
} |
| 164 |
return $this->compareStr( |
| 165 |
$this->value, $attr, $this->caseInsensitive, true, true |
| 166 |
); |
| 167 |
} |
| 168 |
if (is_int($attr) && is_float($this->value)) { |
| 169 |
return (float) $attr === $this->value; |
| 170 |
} |
| 171 |
return $attr === $this->value; |
| 172 |
case "<": |
| 173 |
if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
return $attr < $this->value; |
| 177 |
case ">": |
| 178 |
if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
return $attr > $this->value; |
| 182 |
case "<=": |
| 183 |
if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
return $attr <= $this->value; |
| 187 |
case ">=": |
| 188 |
if (is_float($this->value) && !is_float($attr) && !is_int($attr) && !is_string($attr)) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
return $attr >= $this->value; |
| 192 |
case "^=": |
| 193 |
case "$=": |
| 194 |
case "*=": |
| 195 |
return $this->compareStr( |
| 196 |
$this->value, $attr, $this->caseInsensitive, |
| 197 |
$this->operator === "^=", |
| 198 |
$this->operator === "$=" |
| 199 |
); |
| 200 |
default: |
| 201 |
return true; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Compares two strings |
| 207 |
* |
| 208 |
* @param string $v1 Search value |
| 209 |
* @param string $v2 Compare value |
| 210 |
* @param bool $caseInsensitive True if the search must be case insensitive |
| 211 |
* @param bool $matchStart True if the search must be executed from the |
| 212 |
* beginning of the string |
| 213 |
* @param bool $matchEnd True if the search must be executed from the |
| 214 |
* end of the string |
| 215 |
* |
| 216 |
* @return bool |
| 217 |
*/ |
| 218 |
protected function compareStr($v1, $v2, $caseInsensitive, $matchStart, $matchEnd) |
| 219 |
{ |
| 220 |
$regex = "#" . |
| 221 |
($matchStart ? "^" : "") . |
| 222 |
preg_quote($v1) . |
| 223 |
($matchEnd ? "$" : "") . |
| 224 |
"#u" . |
| 225 |
($caseInsensitive ? "i" : ""); |
| 226 |
return (bool) preg_match($regex, $v2); |
| 227 |
} |
| 228 |
} |