1: <?php
2: // Parco
3: // Copyright (c) 2015 Niels Sonnich Poulsen (http://nielssp.dk)
4: // Licensed under the MIT license.
5: // See the LICENSE file or http://opensource.org/licenses/MIT for more information.
6: namespace Parco;
7:
8: /**
9: * A successful regular expression match.
10: */
11: class Match extends Success
12: {
13:
14: /**
15: * @var array
16: */
17: private $matches;
18:
19: /**
20: * Construct regular expression result.
21: *
22: * @param array $matches
23: * Matches as returned by {@see preg_matches}.
24: * @param int[] $pos
25: * Result position.
26: * @param array $nextInput
27: * Remaining input.
28: * @param int[] $nextPos
29: * Next position.
30: */
31: public function __construct($matches, array $pos, array $nextInput, array $nextPos)
32: {
33: parent::__construct($matches[0][0], $pos, $nextInput, $nextPos);
34: $this->matches = $matches;
35: }
36:
37: /**
38: * Get the matched string in group $i.
39: *
40: * @param int $i
41: * Group number. 0 is the string that matched the full
42: * pattern, i.e. `get() = group(0)`.
43: * @return string|null The matched string or null if empty group.
44: */
45: public function group($i)
46: {
47: if (! isset($this->matches[$i])) {
48: return null;
49: }
50: return $this->matches[$i][0];
51: }
52:
53: /**
54: * Get the index of the first matched character in group $i.
55: *
56: * @param int $i
57: * Group number. 0 is the string that matched the full
58: * pattern.
59: * @return int|null The matched offset or null if empty group.
60: */
61: public function offset($i)
62: {
63: if (! isset($this->matches[$i])) {
64: return null;
65: }
66: return $this->matches[$i][1];
67: }
68: }
69: