FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Phone.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Phone extends Char{
6 
7  const NUMBER_ALL = null;
8  const NUMBER_MOBILE = 'mobile';
9  const NUMBER_FIXED = 'fixed';
10 
11  const NUMBER = 'number'; //!< Allowed number type (see NUMBER_* constants).
12  const COUNTRIES = 'countries'; //!< Allowed countries (single or array of ISO2 codes; empty = all).
13  const POPULAR = 'popular'; //!< Initially show only those popular countries.
14 
15  public $regex = '/^\\+(\\d+( |\\-| ?\\(|\\) ?)?)+\\d+$/';
17  public $countryOrder = 'short';
18  public $countrySelectCaption = '';
19  public $countryShowAllCaption = '...';
20  public $countryFormat = '[emoji]&nbsp;&nbsp;&nbsp;[short] ([shortCode])';
21  public $countriesTtl = 7 * 86400;
22  public $countriesKey = 'fred-controller-widget-phone-countries';
23  public $popular = [];
24 
25  protected $_countries = null;
26 
27  public function clientConfig(){
28  $config = parent::clientConfig();
29  $mask_key = $this->number ? $this->number . 'Mask' : 'mask';
30  $example_key = $this->number ? $this->number . 'Example' : 'example';
31  $config[self::COUNTRIES] = [];
32  foreach($this->countries as $code => $country) $config[self::COUNTRIES][$code] =
33  \Rsi\Record::select($country,['callingCode','internationalPrefix','nationalPrefix']) + [
34  'mask' => $country[$mask_key],
35  'example' => '+' . $country['callingCode'] . ' ' . $country[$example_key]
36  ];
37  return $config;
38  }
39 
40  protected function checkFormat($value,$index = null){
41  $value = preg_replace('/\\D+/','',$value);
42  $mask_key = $this->number ? $this->number . 'Mask' : 'mask';
43  foreach($this->countries as $country) if(preg_match('/^' . $country['callingCode'] . '(' . ($country[$mask_key] ?? '.+') . ')$/',$value)) return true;
44  return false;
45  }
46 
47  protected function getCountries(){
48  if($this->_countries === null){
49  $data = $this->fred->cache->fetch($this->countriesKey,$this->countriesTtl,function(){
50  $data = [];
51  foreach(\Rsi\Record::sort($this->fred->services->countries(),function($a,$b){
52  return $a[$this->countryOrder] <=> $b[$this->countryOrder];
53  }) as $code => $country) if($info = $country['dialling'] ?? null){
54  $data[$code] = ['title' => $this->fred->trans->str($this->countryFormat,$country)];
55  foreach($info as $key => $value) $data[$code][lcfirst(\Rsi\Str::camel($key))] = is_array($value) ? array_shift($value) : $value;
56  }
57  return $data;
58  });
59  $this->_countries = ($countries = array_filter((array)$this->config(self::COUNTRIES))) ? \Rsi\Record::select($data,$countries) : $data;
60  }
61  return $this->_countries;
62  }
63 
64 }
Rsi\Fred\Controller\Widget\Phone\POPULAR
const POPULAR
Initially show only those popular countries.
Definition: Phone.php:13
Rsi\Fred\Controller\Widget\Phone\$countriesKey
$countriesKey
Definition: Phone.php:22
Rsi\Fred\Controller\Widget\Phone\$countriesTtl
$countriesTtl
Definition: Phone.php:21
Rsi\Fred\Controller\Widget\Phone\$number
$number
Definition: Phone.php:16
Rsi
Rsi\Fred\Controller\Widget\Phone\NUMBER_ALL
const NUMBER_ALL
Definition: Phone.php:7
Rsi\Fred\Controller\Widget\Phone\$countrySelectCaption
$countrySelectCaption
Definition: Phone.php:18
Rsi\Fred\Controller\Widget\Phone\getCountries
getCountries()
Definition: Phone.php:47
Rsi\Fred\Controller\Widget\Char
Definition: Char.php:5
Rsi\Fred\Controller\Widget\Phone\COUNTRIES
const COUNTRIES
Allowed countries (single or array of ISO2 codes; empty = all).
Definition: Phone.php:12
Rsi\Fred\Controller\Widget\Phone\clientConfig
clientConfig()
Public configuration.
Definition: Phone.php:27
Rsi\Fred\Controller\Widget\Phone\NUMBER
const NUMBER
Allowed number type (see NUMBER_* constants).
Definition: Phone.php:11
Rsi\Fred\Controller\Widget\Phone\$popular
$popular
Definition: Phone.php:23
Rsi\Fred\Controller\Widget\config
config($key, $default=null)
Get single configuration value.
Definition: Widget.php:98
Rsi\Fred\Controller\Widget\Phone\$countryShowAllCaption
$countryShowAllCaption
Definition: Phone.php:19
Rsi\Fred\Controller\Widget
Definition: Char.php:3
Rsi\Fred\Controller\Widget\Phone
Definition: Phone.php:5
Rsi\Fred\Controller\Widget\Phone\$_countries
$_countries
Definition: Phone.php:25
Rsi\Fred\Controller\Widget\Phone\$countryOrder
$countryOrder
Definition: Phone.php:17
Rsi\Fred\Controller\Widget\Phone\checkFormat
checkFormat($value, $index=null)
Definition: Phone.php:40
Rsi\Fred\Controller\Widget\Phone\$countryFormat
$countryFormat
Definition: Phone.php:20
Rsi\Fred\Controller\Widget\Phone\NUMBER_FIXED
const NUMBER_FIXED
Definition: Phone.php:9
Rsi\Fred\Controller\Widget\Phone\NUMBER_MOBILE
const NUMBER_MOBILE
Definition: Phone.php:8
Rsi\Fred\Controller\Widget\Phone\$regex
$regex
Definition: Phone.php:15