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

use 
Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use 
Automattic\WooCommerce\StoreApi\Utilities\ValidationUtils;

/**
 * ShippingAddressSchema class.
 *
 * Provides a generic shipping address schema for composition in other schemas.
 */
class ShippingAddressSchema extends AbstractAddressSchema {
    
/**
     * The schema item name.
     *
     * @var string
     */
    
protected $title 'shipping_address';

    
/**
     * The schema item identifier.
     *
     * @var string
     */
    
const IDENTIFIER 'shipping-address';

    
/**
     * Convert a term object into an object suitable for the response.
     *
     * @param \WC_Order|\WC_Customer $address An object with shipping address.
     *
     * @throws RouteException When the invalid object types are provided.
     * @return array
     */
    
public function get_item_response$address ) {
        
$validation_util = new ValidationUtils();
        if ( ( 
$address instanceof \WC_Customer || $address instanceof \WC_Order ) ) {
            
$shipping_country $address->get_shipping_country();
            
$shipping_state   $address->get_shipping_state();

            if ( ! 
$validation_util->validate_state$shipping_state$shipping_country ) ) {
                
$shipping_state '';
            }

            
$additional_address_fields $this->additional_fields_controller->get_all_fields_from_object$address'shipping' );

            
$address_object array_merge(
                [
                    
'first_name' => $address->get_shipping_first_name(),
                    
'last_name'  => $address->get_shipping_last_name(),
                    
'company'    => $address->get_shipping_company(),
                    
'address_1'  => $address->get_shipping_address_1(),
                    
'address_2'  => $address->get_shipping_address_2(),
                    
'city'       => $address->get_shipping_city(),
                    
'state'      => $shipping_state,
                    
'postcode'   => $address->get_shipping_postcode(),
                    
'country'    => $shipping_country,
                    
'phone'      => $address->get_shipping_phone(),
                ],
                
$additional_address_fields
            
);

            
// Add any missing keys from additional_fields_controller to the address response.
            
foreach ( $this->additional_fields_controller->get_address_fields_keys() as $field ) {
                if ( isset( 
$address_object$field ] ) ) {
                    continue;
                }
                
$address_object$field ] = '';
            }

            foreach ( 
$address_object as $key => $value ) {
                if ( isset( 
$this->get_properties()[ $key ]['type'] ) && 'boolean' === $this->get_properties()[ $key ]['type'] ) {
                    
$address_object$key ] = (bool) $value;
                } else {
                    
$address_object$key ] = $this->prepare_html_response$value );
                }
            }
            return 
$address_object;
        }

        throw new 
RouteException(
            
'invalid_object_type',
            
sprintf(
                
/* translators: Placeholders are class and method names */
                
__'%1$s requires an instance of %2$s or %3$s for the address''woocommerce' ),
                
'ShippingAddressSchema::get_item_response',
                
'WC_Customer',
                
'WC_Order'
            
),
            
500
        
);
    }
}