Match::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
 * Represents a match in a collection of matches obtained of checking a string against
22
 * a regular expression.
23
 *
24
 * @author mcustiel
25
 */
26
class Match
27
{
28
    /**
29
     *
30
     * @var array
31
     */
32
    protected $element;
33
34
    /**
35
     * Class constructor.
36
     *
37
     * @param array $responseElement
38
     */
39 14
    public function __construct(array $responseElement)
40
    {
41 14
        $this->element = $responseElement;
42 14
    }
43
44
    /**
45
     * Returns the full match string that matches the whole pattern. Null if no match found.
46
     *
47
     * @return null|string
48
     */
49 9
    public function getFullMatch()
50
    {
51 9
        return empty($this->element) ? null : $this->element[0][0];
52
    }
53
54
    /**
55
     * Returns the offset of the string that matches the whole pattern in the subject
56
     * string, null if no match found.
57
     *
58
     * @return null|int
59
     */
60 1
    public function getOffset()
61
    {
62 1
        return empty($this->element) ? null : $this->element[0][1];
63
    }
64
65
    /**
66
     * Returns the string matching a subpattern of a pattern, giving it's index.
67
     *
68
     * @param int $index
69
     *
70
     * @throws \OutOfBoundsException
71
     * @return string
72
     */
73 8
    public function getSubMatchAt($index)
74
    {
75 8
        $this->validateIndex($index);
76
77 6
        return $this->element[$index][0];
78
    }
79
80
    /**
81
     * Returns the string matching a subpattern of a pattern, giving it's index. If there
82
     * is no submatch at that index, return the default value.
83
     *
84
     * @param int $index
85
     *
86
     * @return string|mixed
87
     */
88 1
    public function getSubMatchOrDefaultAt($index, $default = null)
89
    {
90 1
        return isset($this->element[$index]) ? $this->element[$index][0] : $default;
91
    }
92
93
    /**
94
     * Returns the offset of the string matching a subpattern of a pattern, giving it's index.
95
     *
96
     * @param int $index
97
     *
98
     * @throws \OutOfBoundsException
99
     * @return int
100
     */
101 6
    public function getSubmatchOffsetAt($index)
102
    {
103 6
        $this->validateIndex($index);
104 6
        return $this->element[$index][1];
105
    }
106
107
    /**
108
     * Validates the index of a subpattern.
109
     *
110
     * @param int $index
111
     * @throws \OutOfBoundsException
112
     */
113 9
    private function validateIndex($index)
114
    {
115 9
        if (! isset($this->element[$index]) || $index == 0) {
116 2
            throw new \OutOfBoundsException(
117 2
                'Trying to access invalid submatch index ' . $index . 'in match: ' . $this->getFullMatch()
118
            );
119
        }
120 7
    }
121
}
122