HEX
Server: LiteSpeed
System: Linux eko108.isimtescil.net 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: uyarreklamcomtr (11202)
PHP: 7.4.33
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/ServiceRatesCollection.php.tar
httpdocs/wp-content/plugins/google-listings-and-ads/src/Shipping/ServiceRatesCollection.php000064400000005057151550410730034532 0ustar00var/www/vhosts/uyarreklam.com.tr<?php
declare( strict_types=1 );

namespace Automattic\WooCommerce\GoogleListingsAndAds\Shipping;

defined( 'ABSPATH' ) || exit;

/**
 * Class ServiceRatesCollection
 *
 * @package Automattic\WooCommerce\GoogleListingsAndAds\Shipping
 *
 * @since   2.1.0
 */
class ServiceRatesCollection extends CountryRatesCollection {
	/**
	 * @var string
	 */
	protected $shipping_area;

	/**
	 * @var float|null
	 */
	protected $min_order_amount;

	/**
	 * @var LocationRate[][]
	 */
	protected $class_groups;

	/**
	 * ServiceRatesCollection constructor.
	 *
	 * @param string     $country
	 * @param string     $shipping_area
	 * @param float|null $min_order_amount
	 * @param array      $location_rates
	 */
	public function __construct( string $country, string $shipping_area, ?float $min_order_amount = null, array $location_rates = [] ) {
		$this->shipping_area    = $shipping_area;
		$this->min_order_amount = $min_order_amount;
		parent::__construct( $country, $location_rates );
	}

	/**
	 * @return float|null
	 */
	public function get_min_order_amount(): ?float {
		return $this->min_order_amount;
	}

	/**
	 * @return string
	 */
	public function get_shipping_area(): string {
		return $this->shipping_area;
	}

	/**
	 * Return array of location rates grouped by their applicable shipping classes. Multiple rates might be returned per class.
	 *
	 * @return LocationRate[][] Arrays of location rates grouped by their applicable shipping class. Shipping class name is used as array keys.
	 */
	public function get_rates_grouped_by_shipping_class(): array {
		$this->group_rates_by_shipping_class();

		return $this->class_groups;
	}

	/**
	 * Group the location rates by their applicable shipping classes.
	 */
	public function group_rates_by_shipping_class(): void {
		if ( isset( $this->class_groups ) ) {
			return;
		}
		$this->class_groups = [];

		foreach ( $this->location_rates as $location_rate ) {
			if ( ! empty( $location_rate->get_shipping_rate()->get_applicable_classes() ) ) {
				// For every rate defined in the location_rate, create a new shipping rate and add it to the array
				foreach ( $location_rate->get_shipping_rate()->get_applicable_classes() as $class ) {
					$this->class_groups[ $class ][] = $location_rate;
				}
			} else {
				$this->class_groups[''][] = $location_rate;
			}
		}

		// Sort the groups so that the rate with no shipping class is placed at the end.
		krsort( $this->class_groups );
	}

	/**
	 * Reset the internal mappings/groups
	 */
	protected function reset_rates_mappings(): void {
		parent::reset_rates_mappings();
		unset( $this->class_groups );
	}
}