forked from webmaniabr/NFeWooCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-format.php
171 lines (127 loc) · 2.79 KB
/
class-format.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerceNFeFormat extends WooCommerceNFe {
/**
* CPF format
*
* @return string
*/
function cpf( $string ){
if (!$string) return;
$string = self::clear( $string );
$string = self::mask($string,'###.###.###-##');
return $string;
}
/**
* CNPJ format
*
* @return string
*/
function cnpj( $string ){
if (!$string) return;
$string = self::clear( $string );
$string = self::mask($string,'##.###.###/####-##');
return $string;
}
/**
* Zipcode format
*
* @return string
*/
function cep( $string ){
if (!$string) return;
$string = self::clear( $string );
$string = self::mask($string,'#####-###');
return $string;
}
/**
* Clear string
*
* @return string
*/
function clear( $string ) {
$string = str_replace( array(',', '-', '!', '.', '/', '?', '(', ')', ' ', '$', 'R$', '€'), '', $string );
return $string;
}
/**
* Mask string
*
* @return string
*/
function mask($val, $mask) {
$maskared = '';
$k = 0;
for ($i = 0; $i<=strlen($mask)-1; $i++) {
if ($mask[$i] == '#'){
if (isset($val[$k]))
$maskared .= $val[$k++];
} else {
if(isset($mask[$i])) $maskared .= $mask[$i];
}
}
return $maskared;
}
/**
* Validate CPF
*
* @return string
*/
function is_cpf( $cpf ) {
$cpf = preg_replace( '/[^0-9]/', '', $cpf );
if ( 11 != strlen( $cpf ) || preg_match( '/^([0-9])\1+$/', $cpf ) ) {
return false;
}
$digit = substr( $cpf, 0, 9 );
for ( $j = 10; $j <= 11; $j++ ) {
$sum = 0;
for( $i = 0; $i< $j-1; $i++ ) {
$sum += ( $j - $i ) * ( (int) $digit[ $i ] );
}
$summod11 = $sum % 11;
$digit[ $j - 1 ] = $summod11 < 2 ? 0 : 11 - $summod11;
}
return $digit[9] == ( (int) $cpf[9] ) && $digit[10] == ( (int) $cpf[10] );
}
/**
* Validate CNPJ
*
* @return string
*/
function is_cnpj( $cnpj ) {
$cnpj = sprintf( '%014s', preg_replace( '{\D}', '', $cnpj ) );
if ( 14 != ( strlen( $cnpj ) ) || ( 0 == intval( substr( $cnpj, -4 ) ) ) ) {
return false;
}
for ( $t = 11; $t < 13; ) {
for ( $d = 0, $p = 2, $c = $t; $c >= 0; $c--, ( $p < 9 ) ? $p++ : $p = 2 ) {
$d += $cnpj[ $c ] * $p;
}
if ( $cnpj[ ++$t ] != ( $d = ( ( 10 * $d ) % 11 ) % 10 ) ) {
return false;
}
}
return true;
}
/**
* Format number
*
* @return string
**/
function format_number( $string ) {
return str_replace( array( '.', '-', '/' ), '', $string );
}
/**
* Format birthdate
*
* @return string
**/
function get_formatted_birthdate( $date, $server ) {
$birthdate = explode( '/', $date );
if ( isset( $birthdate[1] ) && ! empty( $birthdate[1] ) ) {
return $server->format_datetime( $birthdate[1] . '/' . $birthdate[0] . '/' . $birthdate[2] );
}
return '';
}
}