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