/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Utilities/ValidationUtils.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
<?php
namespace Automattic\WooCommerce\StoreApi\Utilities;

/**
 * ValidationUtils class.
 * Helper class which validates and update customer info.
 */
class ValidationUtils {
    
/**
     * Get list of states for a country.
     *
     * @param string $country Country code.
     * @return array Array of state names indexed by state keys.
     */
    
public function get_states_for_country$country ) {
        return 
$country array_filter( (array) \wc()->countries->get_states$country ) ) : [];
    }

    
/**
     * Validate provided state against a countries list of defined states.
     *
     * If there are no defined states for a country, any given state is valid.
     *
     * @param string $state State name or code (sanitized).
     * @param string $country Country code.
     * @return boolean Valid or not valid.
     */
    
public function validate_state$state$country ) {
        
$states $this->get_states_for_country$country );

        if ( 
count$states ) && ! in_array\wc_strtoupper$state ), array_map'\wc_strtoupper'array_keys$states ) ), true ) ) {
            return 
false;
        }

        return 
true;
    }


    
/**
     * Format a state based on the country. If country has defined states, will return a valid upper case state code.
     *
     * @param string $state State name or code (sanitized).
     * @param string $country Country code.
     * @return string
     */
    
public function format_state$state$country ) {
        
$states $this->get_states_for_country$country );

        if ( 
count$states ) ) {
            
$state        \wc_strtoupper$state );
            
$state_values array_map'\wc_strtoupper'array_fliparray_map'\wc_strtoupper'$states ) ) );

            if ( isset( 
$state_values$state ] ) ) {
                
// Convert to state code if a state name was provided.
                
return $state_values$state ];
            }
        }

        return 
$state;
    }
}