Completed
Push — master ( 0927cb...7b3ff6 )
by Claudio
08:31
created

WC_Validation   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 154
rs 10
wmc 19
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A is_email() 0 3 1
A is_phone() 0 7 2
C is_postcode() 0 42 11
A is_GB_postcode() 0 48 3
A format_postcode() 0 3 1
A format_phone() 0 3 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Contains Validation functions
9
 *
10
 * @class    WC_Validation
11
 * @version  2.4.0
12
 * @package  WooCommerce/Classes
13
 * @category Class
14
 * @author   WooThemes
15
 */
16
class WC_Validation {
17
18
	/**
19
	 * Validates an email using wordpress native is_email function.
20
	 *
21
	 * @param   string	email address
22
	 * @return  bool
23
	 */
24
	public static function is_email( $email ) {
25
		return is_email( $email );
26
	}
27
28
	/**
29
	 * Validates a phone number using a regular expression.
30
	 *
31
	 * @param   string	phone number
32
	 * @return  bool
33
	 */
34
	public static function is_phone( $phone ) {
35
		if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) ) {
36
			return false;
37
		}
38
39
		return true;
40
	}
41
42
	/**
43
	 * Checks for a valid postcode.
44
	 *
45
	 * @param   string	postcode
46
	 * @param	string	country
47
	 * @return  bool
48
	 */
49
	public static function is_postcode( $postcode, $country ) {
50
		if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) {
51
			return false;
52
		}
53
54
		switch ( $country ) {
55
			case 'BR' :
56
				$valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode );
57
				break;
58
			case 'CH' :
59
				$valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode );
60
				break;
61
			case 'DE' :
62
				$valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode );
63
				break;
64
			case 'ES' :
65
				$valid = (bool) preg_match( '/^([0-9]{5})$/i', $postcode );
66
				break;
67
			case 'GB' :
68
				$valid = self::is_GB_postcode( $postcode );
69
				break;
70
			case 'JP' :
71
				$valid = (bool) preg_match( '/^([0-9]{3})([-])([0-9]{4})$/', $postcode );
72
				break;
73
			case 'PT' :
74
				$valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode );
75
				break;
76
			case 'US' :
77
				$valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode );
78
				break;
79
            case 'CA' :
80
                // CA Postal codes cannot contain D,F,I,O,Q,U and cannot start with W or Z. https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Number_of_possible_postal_codes
81
				$valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode );
82
				break;
83
84
			default :
85
				$valid = true;
86
				break;
87
		}
88
89
		return apply_filters( 'woocommerce_validate_postcode', $valid, $postcode, $country );
90
	}
91
92
	/**
93
	 * Check if is a GB postcode.
94
	 *
95
	 * @author John Gardner
96
	 * @param  string $to_check A postcode
97
	 * @return bool
98
	 */
99
	public static function is_GB_postcode( $to_check ) {
100
101
		// Permitted letters depend upon their position in the postcode.
102
		// http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
103
		$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
104
		$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
105
		$alpha3 = "[abcdefghjkpstuw]";         // Character 3 == ABCDEFGHJKPSTUW
106
		$alpha4 = "[abehmnprvwxy]";            // Character 4 == ABEHMNPRVWXY
107
		$alpha5 = "[abdefghjlnpqrstuwxyz]";    // Character 5 != CIKMOV
108
109
		$pcexp = array();
110
111
		// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
112
		$pcexp[0] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$/';
113
114
		// Expression for postcodes: ANA NAA
115
		$pcexp[1] =  '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$/';
116
117
		// Expression for postcodes: AANA NAA
118
		$pcexp[2] =  '/^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$/';
119
120
		// Exception for the special postcode GIR 0AA
121
		$pcexp[3] =  '/^(gir)(0aa)$/';
122
123
		// Standard BFPO numbers
124
		$pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
125
126
		// c/o BFPO numbers
127
		$pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';
128
129
		// Load up the string to check, converting into lowercase and removing spaces
130
		$postcode = strtolower( $to_check );
131
		$postcode = str_replace( ' ', '', $postcode );
132
133
		// Assume we are not going to find a valid postcode
134
		$valid = false;
135
136
		// Check the string against the six types of postcodes
137
		foreach ( $pcexp as $regexp ) {
138
			if ( preg_match( $regexp, $postcode, $matches ) ) {
139
				// Remember that we have found that the code is valid and break from loop
140
				$valid = true;
141
				break;
142
			}
143
		}
144
145
		return $valid;
146
	}
147
148
	/**
149
	 * Format the postcode according to the country and length of the postcode.
150
	 *
151
	 * @param   string	postcode
152
	 * @param	string	country
153
	 * @return  string	formatted postcode
154
	 */
155
	public static function format_postcode( $postcode, $country ) {
156
		return wc_format_postcode( $postcode, $country );
157
	}
158
159
	/**
160
	 * format_phone function.
161
	 *
162
	 * @access public
163
	 * @param mixed $tel
164
	 * @return string
165
	 */
166
	public static function format_phone( $tel ) {
167
		return wc_format_phone_number( $tel );
168
	}
169
}
170