Bouncy Melons 0.1
declarative style orm
PhoneField.php
Go to the documentation of this file.
1 <?php
2 namespace bm;
3 
4 class PhoneField extends Field {
5  const DEFAULT_SQL_TYPE = "varchar(10)";
6 
7  public function beforeSet($value, ISingle $single) {
8  return preg_replace('/[^0-9]/', '', $value);
9  }
10 
11 
12  public function beforeGet($value, ISingle $single) {
13  $val = parent::beforeGet($value, $single);
14  return $this->formatPhone($val);
15  }
16 
17  public function formatPhone($value) {
18  $mask = "+*(***)***-**-**";
19  if(!empty($this->getOption('mask'))) {
20  $mask = $this->getOption('mask');
21  }
22  $dmask = trim(preg_replace('/[^\*]/',',',$mask),",");
23 // $uexploded = explode("*",$mask);
24 // $delimiters = [];
25 // foreach($uexploded as $item) {
26 // if(empty($item)) {
27 // continue;
28 // }
29 // $delimiters[] = $item;
30 // }
31 // var_dump($delimiters);
32  $dexploded = explode(",",$dmask);
33 // var_dump($dexploded);
34  $inputPattern = "";
35 // $varcount = 0;
36  foreach($dexploded as $item) {
37  if(empty($item)) {
38  continue;
39  }
40  $inputPattern .="([0-9]{".strlen($item)."})";
41 // $varcount++;
42  }
43 // var_dump($inputPattern);
44  $outputPattern = "";
45  $prevL = '';
46  $counter = 1;
47  foreach(str_split($mask) as $l) {
48  if($l != '*') {
49  $outputPattern .= $l;
50  $prevL = $l;
51  continue;
52  }
53  if($l != $prevL) {
54  $outputPattern .= "$".$counter;
55  $counter++;
56  }
57  $prevL = $l;
58  }
59 // var_dump($outputPattern);
60  return preg_replace('/'.$inputPattern.'/', $outputPattern, $value);
61  }
62 }
beforeGet($value, ISingle $single)
Definition: PhoneField.php:12
beforeSet($value, ISingle $single)
Definition: PhoneField.php:7
formatPhone($value)
Definition: PhoneField.php:17