phone.pl.inc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Polish phone numbers.
  5. */
  6. /**
  7. * Polish phone number regex helper.
  8. */
  9. function _pl_regex() {
  10. return '/^
  11. ([1-9]\d) # 2-digit area code
  12. ([1-9]\d{2}) # 3-digit phone prefix (cannot start with 0)
  13. (\d{4})
  14. $/x';
  15. }
  16. /**
  17. * Verifies that $number is a valid 9-digit Polish phone number.
  18. */
  19. function pl_validate_number($number, $ext = '', &$error) {
  20. $number = cck_phone_clean_number($number);
  21. $regex = _pl_regex();
  22. $result = preg_match($regex, $number, $matches);
  23. if ($result) {
  24. return TRUE;
  25. }
  26. else {
  27. $error = '"%phone_input" is not a valid Polish phone number, it should be a 9-digit
  28. number like "99 999 99 99".';
  29. return FALSE;
  30. }
  31. }
  32. /**
  33. * Helper for formatting output.
  34. * Output as +48 (AA) BBB CCCC
  35. */
  36. function _pl_formatter($element) {
  37. $phone = '';
  38. $regex = _pl_regex();
  39. $result = preg_match($regex, $element['number'], $matches);
  40. if ($result) {
  41. $phone = '(' . $matches[1] . ') ' . $matches[2] . ' ' . $matches[3];
  42. }
  43. return $phone;
  44. }
  45. /**
  46. * Default formatter for international phone number.
  47. */
  48. function pl_formatter_default($element) {
  49. $cc = cck_phone_countrycodes($element['country_codes']);
  50. $number = _pl_formatter($element);
  51. return $cc['code'] . ' ' . $number;
  52. }
  53. /**
  54. * Local formatter for local phone number.
  55. */
  56. function pl_formatter_local($element) {
  57. return _pl_formatter($element);
  58. }