|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of php-simple-regex. |
|
4
|
|
|
* |
|
5
|
|
|
* php-simple-regex is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* php-simple-regex is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with php-simple-regex. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace Mcustiel\PhpSimpleRegex; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Represent the result of a search through a string using a regular expression. |
|
22
|
|
|
* This |
|
23
|
|
|
* is an iterable collection of matches. |
|
24
|
|
|
* |
|
25
|
|
|
* @author mcustiel |
|
26
|
|
|
*/ |
|
27
|
|
|
class MatchResult implements \Iterator |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $response; |
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
*/ |
|
38
|
|
|
private $current; |
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
private $count; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Class contructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $regexResponse |
|
49
|
|
|
*/ |
|
50
|
9 |
|
public function __construct(array $regexResponse) |
|
51
|
|
|
{ |
|
52
|
9 |
|
$this->response = $regexResponse; |
|
53
|
9 |
|
$this->count = count($this->response); |
|
54
|
9 |
|
$this->current = 0; |
|
55
|
9 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the number of matches found in the subject string for the executed regex. |
|
59
|
|
|
* |
|
60
|
|
|
* @return int |
|
61
|
|
|
*/ |
|
62
|
6 |
|
public function getMatchesCount() |
|
63
|
|
|
{ |
|
64
|
6 |
|
return $this->count; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns a match for the executed pattern using it's order in the string. |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $index |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \OutOfBoundsException |
|
73
|
|
|
* @return \Mcustiel\PhpSimpleRegex\Match |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public function getMatchAt($index) |
|
76
|
|
|
{ |
|
77
|
7 |
|
if (! isset($this->response[$index])) { |
|
78
|
1 |
|
throw new \OutOfBoundsException('Trying to access a match at an invalid index'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
return new Match($this->response[$index]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// \Iterable implementation |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Mcustiel\PhpSimpleRegex\Match |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function current() |
|
91
|
|
|
{ |
|
92
|
1 |
|
return new Match($this->response[$this->current]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function next() |
|
96
|
|
|
{ |
|
97
|
1 |
|
++ $this->current; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function key() |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->current; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function valid() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->current < $this->count; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function rewind() |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->current = 0; |
|
113
|
1 |
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|