-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnormalize.php
130 lines (95 loc) · 2.26 KB
/
normalize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Phutility;
class Normalize {
const FORMAT_DATE = 'Y-m-d';
const FORMAT_TIME = 'H:i:s';
const FORMAT_DATETIME = 'Y-m-d H:i:s';
/** Return a normalized Israeli phone number
*
* @param string $phone_number Phone number
* @param bool $abroad Format the phone number for use outside Israel
* @return string|null Formatted phone number, null on invalid input
*/
public static function phoneIsrael($phone_number, $abroad=false)
{
$s = preg_replace('/[x\D]/iu', '', $phone_number);
// Account for phone extension
$parts = explode('x', strtolower($s));
$s = $parts[0];
// Normalize
if ( substr($s, 0, 3)==='972' ) {
$s = '0' . substr($s, 3);
}
// Check length
if ( strlen($s)==(10) ) {
if ( $s[1]!=='5' ) {
return null;
}
} else if ( strlen($s)==(9) ) {
if ( $s[1]==='5' ) {
return null;
}
} else {
return null;
}
// Format
$s = substr_replace($s, '.', -7, 0);
$s = substr_replace($s, '.', -4, 0);
// Replace extension
if ( 1<count($parts) ) {
$ext = preg_replace('/[\D]/u', '', $parts[1]);
$s = $s . ' Ext ' . $ext;
}
// Outside Israel
if ( $abroad ) {
$s = '+972.'.substr($s, 1);
}
return $s;
}
/**
* Normalize a Date
*
* @param DateTime|int|string $date
* @return null|string
*/
public static function normalizeDate($date)
{
return self::_normalizeFormat($date, self::FORMAT_DATE);
}
/**
* Normalize a Time
*
* @param DateTime|int|string $date
* @return null|string
*/
public static function normalizeTime($date)
{
return self::_normalizeFormat($date, self::FORMAT_TIME);
}
/**
* Normalize a DateTime
*
* @param DateTime|int|string $date
* @return null|string
*/
public static function normalizeDateTime($date)
{
return self::_normalizeFormat($date, self::FORMAT_DATETIME);
}
/**
* Normalize a DateTime
*
* @param DateTime|int|string $date
* @return null|string
*/
protected static function _normalizeFormat($date, $format)
{
if ($date instanceof \DateTime) {
return $date->format($format);
}
if ( is_numeric($date) && 99999999<(int)$date ) { // Consider dates in format "Ymd" to not be UNIX timestamps
return date($format, $date);
}
return $date ? date($format, strtotime($date)) : null;
}
}