Debian Codegraph @

vendor/mck89/peast/lib/Peast/Selector/Node/Selector.php

raw
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;
11
12 use Peast\Selector\Matches;
13
14 /**
15 * Selector class
16 *
17 * @author Marco Marchiò <[email protected]>
18 */
19 class Selector
20 {
21 /**
22 * Selector groups
23 *
24 * @var Group[]
25 */
26 protected $groups = array();
27
28 /**
29 * Adds a new group
30 *
31 * @param Group $group Group
32 *
33 * @return $this
34 */
35 public function addGroup(Group $group)
36 {
37 $this->groups[] = $group;
38 return $this;
39 }
40
41 /**
42 * Executes the current selector on the given matches
43 *
44 * @param Matches $matches Matches
45 *
46 * @return Matches
47 */
48 public function exec(Matches $matches)
49 {
50 $retMatches = array();
51 foreach ($this->groups as $group) {
52 $clonedMatches = $matches->createClone();
53 $group->exec($clonedMatches);
54 $retMatches[] = $clonedMatches;
55 }
56 if (count($retMatches) > 1) {
57 $retMatches[0]->merge(array_slice($retMatches, 1));
58 }
59 return $retMatches[0];
60 }
61 }