/** * Normalizing phone number * * @version 1.0.12 2015-09-25 * @author Dmitry Fedorov <*****@*****.**> * @param string $phone * @param string $countryCode * @return string */ public static function normalizePhone($phone, $countryCode) { if (empty(self::$_callingCodes)) { $countryRepository = new Country(); self::$_callingCodes = $countryRepository->getCallingCodes(); } if (!$countryCode) { $countryCode = 'JP'; } $newPhone = static::PhoneUtil()->normalizeDigitsOnly($phone); $callingCode = self::$_callingCodes[strtoupper($countryCode)]; if (!$newPhone || $newPhone == $callingCode) { return ''; } if (strpos($newPhone, $callingCode) !== 0) { $newPhone = $callingCode . $newPhone; } return '+' . $newPhone; }
public function run() { $data = $this->_countryRepository->findAll(); $items = []; foreach ($data as $code => $lang) { if ($this->countries === [] || in_array(strtolower($code), $this->countries)) { $locales[$code] = FlagIcon::flag($code); $items[$code] = ['label' => Html::a($locales[$code] . ' ' . $lang->name['english']['common'] . ' (' . reset($lang->name['native'])['common'] . ')', '#', ['class' => 'changePhoneMask', 'data-region' => $code])]; } } $this->dropdown = array_merge($this->dropdown, ['items' => $items, 'encodeLabels' => false]); $callingCodes = $this->_countryRepository->getCallingCodes(); $codes = Json::encode($callingCodes); $this->_js[] = <<<JS var dialCodes = {$codes}; \$(".changePhoneMask").on("click", function () { var region = \$(this).data("region"), data = dialCodes[region], list = \$(this).closest("ul"), container = list.parent(), widgetContainer = \$(this).closest(".{$this->s['class']['widgetContainer']}"), span = container ? container.prev() : false, input = \$(".{$this->s['class']['inputMask']}", widgetContainer); if(undefined !== data && !data.startsWith("+")) { data = "+" + data.replace(/(.)/g,"\\\\\$1"); } else { data = ""; } list.siblings("button").html(\$(this).html() + " <span class='caret'></span>"); list.dropdown("toggle"); input .prop("disabled", false) .inputmask({"mask": data + "9{3,15}"}) .val("") .focus(); return false; }); \$(".changePhoneMask").click(function(event) { var widgetContainer = \$(this).closest(".{$this->s["class"]["widgetContainer"]}"); \$("#" + widgetContainer.data("country-code-id")).val( \$(this).data("region") ); }); \$(".{$this->s["class"]["widgetContainer"]}").each(function(index) { var widgetContainer = \$(this), countryCode = \$("#" + widgetContainer.data("country-code-id")).val(); if (countryCode) { var object = \$('.changePhoneMask[data-region=' + countryCode + ']', widgetContainer); object.closest('ul').siblings('button').html(object.html() + " <span class='caret'></span>"); } }); JS; $this->getView()->registerJs(join("\r", $this->_js)); return $this->render('index', ['widget' => $this, 's' => $this->s]); }