PhoneNumber.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <?php
  2. namespace libphonenumber;
  3. class PhoneNumber implements \Serializable
  4. {
  5. /**
  6. * The country calling code for this number, as defined by the International Telecommunication Union
  7. * (ITU). For example, this would be 1 for NANPA countries, and 33 for France.
  8. *
  9. * @var int|null
  10. */
  11. protected $countryCode;
  12. /**
  13. * National (significant) Number is defined in International Telecommunication Union (ITU)
  14. * Recommendation E.164. It is a language/country-neutral representation of a phone number at a
  15. * country level. For countries which have the concept of an "area code" or "national destination
  16. * code", this is included in the National (significant) Number. Although the ITU says the maximum
  17. * length should be 15, we have found longer numbers in some countries e.g. Germany.
  18. *
  19. * Note that the National (significant) Number does not contain the National(trunk) prefix.
  20. *
  21. * @var string|null
  22. */
  23. protected $nationalNumber;
  24. /**
  25. * Extension is not standardized in ITU recommendations, except for being defined as a series of
  26. * numbers with a maximum length of 40 digits. It is defined as a string here to accommodate for the
  27. * possible use of a leading zero in the extension (organizations have complete freedom to do so,
  28. * as there is no standard defined). However, only ASCII digits should be stored here.
  29. *
  30. * @var string|null
  31. */
  32. protected $extension;
  33. /**
  34. * In some countries, the national (significant) number starts with one or more "0"s without this
  35. * being a national prefix or trunk code of some kind. For example, the leading zero in the national
  36. * (significant) number of an Italian phone number indicates the number is a fixed-line number.
  37. * There have been plans to migrate fixed-line numbers to start with the digit two since December
  38. * 2000, but it has not happened yet. See http://en.wikipedia.org/wiki/%2B39 for more details.
  39. *
  40. * These fields can be safely ignored (there is no need to set them) for most countries. Some
  41. * limited number of countries behave like Italy - for these cases, if the leading zero(s) of a
  42. * number would be retained even when dialling internationally, set this flag to true, and also
  43. * set the number of leading zeros.
  44. *
  45. * Clients who use the parsing functionality of the i18n phone number libraries
  46. * will have these fields set if necessary automatically.
  47. *
  48. * @var bool|null
  49. */
  50. protected $italianLeadingZero;
  51. /**
  52. * This field is used to store the raw input string containing phone numbers before it was
  53. * canonicalized by the library. For example, it could be used to store alphanumerical numbers
  54. * such as "1-800-GOOG-411".
  55. *
  56. * @var string|null
  57. */
  58. protected $rawInput;
  59. /**
  60. * The source from which the country_code is derived. This is not set in the general parsing method,
  61. * but in the method that parses and keeps raw_input. New fields could be added upon request.
  62. *
  63. * @see CountryCodeSource
  64. *
  65. * This must be one of the CountryCodeSource constants.
  66. *
  67. * @var int|null
  68. */
  69. protected $countryCodeSource = CountryCodeSource::UNSPECIFIED;
  70. /**
  71. * The carrier selection code that is preferred when calling this phone number domestically. This
  72. * also includes codes that need to be dialed in some countries when calling from landlines to
  73. * mobiles or vice versa. For example, in Columbia, a "3" needs to be dialed before the phone number
  74. * itself when calling from a mobile phone to a domestic landline phone and vice versa.
  75. *
  76. * Note this is the "preferred" code, which means other codes may work as well.
  77. *
  78. * @var string|null
  79. */
  80. protected $preferredDomesticCarrierCode;
  81. /**
  82. * Whether this phone number has a number of leading zeros set.
  83. *
  84. * @var bool
  85. */
  86. protected $hasNumberOfLeadingZeros = false;
  87. /**
  88. * The number of leading zeros of this phone number.
  89. *
  90. * @var int
  91. */
  92. protected $numberOfLeadingZeros = 1;
  93. /**
  94. * Clears this phone number.
  95. *
  96. * This effectively resets this phone number to the state of a new instance.
  97. *
  98. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  99. */
  100. public function clear()
  101. {
  102. $this->clearCountryCode();
  103. $this->clearNationalNumber();
  104. $this->clearExtension();
  105. $this->clearItalianLeadingZero();
  106. $this->clearNumberOfLeadingZeros();
  107. $this->clearRawInput();
  108. $this->clearCountryCodeSource();
  109. $this->clearPreferredDomesticCarrierCode();
  110. return $this;
  111. }
  112. /**
  113. * Clears the country code of this phone number.
  114. *
  115. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  116. */
  117. public function clearCountryCode()
  118. {
  119. $this->countryCode = null;
  120. return $this;
  121. }
  122. /**
  123. * Clears the national number of this phone number.
  124. *
  125. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  126. */
  127. public function clearNationalNumber()
  128. {
  129. $this->nationalNumber = null;
  130. return $this;
  131. }
  132. /**
  133. * Clears the extension of this phone number.
  134. *
  135. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  136. */
  137. public function clearExtension()
  138. {
  139. $this->extension = null;
  140. return $this;
  141. }
  142. /**
  143. * Clears the italian leading zero information of this phone number.
  144. *
  145. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  146. */
  147. public function clearItalianLeadingZero()
  148. {
  149. $this->italianLeadingZero = null;
  150. return $this;
  151. }
  152. /**
  153. * Clears the number of leading zeros of this phone number.
  154. *
  155. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  156. */
  157. public function clearNumberOfLeadingZeros()
  158. {
  159. $this->hasNumberOfLeadingZeros = false;
  160. $this->numberOfLeadingZeros = 1;
  161. return $this;
  162. }
  163. /**
  164. * Clears the raw input of this phone number.
  165. *
  166. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  167. */
  168. public function clearRawInput()
  169. {
  170. $this->rawInput = null;
  171. return $this;
  172. }
  173. /**
  174. * Clears the country code source of this phone number.
  175. *
  176. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  177. */
  178. public function clearCountryCodeSource()
  179. {
  180. $this->countryCodeSource = CountryCodeSource::UNSPECIFIED;
  181. return $this;
  182. }
  183. /**
  184. * Clears the preferred domestic carrier code of this phone number.
  185. *
  186. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  187. */
  188. public function clearPreferredDomesticCarrierCode()
  189. {
  190. $this->preferredDomesticCarrierCode = null;
  191. return $this;
  192. }
  193. /**
  194. * Merges the information from another phone number into this phone number.
  195. *
  196. * @param PhoneNumber $other The phone number to copy.
  197. *
  198. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  199. */
  200. public function mergeFrom(PhoneNumber $other)
  201. {
  202. if ($other->hasCountryCode()) {
  203. $this->setCountryCode($other->getCountryCode());
  204. }
  205. if ($other->hasNationalNumber()) {
  206. $this->setNationalNumber($other->getNationalNumber());
  207. }
  208. if ($other->hasExtension()) {
  209. $this->setExtension($other->getExtension());
  210. }
  211. if ($other->hasItalianLeadingZero()) {
  212. $this->setItalianLeadingZero($other->isItalianLeadingZero());
  213. }
  214. if ($other->hasNumberOfLeadingZeros()) {
  215. $this->setNumberOfLeadingZeros($other->getNumberOfLeadingZeros());
  216. }
  217. if ($other->hasRawInput()) {
  218. $this->setRawInput($other->getRawInput());
  219. }
  220. if ($other->hasCountryCodeSource()) {
  221. $this->setCountryCodeSource($other->getCountryCodeSource());
  222. }
  223. if ($other->hasPreferredDomesticCarrierCode()) {
  224. $this->setPreferredDomesticCarrierCode($other->getPreferredDomesticCarrierCode());
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Returns whether this phone number has a country code set.
  230. *
  231. * @return bool True if a country code is set, false otherwise.
  232. */
  233. public function hasCountryCode()
  234. {
  235. return $this->countryCode !== null;
  236. }
  237. /**
  238. * Returns the country code of this phone number.
  239. *
  240. * @return int|null The country code, or null if not set.
  241. */
  242. public function getCountryCode()
  243. {
  244. return $this->countryCode;
  245. }
  246. /**
  247. * Sets the country code of this phone number.
  248. *
  249. * @param int $value The country code.
  250. *
  251. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  252. */
  253. public function setCountryCode($value)
  254. {
  255. $this->countryCode = (int) $value;
  256. return $this;
  257. }
  258. /**
  259. * Returns whether this phone number has a national number set.
  260. *
  261. * @return bool True if a national number is set, false otherwise.
  262. */
  263. public function hasNationalNumber()
  264. {
  265. return $this->nationalNumber !== null;
  266. }
  267. /**
  268. * Returns the national number of this phone number.
  269. *
  270. * @return string|null The national number, or null if not set.
  271. */
  272. public function getNationalNumber()
  273. {
  274. return $this->nationalNumber;
  275. }
  276. /**
  277. * Sets the national number of this phone number.
  278. *
  279. * @param string $value The national number.
  280. *
  281. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  282. */
  283. public function setNationalNumber($value)
  284. {
  285. $this->nationalNumber = (string) $value;
  286. return $this;
  287. }
  288. /**
  289. * Returns whether this phone number has an extension set.
  290. *
  291. * @return bool True if an extension is set, false otherwise.
  292. */
  293. public function hasExtension()
  294. {
  295. return $this->extension !== null;
  296. }
  297. /**
  298. * Returns the extension of this phone number.
  299. *
  300. * @return string|null The extension, or null if not set.
  301. */
  302. public function getExtension()
  303. {
  304. return $this->extension;
  305. }
  306. /**
  307. * Sets the extension of this phone number.
  308. *
  309. * @param string $value The extension.
  310. *
  311. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  312. */
  313. public function setExtension($value)
  314. {
  315. $this->extension = (string) $value;
  316. return $this;
  317. }
  318. /**
  319. * Returns whether this phone number has the italian leading zero information set.
  320. *
  321. * @return bool
  322. */
  323. public function hasItalianLeadingZero()
  324. {
  325. return $this->italianLeadingZero !== null;
  326. }
  327. /**
  328. * Sets whether this phone number uses an italian leading zero.
  329. *
  330. * @param bool $value True to use italian leading zero, false otherwise.
  331. *
  332. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  333. */
  334. public function setItalianLeadingZero($value)
  335. {
  336. $this->italianLeadingZero = (bool) $value;
  337. return $this;
  338. }
  339. /**
  340. * Returns whether this phone number uses an italian leading zero.
  341. *
  342. * @return bool|null True if it uses an italian leading zero, false it it does not, null if not set.
  343. */
  344. public function isItalianLeadingZero()
  345. {
  346. return $this->italianLeadingZero;
  347. }
  348. /**
  349. * Returns whether this phone number has a number of leading zeros set.
  350. *
  351. * @return bool True if a number of leading zeros is set, false otherwise.
  352. */
  353. public function hasNumberOfLeadingZeros()
  354. {
  355. return $this->hasNumberOfLeadingZeros;
  356. }
  357. /**
  358. * Returns the number of leading zeros of this phone number.
  359. *
  360. * @return int The number of leading zeros.
  361. */
  362. public function getNumberOfLeadingZeros()
  363. {
  364. return $this->numberOfLeadingZeros;
  365. }
  366. /**
  367. * Sets the number of leading zeros of this phone number.
  368. *
  369. * @param int $value The number of leading zeros.
  370. *
  371. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  372. */
  373. public function setNumberOfLeadingZeros($value)
  374. {
  375. $this->hasNumberOfLeadingZeros = true;
  376. $this->numberOfLeadingZeros = (int) $value;
  377. return $this;
  378. }
  379. /**
  380. * Returns whether this phone number has a raw input.
  381. *
  382. * @return bool True if a raw input is set, false otherwise.
  383. */
  384. public function hasRawInput()
  385. {
  386. return $this->rawInput !== null;
  387. }
  388. /**
  389. * Returns the raw input of this phone number.
  390. *
  391. * @return string|null The raw input, or null if not set.
  392. */
  393. public function getRawInput()
  394. {
  395. return $this->rawInput;
  396. }
  397. /**
  398. * Sets the raw input of this phone number.
  399. *
  400. * @param string $value The raw input.
  401. *
  402. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  403. */
  404. public function setRawInput($value)
  405. {
  406. $this->rawInput = (string) $value;
  407. return $this;
  408. }
  409. /**
  410. * Returns whether this phone number has a country code source.
  411. *
  412. * @return bool True if a country code source is set, false otherwise.
  413. */
  414. public function hasCountryCodeSource()
  415. {
  416. return $this->countryCodeSource !== CountryCodeSource::UNSPECIFIED;
  417. }
  418. /**
  419. * Returns the country code source of this phone number.
  420. *
  421. * @return int|null A CountryCodeSource constant, or null if not set.
  422. */
  423. public function getCountryCodeSource()
  424. {
  425. return $this->countryCodeSource;
  426. }
  427. /**
  428. * Sets the country code source of this phone number.
  429. *
  430. * @param int $value A CountryCodeSource constant.
  431. *
  432. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  433. */
  434. public function setCountryCodeSource($value)
  435. {
  436. $this->countryCodeSource = (int) $value;
  437. return $this;
  438. }
  439. /**
  440. * Returns whether this phone number has a preferred domestic carrier code.
  441. *
  442. * @return bool True if a preferred domestic carrier code is set, false otherwise.
  443. */
  444. public function hasPreferredDomesticCarrierCode()
  445. {
  446. return $this->preferredDomesticCarrierCode !== null;
  447. }
  448. /**
  449. * Returns the preferred domestic carrier code of this phone number.
  450. *
  451. * @return string|null The preferred domestic carrier code, or null if not set.
  452. */
  453. public function getPreferredDomesticCarrierCode()
  454. {
  455. return $this->preferredDomesticCarrierCode;
  456. }
  457. /**
  458. * Sets the preferred domestic carrier code of this phone number.
  459. *
  460. * @param string $value The preferred domestic carrier code.
  461. *
  462. * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
  463. */
  464. public function setPreferredDomesticCarrierCode($value)
  465. {
  466. $this->preferredDomesticCarrierCode = (string) $value;
  467. return $this;
  468. }
  469. /**
  470. * Returns whether this phone number is equal to another.
  471. *
  472. * @param PhoneNumber $other The phone number to compare.
  473. *
  474. * @return bool True if the phone numbers are equal, false otherwise.
  475. */
  476. public function equals(PhoneNumber $other)
  477. {
  478. if ($this === $other) {
  479. return true;
  480. }
  481. return $this->countryCode === $other->countryCode
  482. && $this->nationalNumber === $other->nationalNumber
  483. && $this->extension === $other->extension
  484. && $this->italianLeadingZero === $other->italianLeadingZero
  485. && $this->numberOfLeadingZeros === $other->numberOfLeadingZeros
  486. && $this->rawInput === $other->rawInput
  487. && $this->countryCodeSource === $other->countryCodeSource
  488. && $this->preferredDomesticCarrierCode === $other->preferredDomesticCarrierCode;
  489. }
  490. /**
  491. * Returns a string representation of this phone number.
  492. * @return string
  493. */
  494. public function __toString()
  495. {
  496. $outputString = '';
  497. $outputString .= 'Country Code: ' . $this->countryCode;
  498. $outputString .= ' National Number: ' . $this->nationalNumber;
  499. if ($this->hasItalianLeadingZero()) {
  500. $outputString .= ' Leading Zero(s): true';
  501. }
  502. if ($this->hasNumberOfLeadingZeros()) {
  503. $outputString .= ' Number of leading zeros: ' . $this->numberOfLeadingZeros;
  504. }
  505. if ($this->hasExtension()) {
  506. $outputString .= ' Extension: ' . $this->extension;
  507. }
  508. if ($this->hasCountryCodeSource()) {
  509. $outputString .= ' Country Code Source: ' . $this->countryCodeSource;
  510. }
  511. if ($this->hasPreferredDomesticCarrierCode()) {
  512. $outputString .= ' Preferred Domestic Carrier Code: ' . $this->preferredDomesticCarrierCode;
  513. }
  514. return $outputString;
  515. }
  516. /**
  517. * @inheritDoc
  518. */
  519. public function serialize()
  520. {
  521. return serialize($this->__serialize());
  522. }
  523. public function __serialize()
  524. {
  525. return array(
  526. $this->countryCode,
  527. $this->nationalNumber,
  528. $this->extension,
  529. $this->italianLeadingZero,
  530. $this->numberOfLeadingZeros,
  531. $this->rawInput,
  532. $this->countryCodeSource,
  533. $this->preferredDomesticCarrierCode
  534. );
  535. }
  536. /**
  537. * @inheritDoc
  538. */
  539. public function unserialize($serialized)
  540. {
  541. $this->__unserialize(unserialize($serialized));
  542. }
  543. public function __unserialize($data)
  544. {
  545. list(
  546. $this->countryCode,
  547. $this->nationalNumber,
  548. $this->extension,
  549. $this->italianLeadingZero,
  550. $this->numberOfLeadingZeros,
  551. $this->rawInput,
  552. $this->countryCodeSource,
  553. $this->preferredDomesticCarrierCode
  554. ) = $data;
  555. if ($this->numberOfLeadingZeros > 1) {
  556. $this->hasNumberOfLeadingZeros = true;
  557. }
  558. }
  559. }