PhoneNumberMatch.php 1.9 KB

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