100 lines
3.4 KiB
PHP
100 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Fuel catalog bridge between MITECO product IDs and station price fields.
|
|
*
|
|
* @package FontdelboscCombustible
|
|
*/
|
|
|
|
declare( strict_types=1 );
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Resolves product IDs to station price field names and translated labels.
|
|
*/
|
|
final class Fontdelbosc_Combustible_Fuels {
|
|
|
|
/**
|
|
* Default products: Gasolina 95 E5 (1), Gasolina 98 E5 (3),
|
|
* Gasóleo A habitual (4), Gasóleo Premium (5). Verified against the
|
|
* live catalog; the spec's older ID table is outdated.
|
|
*/
|
|
public const DEFAULT_IDS = array( 1, 3, 4, 5 );
|
|
|
|
/**
|
|
* Catalog product names whose station payload key differs from
|
|
* the "Precio " + name join (accents or wording).
|
|
*
|
|
* @var array<string,string>
|
|
*/
|
|
private const NAME_ALIASES = array(
|
|
'Gasóleo A habitual' => 'Gasoleo A',
|
|
'Gasóleo B' => 'Gasoleo B',
|
|
'Gasóleo C' => 'Gasoleo C',
|
|
'Gasóleo Premium' => 'Gasoleo Premium',
|
|
'Biodiésel' => 'Biodiesel',
|
|
'Hidrógeno' => 'Hidrogeno',
|
|
);
|
|
|
|
/**
|
|
* Resolves a catalog product name to the matching station price field.
|
|
*
|
|
* @param string $product_name Catalog NombreProducto.
|
|
* @param array<string> $labels Price field labels present in the payload.
|
|
*/
|
|
public static function station_field( string $product_name, array $labels ): ?string {
|
|
$alias = self::NAME_ALIASES[ $product_name ] ?? $product_name;
|
|
$want = self::norm( $alias );
|
|
|
|
foreach ( $labels as $label ) {
|
|
if ( self::norm( $label ) === $want ) {
|
|
return $label;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Returns a translated display label for a product name,
|
|
* falling back to the raw API name for unknown products.
|
|
*
|
|
* @param string $product_name Catalog NombreProducto or station field label.
|
|
*/
|
|
public static function label( string $product_name ): string {
|
|
$name = self::NAME_ALIASES[ $product_name ] ?? $product_name;
|
|
|
|
return match ( self::norm( $name ) ) {
|
|
'gasolina 95 e5' => __( 'Gasoline 95 E5', 'fontdelbosc-combustible' ),
|
|
'gasolina 95 e5 premium' => __( 'Gasoline 95 E5 Premium', 'fontdelbosc-combustible' ),
|
|
'gasolina 98 e5' => __( 'Gasoline 98 E5', 'fontdelbosc-combustible' ),
|
|
'gasoleo a' => __( 'Diesel A', 'fontdelbosc-combustible' ),
|
|
'gasoleo premium' => __( 'Premium Diesel', 'fontdelbosc-combustible' ),
|
|
'gasoleo b' => __( 'Diesel B', 'fontdelbosc-combustible' ),
|
|
'gases licuados del petroleo' => __( 'LPG', 'fontdelbosc-combustible' ),
|
|
'gas natural comprimido' => __( 'CNG', 'fontdelbosc-combustible' ),
|
|
'gas natural licuado' => __( 'LNG', 'fontdelbosc-combustible' ),
|
|
'hidrogeno' => __( 'Hydrogen', 'fontdelbosc-combustible' ),
|
|
'biodiesel' => __( 'Biodiesel', 'fontdelbosc-combustible' ),
|
|
'diesel renovable' => __( 'Renewable Diesel', 'fontdelbosc-combustible' ),
|
|
'gasolina renovable' => __( 'Renewable Gasoline', 'fontdelbosc-combustible' ),
|
|
'adblue' => __( 'AdBlue', 'fontdelbosc-combustible' ),
|
|
default => $product_name,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Normalizes a name for comparison: lowercase, accents stripped.
|
|
*
|
|
* @param string $name Raw name.
|
|
*/
|
|
private static function norm( string $name ): string {
|
|
$ascii = iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', $name );
|
|
|
|
if ( false === $ascii || '' === $ascii ) {
|
|
$ascii = $name;
|
|
}
|
|
|
|
return trim( strtolower( $ascii ) );
|
|
}
|
|
}
|