diff --git a/bridges/EdfColorDayBridge.php b/bridges/EdfColorDayBridge.php new file mode 100644 index 00000000000..9f82db6562d --- /dev/null +++ b/bridges/EdfColorDayBridge.php @@ -0,0 +1,122 @@ + [ + 'name' => 'Choisir un contrat', + 'type' => 'list', + // we can add later more option prices like EJP + 'values' => [ + 'Tempo' => 'tempo' + ], + ] + ] + ]; + const CACHE_TIMEOUT = 7200; // 2h + + /** + * @param string $json + * @return void + */ + private function tempo(string $json): void + { + $jsonDecoded = Json::decode($json); + + $values = [ + $this->formatFrenchDate('now') => date('Y-m-d'), + 'Demain ' . $this->formatFrenchDate('tomorrow') => date('Y-m-d', strtotime('+1 day')) + ]; + + foreach ($values as $key => $value) { + $item = []; + + $text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]); + $item['uri'] = self::URI . '?season=' . $this->getTempoYear(); + $item['title'] = $text; + $item['author'] = self::MAINTAINER; + $item['content'] = $text; + $item['uid'] = hash('sha256', $item['title']); + + $this->items[] = $item; + } + } + + private function formatFrenchDate(string $datetime): string + { + // Set the locale and date format + $locale = 'fr_FR'; + $formatter = new IntlDateFormatter( + $locale, + IntlDateFormatter::FULL, // Full date format + IntlDateFormatter::NONE, // No time + null, // Default timezone + IntlDateFormatter::GREGORIAN, // Gregorian calendar + 'EEEE dd MMMM yyyy' // Custom pattern + ); + + // Create a DateTime object for the desired date + $now = new DateTime($datetime); + + // Format the date + return $formatter->format($now); + } + + private function getDisplayableColor(?string $color): string + { + $displayableColor = null; + switch ($color) { + case 'BLUE': + $displayableColor = '🟦 TEMPO_BLEU'; + break; + case 'WHITE': + $displayableColor = '⬜ TEMPO_BLANC'; + break; + case 'RED': + $displayableColor = '🟥 TEMPO_ROUGE'; + break; + default: + $displayableColor = '⬛ NON_DEFINI'; + break; + } + + return $displayableColor; + } + + private function getTempoYear(): string + { + $month = date('n'); // Current month as a number (1-12) + $year = date('Y'); // Current year + + // Assuming the tempo year starts in September + if ($month >= 9) { + return $year . '-' . ($year + 1); // e.g., 2024-2025 + } + + return ($year - 1) . '-' . $year; // e.g., 2023-2024 + } + + public function collectData() + { + $contract = $this->getKey('contract'); + + $header = [ + 'Accept: application/json', + ]; + $opts = [ + CURLOPT_HTTPGET => 1, + ]; + + $json = getContents(self::URI . '?season=' . $this->getTempoYear(), $header, $opts); + + if ($contract === 'Tempo') { + $this->tempo($json); + } + } +}