PhoneNumber.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the overtrue/easy-sms.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\EasySms;
  11. /**
  12. * Class PhoneNumberInterface.
  13. *
  14. * @author overtrue <i@overtrue.me>
  15. */
  16. class PhoneNumber implements \Overtrue\EasySms\Contracts\PhoneNumberInterface
  17. {
  18. /**
  19. * @var int
  20. */
  21. protected $number;
  22. /**
  23. * @var int
  24. */
  25. protected $IDDCode;
  26. /**
  27. * PhoneNumberInterface constructor.
  28. *
  29. * @param int $numberWithoutIDDCode
  30. * @param string $IDDCode
  31. */
  32. public function __construct($numberWithoutIDDCode, $IDDCode = null)
  33. {
  34. $this->number = $numberWithoutIDDCode;
  35. $this->IDDCode = $IDDCode ? intval(ltrim($IDDCode, '+0')) : null;
  36. }
  37. /**
  38. * 86.
  39. *
  40. * @return int
  41. */
  42. public function getIDDCode()
  43. {
  44. return $this->IDDCode;
  45. }
  46. /**
  47. * 18888888888.
  48. *
  49. * @return int
  50. */
  51. public function getNumber()
  52. {
  53. return $this->number;
  54. }
  55. /**
  56. * +8618888888888.
  57. *
  58. * @return string
  59. */
  60. public function getUniversalNumber()
  61. {
  62. return $this->getPrefixedIDDCode('+').$this->number;
  63. }
  64. /**
  65. * 008618888888888.
  66. *
  67. * @return string
  68. */
  69. public function getZeroPrefixedNumber()
  70. {
  71. return $this->getPrefixedIDDCode('00').$this->number;
  72. }
  73. /**
  74. * @param string $prefix
  75. *
  76. * @return string|null
  77. */
  78. public function getPrefixedIDDCode($prefix)
  79. {
  80. return $this->IDDCode ? $prefix.$this->IDDCode : null;
  81. }
  82. /**
  83. * @return string
  84. */
  85. public function __toString()
  86. {
  87. return $this->getUniversalNumber();
  88. }
  89. /**
  90. * Specify data which should be serialized to JSON.
  91. *
  92. * @see http://php.net/manual/en/jsonserializable.jsonserialize.php
  93. *
  94. * @return mixed data which can be serialized by <b>json_encode</b>,
  95. * which is a value of any type other than a resource
  96. *
  97. * @since 5.4.0
  98. */
  99. #[\ReturnTypeWillChange]
  100. public function jsonSerialize()
  101. {
  102. return $this->getUniversalNumber();
  103. }
  104. }