v1.0.0
This commit is contained in:
commit
1ef08bd731
27 changed files with 4854 additions and 0 deletions
263
includes/class-fontdelbosc-transit-maps.php
Normal file
263
includes/class-fontdelbosc-transit-maps.php
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
<?php
|
||||
/**
|
||||
* Label maps and helpers (types, directions, stickers, C-15 detection).
|
||||
*
|
||||
* @package Fontdelbosc_Transit
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Maps raw API values to display labels.
|
||||
*/
|
||||
class Fontdelbosc_Transit_Maps {
|
||||
|
||||
/**
|
||||
* Translate a DATEX2 record type into a display label.
|
||||
*
|
||||
* @param string $type Raw type (local part of xsi:type).
|
||||
* @return string
|
||||
*/
|
||||
public static function type_label( string $type ): string {
|
||||
switch ( $type ) {
|
||||
case 'Accident':
|
||||
return __( 'Accident', 'fontdelbosc-transit' );
|
||||
case 'MaintenanceWorks':
|
||||
case 'RoadWorks':
|
||||
return __( 'Roadworks', 'fontdelbosc-transit' );
|
||||
case 'VehicleObstruction':
|
||||
case 'ObstructionOnRoad':
|
||||
return __( 'Obstruction', 'fontdelbosc-transit' );
|
||||
case 'WeatherRelatedRoadConditions':
|
||||
return __( 'Weather conditions', 'fontdelbosc-transit' );
|
||||
case 'RoadsideAssistance':
|
||||
return __( 'Roadside assistance', 'fontdelbosc-transit' );
|
||||
case 'AbnormalTraffic':
|
||||
case 'TrafficCongestion':
|
||||
return __( 'Congestion', 'fontdelbosc-transit' );
|
||||
case 'NetworkManagement':
|
||||
case 'GeneralNetworkManagement':
|
||||
return __( 'Traffic management', 'fontdelbosc-transit' );
|
||||
case 'Restrictions':
|
||||
return __( 'Restrictions', 'fontdelbosc-transit' );
|
||||
default:
|
||||
return __( 'Incident', 'fontdelbosc-transit' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a TPEG direction code into a display label.
|
||||
*
|
||||
* @param string $direction Raw direction code.
|
||||
* @return string
|
||||
*/
|
||||
public static function direction_label( string $direction ): string {
|
||||
switch ( $direction ) {
|
||||
case 'northBound':
|
||||
return __( 'Northbound', 'fontdelbosc-transit' );
|
||||
case 'southBound':
|
||||
return __( 'Southbound', 'fontdelbosc-transit' );
|
||||
case 'eastBound':
|
||||
return __( 'Eastbound', 'fontdelbosc-transit' );
|
||||
case 'westBound':
|
||||
return __( 'Westbound', 'fontdelbosc-transit' );
|
||||
case 'bothWays':
|
||||
case 'both':
|
||||
return __( 'Both directions', 'fontdelbosc-transit' );
|
||||
default:
|
||||
return $direction;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a DATEX2 impact code into a display label.
|
||||
*
|
||||
* @param string $impact Raw impact code.
|
||||
* @return string
|
||||
*/
|
||||
public static function impact_label( string $impact ): string {
|
||||
switch ( $impact ) {
|
||||
case 'heavy':
|
||||
case 'veryHeavy':
|
||||
return __( 'Heavy impact on traffic', 'fontdelbosc-transit' );
|
||||
case 'medium':
|
||||
return __( 'Medium impact on traffic', 'fontdelbosc-transit' );
|
||||
case 'light':
|
||||
case 'low':
|
||||
return __( 'Light impact on traffic', 'fontdelbosc-transit' );
|
||||
default:
|
||||
return $impact;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate a validity status code into a display label.
|
||||
*
|
||||
* @param string $status Raw status code.
|
||||
* @return string
|
||||
*/
|
||||
public static function status_label( string $status ): string {
|
||||
switch ( $status ) {
|
||||
case 'active':
|
||||
return __( 'Active', 'fontdelbosc-transit' );
|
||||
case 'scheduled':
|
||||
return __( 'Scheduled', 'fontdelbosc-transit' );
|
||||
default:
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a DGT sticker value ("Sin distintivo", "B", "C"...) to a label.
|
||||
*
|
||||
* @param string $sticker Raw sticker value.
|
||||
* @return string
|
||||
*/
|
||||
public static function sticker_label( string $sticker ): string {
|
||||
switch ( $sticker ) {
|
||||
case 'Sin distintivo':
|
||||
case 'Sin etiqueta':
|
||||
return __( 'No label (pre-2000 petrol / pre-2006 diesel)', 'fontdelbosc-transit' );
|
||||
case 'B':
|
||||
return __( 'Label B', 'fontdelbosc-transit' );
|
||||
case 'C':
|
||||
return __( 'Label C', 'fontdelbosc-transit' );
|
||||
case 'ECO':
|
||||
return __( 'Label ECO', 'fontdelbosc-transit' );
|
||||
case '0':
|
||||
return __( 'Label 0 emissions', 'fontdelbosc-transit' );
|
||||
default:
|
||||
return $sticker;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a DATEX2 record type to a stable icon/badge key.
|
||||
*
|
||||
* @param string $type Raw type (local part of xsi:type).
|
||||
* @return string
|
||||
*/
|
||||
public static function type_key( string $type ): string {
|
||||
switch ( $type ) {
|
||||
case 'Accident':
|
||||
return 'accident';
|
||||
case 'MaintenanceWorks':
|
||||
case 'RoadWorks':
|
||||
return 'works';
|
||||
case 'VehicleObstruction':
|
||||
case 'ObstructionOnRoad':
|
||||
return 'obstruction';
|
||||
case 'WeatherRelatedRoadConditions':
|
||||
return 'weather';
|
||||
case 'RoadsideAssistance':
|
||||
return 'assistance';
|
||||
case 'AbnormalTraffic':
|
||||
case 'TrafficCongestion':
|
||||
return 'congestion';
|
||||
case 'NetworkManagement':
|
||||
case 'GeneralNetworkManagement':
|
||||
return 'management';
|
||||
case 'Restrictions':
|
||||
return 'restriction';
|
||||
default:
|
||||
return 'incident';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a DGT sticker value to a chip color key (real label colors).
|
||||
*
|
||||
* @param string $sticker Raw sticker value.
|
||||
* @return string
|
||||
*/
|
||||
public static function sticker_class( string $sticker ): string {
|
||||
switch ( $sticker ) {
|
||||
case 'Sin distintivo':
|
||||
case 'Sin etiqueta':
|
||||
return 'none';
|
||||
case 'B':
|
||||
return 'b';
|
||||
case 'C':
|
||||
return 'c';
|
||||
case 'ECO':
|
||||
return 'eco';
|
||||
case '0':
|
||||
return 'zero';
|
||||
default:
|
||||
return 'other';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether an incident belongs to the C-15 corridor (Eix Garraf-Anoia).
|
||||
*
|
||||
* @param array<mixed> $incident Normalized incident.
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_c15( array $incident ): bool {
|
||||
$road = self::normalize_road( Fontdelbosc_Transit_Payload::str( $incident['road_name'] ?? null ) ?? '' );
|
||||
|
||||
if ( 'C15' === $road || 'EIXGARRAF' === substr( $road, 0, 9 ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$title = Fontdelbosc_Transit_Payload::str( $incident['title'] ?? null ) ?? '';
|
||||
$description = Fontdelbosc_Transit_Payload::str( $incident['description'] ?? null ) ?? '';
|
||||
$haystack = $title . ' ' . $description;
|
||||
|
||||
// Word boundary required: C-153 and C-155 are different roads.
|
||||
return 1 === preg_match( '/C-?15\b/i', $haystack ) || false !== stripos( $haystack, 'eix garraf' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a road name for comparison (uppercase, no spaces or hyphens).
|
||||
*
|
||||
* @param string $road Raw road name.
|
||||
* @return string
|
||||
*/
|
||||
private static function normalize_road( string $road ): string {
|
||||
return strtoupper( preg_replace( '/[\s\-]+/u', '', $road ) ?? '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an ISO or RSS timestamp as a local date-time string.
|
||||
*
|
||||
* @param string $time Raw timestamp.
|
||||
* @return string Empty when unparseable.
|
||||
*/
|
||||
public static function format_time( string $time ): string {
|
||||
if ( '' === $time ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$ts = strtotime( $time );
|
||||
|
||||
if ( false === $ts ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$formatted = wp_date( 'j M Y H:i', $ts );
|
||||
|
||||
return is_string( $formatted ) ? $formatted : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Human "last updated" line from a raw timestamp.
|
||||
*
|
||||
* @param string $time Raw timestamp.
|
||||
* @return string Empty when unparseable.
|
||||
*/
|
||||
public static function last_updated( string $time ): string {
|
||||
if ( '' === $time ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$ts = strtotime( $time );
|
||||
|
||||
return false === $ts ? '' : sprintf(
|
||||
/* translators: %s: human-readable time difference. */
|
||||
__( 'Last updated: %s ago', 'fontdelbosc-transit' ),
|
||||
human_time_diff( $ts, time() )
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue