<?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   = strtoupper( trim( $postal_code ) );
		$country_code = strtoupper( trim( $country_code ) );

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

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

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

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