NumberFormat.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace libphonenumber;
  3. /**
  4. * Number Format
  5. */
  6. class NumberFormat
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $pattern;
  12. /**
  13. * @var bool
  14. */
  15. protected $hasPattern = false;
  16. /**
  17. * @var string
  18. */
  19. protected $format;
  20. /**
  21. * @var bool
  22. */
  23. protected $hasFormat = false;
  24. /**
  25. * @var array
  26. */
  27. protected $leadingDigitsPattern = array();
  28. /**
  29. * @var string
  30. */
  31. protected $nationalPrefixFormattingRule = '';
  32. /**
  33. * @var bool
  34. */
  35. protected $hasNationalPrefixFormattingRule = false;
  36. /**
  37. * @var bool
  38. */
  39. protected $nationalPrefixOptionalWhenFormatting = false;
  40. /**
  41. * @var bool
  42. */
  43. protected $hasNationalPrefixOptionalWhenFormatting = false;
  44. /**
  45. * @var string
  46. */
  47. protected $domesticCarrierCodeFormattingRule = '';
  48. /**
  49. * @var bool
  50. */
  51. protected $hasDomesticCarrierCodeFormattingRule = false;
  52. public function __construct()
  53. {
  54. $this->clear();
  55. }
  56. /**
  57. * @return NumberFormat
  58. */
  59. public function clear()
  60. {
  61. $this->hasPattern = false;
  62. $this->pattern = null;
  63. $this->hasFormat = false;
  64. $this->format = null;
  65. $this->leadingDigitsPattern = array();
  66. $this->hasNationalPrefixFormattingRule = false;
  67. $this->nationalPrefixFormattingRule = '';
  68. $this->hasNationalPrefixOptionalWhenFormatting = false;
  69. $this->nationalPrefixOptionalWhenFormatting = false;
  70. $this->hasDomesticCarrierCodeFormattingRule = false;
  71. $this->domesticCarrierCodeFormattingRule = '';
  72. return $this;
  73. }
  74. /**
  75. * @return boolean
  76. */
  77. public function hasPattern()
  78. {
  79. return $this->hasPattern;
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getPattern()
  85. {
  86. return $this->pattern;
  87. }
  88. /**
  89. * @param string $value
  90. * @return NumberFormat
  91. */
  92. public function setPattern($value)
  93. {
  94. $this->hasPattern = true;
  95. $this->pattern = $value;
  96. return $this;
  97. }
  98. /**
  99. * @return boolean
  100. */
  101. public function hasNationalPrefixOptionalWhenFormatting()
  102. {
  103. return $this->hasNationalPrefixOptionalWhenFormatting;
  104. }
  105. /**
  106. * @return boolean
  107. */
  108. public function getNationalPrefixOptionalWhenFormatting()
  109. {
  110. return $this->nationalPrefixOptionalWhenFormatting;
  111. }
  112. /**
  113. * @param boolean $nationalPrefixOptionalWhenFormatting
  114. */
  115. public function setNationalPrefixOptionalWhenFormatting($nationalPrefixOptionalWhenFormatting)
  116. {
  117. $this->hasNationalPrefixOptionalWhenFormatting = true;
  118. $this->nationalPrefixOptionalWhenFormatting = $nationalPrefixOptionalWhenFormatting;
  119. }
  120. /**
  121. * @return boolean
  122. */
  123. public function hasFormat()
  124. {
  125. return $this->hasFormat;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getFormat()
  131. {
  132. return $this->format;
  133. }
  134. /**
  135. * @param string $value
  136. * @return NumberFormat
  137. */
  138. public function setFormat($value)
  139. {
  140. $this->hasFormat = true;
  141. $this->format = $value;
  142. return $this;
  143. }
  144. /**
  145. * @return string[]
  146. */
  147. public function leadingDigitPatterns()
  148. {
  149. return $this->leadingDigitsPattern;
  150. }
  151. /**
  152. * @return int
  153. */
  154. public function leadingDigitsPatternSize()
  155. {
  156. return count($this->leadingDigitsPattern);
  157. }
  158. /**
  159. * @param int $index
  160. * @return string
  161. */
  162. public function getLeadingDigitsPattern($index)
  163. {
  164. return $this->leadingDigitsPattern[$index];
  165. }
  166. /**
  167. * @param string $value
  168. * @return NumberFormat
  169. */
  170. public function addLeadingDigitsPattern($value)
  171. {
  172. $this->leadingDigitsPattern[] = $value;
  173. return $this;
  174. }
  175. /**
  176. * @return boolean
  177. */
  178. public function hasNationalPrefixFormattingRule()
  179. {
  180. return $this->hasNationalPrefixFormattingRule;
  181. }
  182. /**
  183. * @return string
  184. */
  185. public function getNationalPrefixFormattingRule()
  186. {
  187. return $this->nationalPrefixFormattingRule;
  188. }
  189. /**
  190. * @param string $value
  191. * @return NumberFormat
  192. */
  193. public function setNationalPrefixFormattingRule($value)
  194. {
  195. $this->hasNationalPrefixFormattingRule = true;
  196. $this->nationalPrefixFormattingRule = (string)$value;
  197. return $this;
  198. }
  199. /**
  200. * @return NumberFormat
  201. */
  202. public function clearNationalPrefixFormattingRule()
  203. {
  204. $this->nationalPrefixFormattingRule = '';
  205. return $this;
  206. }
  207. /**
  208. * @return boolean
  209. */
  210. public function hasDomesticCarrierCodeFormattingRule()
  211. {
  212. return $this->hasDomesticCarrierCodeFormattingRule;
  213. }
  214. /**
  215. * @return string
  216. */
  217. public function getDomesticCarrierCodeFormattingRule()
  218. {
  219. return $this->domesticCarrierCodeFormattingRule;
  220. }
  221. /**
  222. * @param string $value
  223. * @return NumberFormat
  224. */
  225. public function setDomesticCarrierCodeFormattingRule($value)
  226. {
  227. $this->hasDomesticCarrierCodeFormattingRule = true;
  228. $this->domesticCarrierCodeFormattingRule = (string)$value;
  229. return $this;
  230. }
  231. /**
  232. * @param NumberFormat $other
  233. * @return NumberFormat
  234. */
  235. public function mergeFrom(NumberFormat $other)
  236. {
  237. if ($other->hasPattern()) {
  238. $this->setPattern($other->getPattern());
  239. }
  240. if ($other->hasFormat()) {
  241. $this->setFormat($other->getFormat());
  242. }
  243. $leadingDigitsPatternSize = $other->leadingDigitsPatternSize();
  244. for ($i = 0; $i < $leadingDigitsPatternSize; $i++) {
  245. $this->addLeadingDigitsPattern($other->getLeadingDigitsPattern($i));
  246. }
  247. if ($other->hasNationalPrefixFormattingRule()) {
  248. $this->setNationalPrefixFormattingRule($other->getNationalPrefixFormattingRule());
  249. }
  250. if ($other->hasDomesticCarrierCodeFormattingRule()) {
  251. $this->setDomesticCarrierCodeFormattingRule($other->getDomesticCarrierCodeFormattingRule());
  252. }
  253. if ($other->hasNationalPrefixOptionalWhenFormatting()) {
  254. $this->setNationalPrefixOptionalWhenFormatting($other->getNationalPrefixOptionalWhenFormatting());
  255. }
  256. return $this;
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function toArray()
  262. {
  263. $output = array();
  264. $output['pattern'] = $this->getPattern();
  265. $output['format'] = $this->getFormat();
  266. $output['leadingDigitsPatterns'] = $this->leadingDigitPatterns();
  267. if ($this->hasNationalPrefixFormattingRule()) {
  268. $output['nationalPrefixFormattingRule'] = $this->getNationalPrefixFormattingRule();
  269. }
  270. if ($this->hasDomesticCarrierCodeFormattingRule()) {
  271. $output['domesticCarrierCodeFormattingRule'] = $this->getDomesticCarrierCodeFormattingRule();
  272. }
  273. if ($this->hasNationalPrefixOptionalWhenFormatting()) {
  274. $output['nationalPrefixOptionalWhenFormatting'] = $this->getNationalPrefixOptionalWhenFormatting();
  275. }
  276. return $output;
  277. }
  278. /**
  279. * @param array $input
  280. */
  281. public function fromArray(array $input)
  282. {
  283. $this->setPattern($input['pattern']);
  284. $this->setFormat($input['format']);
  285. foreach ($input['leadingDigitsPatterns'] as $leadingDigitsPattern) {
  286. $this->addLeadingDigitsPattern($leadingDigitsPattern);
  287. }
  288. if (isset($input['nationalPrefixFormattingRule']) && $input['nationalPrefixFormattingRule'] !== '') {
  289. $this->setNationalPrefixFormattingRule($input['nationalPrefixFormattingRule']);
  290. }
  291. if (isset($input['domesticCarrierCodeFormattingRule']) && $input['domesticCarrierCodeFormattingRule'] !== '') {
  292. $this->setDomesticCarrierCodeFormattingRule($input['domesticCarrierCodeFormattingRule']);
  293. }
  294. if (isset($input['nationalPrefixOptionalWhenFormatting'])) {
  295. $this->setNationalPrefixOptionalWhenFormatting($input['nationalPrefixOptionalWhenFormatting']);
  296. }
  297. }
  298. }