| 1 | <?php |
||
| 2 | |||
| 3 | namespace libphonenumber; |
||
| 4 | |||
| 5 | class PhoneNumberMatch |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * The start index into the text. |
||
| 9 | * @var int |
||
| 10 | */ |
||
| 11 | private $start; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The raw substring matched. |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $rawString; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The matched phone number. |
||
| 21 | * @var PhoneNumber |
||
| 22 | */ |
||
| 23 | private $number; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Creates a new match |
||
| 27 | * |
||
| 28 | * @param int $start The start index into the target text |
||
| 29 | * @param string $rawString The matched substring of the target text |
||
| 30 | * @param PhoneNumber $number The matched phone number |
||
| 31 | */ |
||
| 32 | 128 | public function __construct($start, $rawString, PhoneNumber $number) |
|
| 33 | { |
||
| 34 | 128 | if ($start < 0) { |
|
| 35 | 1 | throw new \InvalidArgumentException('Start index must be >= 0.'); |
|
| 36 | } |
||
| 37 | |||
| 38 | 128 | if ($rawString === null) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 39 | 1 | throw new \InvalidArgumentException('$rawString must be a string'); |
|
| 40 | } |
||
| 41 | |||
| 42 | 127 | $this->start = $start; |
|
| 43 | 127 | $this->rawString = $rawString; |
|
| 44 | 127 | $this->number = $number; |
|
| 45 | 127 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Returns the phone number matched by the receiver. |
||
| 49 | * @return PhoneNumber |
||
| 50 | */ |
||
| 51 | 6 | public function number() |
|
| 52 | { |
||
| 53 | 6 | return $this->number; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns the start index of the matched phone number within the searched text. |
||
| 58 | * @return int |
||
| 59 | */ |
||
| 60 | 18 | public function start() |
|
| 61 | { |
||
| 62 | 18 | return $this->start; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns the exclusive end index of the matched phone number within the searched text. |
||
| 67 | * @return int |
||
| 68 | */ |
||
| 69 | 127 | public function end() |
|
| 70 | { |
||
| 71 | 127 | return $this->start + \mb_strlen($this->rawString); |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the raw string matched as a phone number in the searched text. |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 121 | public function rawString() |
|
| 79 | { |
||
| 80 | 121 | return $this->rawString; |
|
| 81 | } |
||
| 82 | |||
| 83 | 16 | public function __toString() |
|
| 84 | { |
||
| 85 | 16 | return "PhoneNumberMatch [{$this->start()},{$this->end()}) {$this->rawString}"; |
|
| 86 | } |
||
| 87 | } |
||
| 88 |