|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MamyRaoby\Phanisana\Converter; |
| 4 | + |
| 5 | +use DateTimeInterface; |
| 6 | +use MamyRaoby\Phanisana\Enum\Dictionary; |
| 7 | +use MamyRaoby\Phanisana\Enum\DateDictionary; |
| 8 | +use MamyRaoby\Phanisana\Exceptions\InvalidTimeFormatException; |
| 9 | +use MamyRaoby\Phanisana\Extensions\DateTime; |
| 10 | + |
| 11 | +class DateTimeConverter |
| 12 | +{ |
| 13 | + public const FORMAT_DATE_LONG_TEXT = 'DATE_LONG_TEXT'; |
| 14 | + public const FORMAT_DATE_MEDIUM_TEXT = 'DATE_MEDIUM_TEXT'; |
| 15 | + public const FORMAT_DATE_TEXT = 'DATE_TEXT'; |
| 16 | + public const FORMAT_TIME_LONG_TEXT = 'TIME_LONG_TEXT'; |
| 17 | + |
| 18 | + public function convertDate(DateTimeInterface $date, string $format): string |
| 19 | + { |
| 20 | + $localeDate = DateTime::createFromInterface($date); |
| 21 | + |
| 22 | + if ($format === self::FORMAT_DATE_LONG_TEXT) { |
| 23 | + $weekday = $localeDate->format('l'); |
| 24 | + $day = phanisana_convert_number($localeDate->format('d')); |
| 25 | + $month = $localeDate->format('F'); |
| 26 | + $year = phanisana_convert_number($localeDate->format('Y')); |
| 27 | + |
| 28 | + return strtolower(implode(' ', [$weekday, $day, $month, DateDictionary::YEAR->value, $year])); |
| 29 | + } |
| 30 | + |
| 31 | + if ($format === self::FORMAT_DATE_MEDIUM_TEXT) { |
| 32 | + $day = phanisana_convert_number($localeDate->format('d')); |
| 33 | + $month = $localeDate->format('F'); |
| 34 | + $year = phanisana_convert_number($localeDate->format('Y')); |
| 35 | + |
| 36 | + return strtolower(implode(' ', [$day, $month, DateDictionary::YEAR->value, $year])); |
| 37 | + } |
| 38 | + |
| 39 | + if ($format === self::FORMAT_DATE_TEXT) { |
| 40 | + $day = phanisana_convert_number($localeDate->format('d')); |
| 41 | + $month = $localeDate->format('F'); |
| 42 | + $year = phanisana_convert_number($localeDate->format('Y')); |
| 43 | + |
| 44 | + return strtolower(implode(' ', [$day, $month, $year])); |
| 45 | + } |
| 46 | + |
| 47 | + return $localeDate->format($format); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @throws \MamyRaoby\Phanisana\Exceptions\InvalidTimeFormatException |
| 52 | + */ |
| 53 | + public function convertTime(DateTimeInterface|string $time, string $format): string |
| 54 | + { |
| 55 | + if ($time instanceof DateTimeInterface) { |
| 56 | + $time = $time->format('H:i:s'); |
| 57 | + } |
| 58 | + |
| 59 | + $pattern = '/^([01]\d|2[0-3]):([0-5]\d)(:[0-5]\d)?$/'; |
| 60 | + |
| 61 | + $isValid = preg_match($pattern, $time) === 1; |
| 62 | + |
| 63 | + if ($isValid) { |
| 64 | + $splittedTime = explode(':', $time); |
| 65 | + $hour = intval($splittedTime[0]); |
| 66 | + $minute = intval($splittedTime[1]); |
| 67 | + $second = intval($splittedTime[2] ?? 0); |
| 68 | + |
| 69 | + $hourSuffix = $this->getHourSuffix($hour); |
| 70 | + |
| 71 | + if ($hour > 12) { |
| 72 | + $hour = $hour - 12; |
| 73 | + } |
| 74 | + |
| 75 | + if ($hour === 0) { |
| 76 | + $hour = 12; |
| 77 | + } |
| 78 | + |
| 79 | + $words = []; |
| 80 | + |
| 81 | + $words[] = phanisana_convert_number($hour); |
| 82 | + $words[] = DateDictionary::HOUR->value; |
| 83 | + $words[] = $hourSuffix; |
| 84 | + |
| 85 | + if ($minute > 0) { |
| 86 | + $words[] = Dictionary::GLUE_SY->value; |
| 87 | + $words[] = phanisana_convert_number($minute); |
| 88 | + $words[] = DateDictionary::MINUTE->value; |
| 89 | + } |
| 90 | + |
| 91 | + if ($second > 0) { |
| 92 | + $words[] = Dictionary::GLUE_SY->value; |
| 93 | + $words[] = phanisana_convert_number($second); |
| 94 | + $words[] = DateDictionary::SECOND->value; |
| 95 | + } |
| 96 | + |
| 97 | + return trim(preg_replace('/\s+/', ' ', implode(' ', $words))); |
| 98 | + } |
| 99 | + |
| 100 | + throw new InvalidTimeFormatException(); |
| 101 | + } |
| 102 | + |
| 103 | + public function convertDateTime(DateTimeInterface $datetime, string $dateFormat, string $timeFormat): string |
| 104 | + { |
| 105 | + $date = $this->convertDate($datetime, $dateFormat); |
| 106 | + $time = $this->convertTime($datetime, $timeFormat); |
| 107 | + |
| 108 | + return $date . DateDictionary::GLUE_AT->value . $time; |
| 109 | + } |
| 110 | + |
| 111 | + protected function getHourSuffix(int $hour): string |
| 112 | + { |
| 113 | + $suffix = ''; |
| 114 | + |
| 115 | + if (in_array($hour, range(1, 10))) { |
| 116 | + $suffix = DateDictionary::MORNING->value; |
| 117 | + } |
| 118 | + |
| 119 | + if (in_array($hour, range(11, 13))) { |
| 120 | + $suffix = DateDictionary::MIDDAY->value; |
| 121 | + } |
| 122 | + |
| 123 | + if (in_array($hour, range(14, 16))) { |
| 124 | + $suffix = DateDictionary::AFTERNOON->value; |
| 125 | + } |
| 126 | + |
| 127 | + if (in_array($hour, range(17, 23))) { |
| 128 | + $suffix = DateDictionary::EVENING->value; |
| 129 | + } |
| 130 | + |
| 131 | + if ($hour === 0) { |
| 132 | + $suffix = DateDictionary::NIGHT->value; |
| 133 | + } |
| 134 | + |
| 135 | + return $suffix; |
| 136 | + } |
| 137 | +} |
0 commit comments