1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Factories; 21 22use DomainException; 23use Fisharebest\Webtrees\Contracts\CalendarDateFactoryInterface; 24use Fisharebest\Webtrees\Date\AbstractCalendarDate; 25use Fisharebest\Webtrees\Date\FrenchDate; 26use Fisharebest\Webtrees\Date\GregorianDate; 27use Fisharebest\Webtrees\Date\HijriDate; 28use Fisharebest\Webtrees\Date\JalaliDate; 29use Fisharebest\Webtrees\Date\JewishDate; 30use Fisharebest\Webtrees\Date\JulianDate; 31use Fisharebest\Webtrees\Date\RomanDate; 32use Fisharebest\Webtrees\I18N; 33 34/** 35 * Create a calendar date object. 36 */ 37class CalendarDateFactory implements CalendarDateFactoryInterface 38{ 39 /** 40 * Parse a string containing a calendar date. 41 * 42 * @param string $date 43 * 44 * @return AbstractCalendarDate|null 45 */ 46 public function make(string $date): ?AbstractCalendarDate { 47 // Valid calendar escape specified? - use it 48 if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) { 49 $cal = $match[1]; 50 $date = $match[2]; 51 } else { 52 $cal = ''; 53 } 54 // A date with a month: DM, M, MY or DMY 55 if (preg_match('/^(\d?\d?) ?(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN) ?((?:\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)?)$/', $date, $match)) { 56 $d = $match[1]; 57 $m = $match[2]; 58 $y = $match[3]; 59 } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) { 60 // A date with just a year 61 $d = ''; 62 $m = ''; 63 $y = $match[1]; 64 } else { 65 // An invalid date - do the best we can. 66 $d = ''; 67 $m = ''; 68 $y = ''; 69 // Look for a 3/4 digit year anywhere in the date 70 if (preg_match('/\b(\d{3,4})\b/', $date, $match)) { 71 $y = $match[1]; 72 } 73 // Look for a month anywhere in the date 74 if (preg_match('/(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL|VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP|MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH|FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)/', $date, $match)) { 75 $m = $match[1]; 76 // Look for a day number anywhere in the date 77 if (preg_match('/\b(\d\d?)\b/', $date, $match)) { 78 $d = $match[1]; 79 } 80 } 81 } 82 83 // Unambiguous dates - override calendar escape 84 if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) { 85 $cal = JewishDate::ESCAPE; 86 } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) { 87 $cal = FrenchDate::ESCAPE; 88 } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) { 89 $cal = HijriDate::ESCAPE; // This is a WT extension 90 } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) { 91 $cal = JalaliDate::ESCAPE; // This is a WT extension 92 } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) { 93 $cal = JulianDate::ESCAPE; 94 } 95 96 // Ambiguous dates - don't override calendar escape 97 if ($cal === '') { 98 if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) { 99 $cal = GregorianDate::ESCAPE; 100 } elseif (preg_match('/^[345]\d\d\d$/', $y)) { 101 // Year 3000-5999 102 $cal = JewishDate::ESCAPE; 103 } else { 104 $cal = GregorianDate::ESCAPE; 105 } 106 } 107 108 // Now construct an object of the correct type 109 switch ($cal) { 110 case GregorianDate::ESCAPE: 111 return new GregorianDate([$y, $m, $d]); 112 113 case JulianDate::ESCAPE: 114 return new JulianDate([$y, $m, $d]); 115 116 case JewishDate::ESCAPE: 117 return new JewishDate([$y, $m, $d]); 118 119 case HijriDate::ESCAPE: 120 return new HijriDate([$y, $m, $d]); 121 122 case FrenchDate::ESCAPE: 123 return new FrenchDate([$y, $m, $d]); 124 125 case JalaliDate::ESCAPE: 126 return new JalaliDate([$y, $m, $d]); 127 128 case RomanDate::ESCAPE: 129 return new RomanDate([$y, $m, $d]); 130 131 default: 132 throw new DomainException('Invalid calendar'); 133 } 134 } 135 136 /** 137 * A list of supported calendars and their names. 138 * 139 * @return array<string,string> 140 */ 141 public function supportedCalendars(): array 142 { 143 return [ 144 /* I18N: The gregorian calendar */ 145 'gregorian' => I18N::translate('Gregorian'), 146 /* I18N: The julian calendar */ 147 'julian' => I18N::translate('Julian'), 148 /* I18N: The French calendar */ 149 'french' => I18N::translate('French'), 150 /* I18N: The Hebrew/Jewish calendar */ 151 'jewish' => I18N::translate('Jewish'), 152 /* I18N: The Arabic/Hijri calendar */ 153 'hijri' => I18N::translate('Hijri'), 154 /* I18N: The Persian/Jalali calendar */ 155 'jalali' => I18N::translate('Jalali'), 156 ]; 157 } 158} 159