<?php
/**
 * Class PostalCodeNormalizer
 *
 * @package DhlPolska\Shipment
 */

namespace DhlPolska\Shipment;

/**
 * Normalizes postal codes for DHL API requests.
 */
class PostalCodeNormalizer {

	/**
	 * @param string $postal_code .
	 * @param string $country_code .
	 *
	 * @return string
	 */
	public function normalize( string $postal_code, string $country_code ): string {
		$normalized_postal_code = strtoupper( trim( $postal_code ) );
		$country_code           = strtoupper( trim( $country_code ) );

		if ( '' === $normalized_postal_code ) {
			return $normalized_postal_code;
		}

		if ( 'PT' === $country_code ) {
			return str_replace( ' ', '', $normalized_postal_code );
		}

		if ( 'LV' === $country_code ) {
			$normalized_postal_code = preg_replace( '/^LV-?/i', '', $normalized_postal_code );
		}

		return str_replace( [ ' ', '-' ], '', $normalized_postal_code );
	}
}
