MapsGenericAddress.php
33 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Abilities\Concerns; |
| 4 | |
| 5 | /** |
| 6 | * Maps generic `{prefix}_*` address fields from ability input into a nested address |
| 7 | * array (`city`, `country`, `line_1`, …) for the SureCart API. |
| 8 | */ |
| 9 | trait MapsGenericAddress { |
| 10 | |
| 11 | /** |
| 12 | * Map prefixed address keys in ability input to a nested address array. |
| 13 | * |
| 14 | * @param array $input The input data. |
| 15 | * @param string $prefix The key prefix without trailing underscore (e.g. `shipping_address`). |
| 16 | * |
| 17 | * @return array |
| 18 | */ |
| 19 | protected function map_generic_address( array $input, string $prefix ): array { |
| 20 | $address = array(); |
| 21 | $address_fields = array( 'city', 'country', 'line_1', 'line_2', 'postal_code', 'state' ); |
| 22 | |
| 23 | foreach ( $address_fields as $key ) { |
| 24 | $input_key = $prefix . '_' . $key; |
| 25 | if ( ! empty( $input[ $input_key ] ) ) { |
| 26 | $address[ $key ] = sanitize_text_field( $input[ $input_key ] ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | return $address; |
| 31 | } |
| 32 | } |
| 33 |