3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Created by PhpStorm. * User: Martin Jirásek <martin.jirasek@nms.cz> * Date: 31.03.2017 * Time: 12:41 */ class PhoneNumberValidator { const PREFIX_SYMBOL_ZEROS = '00'; const PREFIX_SYMBOL_PLUS = '+'; private static $prefixSymbols = [ self::PREFIX_SYMBOL_ZEROS, self::PREFIX_SYMBOL_PLUS, ]; /** * @return array */ public static function internationalPrefixNumbers() { return [ '420', // cz '421', // sk ]; } /** * @param $phoneNumber * @return string */ public static function getPhoneNumberBase($phoneNumber) { return substr($phoneNumber, 0, -9); } /** * @param $phoneNumber * @return array|bool */ public static function getRecognizedCountryPrefixes($phoneNumber) { $prefixes = []; $prefixPart = self::getPhoneNumberBase($phoneNumber); $internationalPrefixNumbers = self::internationalPrefixNumbers(); if (strlen($prefixPart) <= min(array_map('strlen', $internationalPrefixNumbers))) { return false; } foreach ($internationalPrefixNumbers as $prefix) { if (!$prefixPart || strpos($prefixPart, $prefix) !== false) { foreach (self::$prefixSymbols as $prefixType) { $prefixes[] = $prefixType . $prefix; } } } return $prefixes; } /** * @param $phoneNumber * @return array|bool */ public static function getPhoneNumberInAllPossibleForms($phoneNumber) { $prefixes = self::getRecognizedCountryPrefixes($phoneNumber); if ($prefixes === false) { return false; } $forms = []; foreach ($prefixes as $prefix) { $forms[] = $prefix . self::getPhoneNumberBase($phoneNumber); } return $forms; } /** * @param $phoneNumber * @return bool|string */ public static function convertToRightForm($phoneNumber) { $forms = self::getPhoneNumberInAllPossibleForms($phoneNumber); if ($forms === false) { return false; } foreach ($forms as $form) { if (substr($form, 0, strlen(self::PREFIX_SYMBOL_PLUS)) === self::PREFIX_SYMBOL_PLUS) { return $form; } } return false; } /** * @param $phoneNumber * @return bool */ public static function isValid($phoneNumber) { if (!$phoneNumber) { return false; } $recognizedCountryPrefixes = self::getRecognizedCountryPrefixes($phoneNumber); if ($recognizedCountryPrefixes === false) { return false; } $regexPrefixes = array_map(function ($value) { return strpos($value, '+') !== false ? "\\$value" : $value; // escape for regex }, $recognizedCountryPrefixes); return (bool) preg_match('~^(' . implode('|', $regexPrefixes) . ')' /*. '?' */. '[0-9]{9}$~', $phoneNumber); } } var_dump('getPhoneNumberBase'); var_dump(PhoneNumberValidator::getPhoneNumberBase("608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("0608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("421608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("+421608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("00421608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("+420608171262")); var_dump(PhoneNumberValidator::getPhoneNumberBase("00420608171262")); var_dump('isValid'); var_dump(PhoneNumberValidator::isValid("608171262")); var_dump(PhoneNumberValidator::isValid("0608171262")); var_dump(PhoneNumberValidator::isValid("420608171262")); var_dump(PhoneNumberValidator::isValid("+421608171262")); var_dump(PhoneNumberValidator::isValid("00421608171262")); var_dump(PhoneNumberValidator::isValid("+420608171262")); var_dump(PhoneNumberValidator::isValid("00420608171262")); var_dump('getRecognizedCountryPrefixes'); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("0608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("420608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("+421608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("00421608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("+420608171262")); var_dump(PhoneNumberValidator::getRecognizedCountryPrefixes("00420608171262")); var_dump('getPhoneNumberInAllPossibleForms'); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("0608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("420608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("+421608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("00421608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("+420608171262")); var_dump(PhoneNumberValidator::getPhoneNumberInAllPossibleForms("00420608171262")); var_dump('convertToRightForm'); var_dump(PhoneNumberValidator::convertToRightForm("608171262")); var_dump(PhoneNumberValidator::convertToRightForm("0608171262")); var_dump(PhoneNumberValidator::convertToRightForm("420608171262")); var_dump(PhoneNumberValidator::convertToRightForm("+421608171262")); var_dump(PhoneNumberValidator::convertToRightForm("00421608171262")); var_dump(PhoneNumberValidator::convertToRightForm("+420608171262")); var_dump(PhoneNumberValidator::convertToRightForm("00420608171262"));
based on VZjsN
Output for 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.17.3, 7.0.0 - 7.3.0beta1
string(18) "getPhoneNumberBase" string(0) "" string(1) "0" string(3) "421" string(4) "+421" string(5) "00421" string(4) "+420" string(5) "00420" string(7) "isValid" bool(false) bool(false) bool(false) bool(true) bool(true) bool(true) bool(true) string(28) "getRecognizedCountryPrefixes" bool(false) bool(false) bool(false) array(2) { [0]=> string(5) "00421" [1]=> string(4) "+421" } array(2) { [0]=> string(5) "00421" [1]=> string(4) "+421" } array(2) { [0]=> string(5) "00420" [1]=> string(4) "+420" } array(2) { [0]=> string(5) "00420" [1]=> string(4) "+420" } string(32) "getPhoneNumberInAllPossibleForms" bool(false) bool(false) bool(false) array(2) { [0]=> string(9) "00421+421" [1]=> string(8) "+421+421" } array(2) { [0]=> string(10) "0042100421" [1]=> string(9) "+42100421" } array(2) { [0]=> string(9) "00420+420" [1]=> string(8) "+420+420" } array(2) { [0]=> string(10) "0042000420" [1]=> string(9) "+42000420" } string(18) "convertToRightForm" bool(false) bool(false) bool(false) string(8) "+421+421" string(9) "+42100421" string(8) "+420+420" string(9) "+42000420"