| 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; |
| 11 |
|
| 12 |
use Peast\Syntax\Node\Node; |
| 13 |
|
| 14 |
/** |
| 15 |
* Selector matches class |
| 16 |
* |
| 17 |
* @author Marco Marchiò <[email protected]> |
| 18 |
*/ |
| 19 |
class Matches |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Matches array |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $matches; |
| 27 |
|
| 28 |
/** |
| 29 |
* Class constructor |
| 30 |
* |
| 31 |
* @param array $matches Matches |
| 32 |
*/ |
| 33 |
public function __construct($matches = array()) |
| 34 |
{ |
| 35 |
$this->matches = $matches; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Adds a new match |
| 40 |
* |
| 41 |
* @param Node $node |
| 42 |
* @param Node|null $parent |
| 43 |
*/ |
| 44 |
public function addMatch(Node $node, $parent = null) |
| 45 |
{ |
| 46 |
$this->matches[] = array($node, $parent); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the matches |
| 51 |
* |
| 52 |
* @returns array |
| 53 |
*/ |
| 54 |
public function getMatches() |
| 55 |
{ |
| 56 |
return $this->matches; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the matched nodes |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function getNodes() { |
| 65 |
return array_map(function ($m) { |
| 66 |
return $m[0]; |
| 67 |
}, $this->matches); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Filters the matches using the given function, if it returns |
| 72 |
* a false value the match will be removed. The function will |
| 73 |
* receive the node and its parent as arguments. |
| 74 |
* |
| 75 |
* @param callable $fn Filter function |
| 76 |
* @return $this |
| 77 |
*/ |
| 78 |
public function filter(callable $fn) |
| 79 |
{ |
| 80 |
$newMatches = array(); |
| 81 |
foreach ($this->matches as $match) { |
| 82 |
if ($fn($match[0], $match[1])) { |
| 83 |
$newMatches[] = $match; |
| 84 |
} |
| 85 |
} |
| 86 |
$this->matches = $newMatches; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Replaces all the matches with the result of the given function. |
| 92 |
* The function will receive the node and its parent as arguments |
| 93 |
* and must return an array of matches |
| 94 |
* |
| 95 |
* @param callable $fn Map function |
| 96 |
* |
| 97 |
* @return $this |
| 98 |
*/ |
| 99 |
public function map(callable $fn) |
| 100 |
{ |
| 101 |
$newMatches = array(); |
| 102 |
foreach ($this->matches as $match) { |
| 103 |
$res = $fn($match[0], $match[1]); |
| 104 |
if ($res) { |
| 105 |
$newMatches = array_merge($newMatches, $res); |
| 106 |
} |
| 107 |
} |
| 108 |
$this->matches = $newMatches; |
| 109 |
return $this->unique(); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Merges the current object with the other given Matches objects |
| 114 |
* |
| 115 |
* @param Matches[] $matchesArr Array of Matches to merge |
| 116 |
* |
| 117 |
* @return $this |
| 118 |
*/ |
| 119 |
public function merge($matchesArr) |
| 120 |
{ |
| 121 |
foreach ($matchesArr as $matches) { |
| 122 |
foreach ($matches->getMatches() as $match) { |
| 123 |
$this->addMatch($match[0], $match[1]); |
| 124 |
} |
| 125 |
} |
| 126 |
return $this->unique(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Remove all duplicated matches |
| 131 |
* |
| 132 |
* @return $this |
| 133 |
*/ |
| 134 |
public function unique() |
| 135 |
{ |
| 136 |
$newMatches = array(); |
| 137 |
$newNodes = array(); |
| 138 |
foreach ($this->matches as $match) { |
| 139 |
if (!in_array($match[0], $newNodes, true)) { |
| 140 |
$newMatches[] = $match; |
| 141 |
$newNodes[] = $match[0]; |
| 142 |
} |
| 143 |
} |
| 144 |
$this->matches = $newMatches; |
| 145 |
return $this; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns a clone of the current object |
| 150 |
* |
| 151 |
* @return Matches |
| 152 |
*/ |
| 153 |
public function createClone() |
| 154 |
{ |
| 155 |
return new self($this->matches); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the number of matches |
| 160 |
* |
| 161 |
* @return int |
| 162 |
*/ |
| 163 |
public function count() |
| 164 |
{ |
| 165 |
return count($this->matches); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Returns the match at the given index |
| 170 |
* |
| 171 |
* @param int $index Index |
| 172 |
* |
| 173 |
* @return array |
| 174 |
* |
| 175 |
* @throws \Exception |
| 176 |
*/ |
| 177 |
public function get($index) |
| 178 |
{ |
| 179 |
$index = (int) $index; |
| 180 |
if (!isset($this->matches[$index])) { |
| 181 |
throw new \Exception("Invalid index $index"); |
| 182 |
} |
| 183 |
return $this->matches[$index]; |
| 184 |
} |
| 185 |
} |