xref: /webtrees/app/Factories/CalendarDateFactory.php (revision 2c685d76b4ab2ea8f166ab8a2e4112fa1ba8de5d)
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
45     */
46    public function make(string $date): AbstractCalendarDate
47    {
48        // Valid calendar escape specified? - use it
49        if (preg_match('/^(@#D(?:GREGORIAN|JULIAN|HEBREW|HIJRI|JALALI|FRENCH R|ROMAN)+@) ?(.*)/', $date, $match)) {
50            $cal  = $match[1];
51            $date = $match[2];
52        } else {
53            $cal = '';
54        }
55        // A date with a month: DM, M, MY or DMY
56        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)) {
57            $d = $match[1];
58            $m = $match[2];
59            $y = $match[3];
60        } elseif (preg_match('/^(\d{1,4}(?: B\.C\.)?|\d\d\d\d\/\d\d)$/', $date, $match)) {
61            // A date with just a year
62            $d = '';
63            $m = '';
64            $y = $match[1];
65        } else {
66            // An invalid date - do the best we can.
67            $d = '';
68            $m = '';
69            $y = '';
70            // Look for a 3/4 digit year anywhere in the date
71            if (preg_match('/(\d{3,4})/', $date, $match)) {
72                $y = $match[1];
73            }
74            // Look for a month anywhere in the date
75            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)) {
76                $m = $match[1];
77                // Look for a day number anywhere in the date
78                if (preg_match('/\b(\d\d?)\b/', $date, $match)) {
79                    $d = $match[1];
80                }
81            }
82        }
83
84        // Unambiguous dates - override calendar escape
85        if (preg_match('/^(TSH|CSH|KSL|TVT|SHV|ADR|ADS|NSN|IYR|SVN|TMZ|AAV|ELL)$/', $m)) {
86            $cal = JewishDate::ESCAPE;
87        } elseif (preg_match('/^(VEND|BRUM|FRIM|NIVO|PLUV|VENT|GERM|FLOR|PRAI|MESS|THER|FRUC|COMP)$/', $m)) {
88            $cal = FrenchDate::ESCAPE;
89        } elseif (preg_match('/^(MUHAR|SAFAR|RABI[AT]|JUMA[AT]|RAJAB|SHAAB|RAMAD|SHAWW|DHUAQ|DHUAH)$/', $m)) {
90            $cal = HijriDate::ESCAPE; // This is a WT extension
91        } elseif (preg_match('/^(FARVA|ORDIB|KHORD|TIR|MORDA|SHAHR|MEHR|ABAN|AZAR|DEY|BAHMA|ESFAN)$/', $m)) {
92            $cal = JalaliDate::ESCAPE; // This is a WT extension
93        } elseif (preg_match('/^\d{1,4}( B\.C\.)|\d\d\d\d\/\d\d$/', $y)) {
94            $cal = JulianDate::ESCAPE;
95        }
96
97        // Ambiguous dates - don't override calendar escape
98        if ($cal === '') {
99            if (preg_match('/^(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)$/', $m)) {
100                $cal = GregorianDate::ESCAPE;
101            } elseif (preg_match('/^[345]\d\d\d$/', $y)) {
102                // Year 3000-5999
103                $cal = JewishDate::ESCAPE;
104            } else {
105                $cal = GregorianDate::ESCAPE;
106            }
107        }
108
109        // Now construct an object of the correct type
110        switch ($cal) {
111            case GregorianDate::ESCAPE:
112                return new GregorianDate([$y, $m, $d]);
113
114            case JulianDate::ESCAPE:
115                return new JulianDate([$y, $m, $d]);
116
117            case JewishDate::ESCAPE:
118                return new JewishDate([$y, $m, $d]);
119
120            case HijriDate::ESCAPE:
121                return new HijriDate([$y, $m, $d]);
122
123            case FrenchDate::ESCAPE:
124                return new FrenchDate([$y, $m, $d]);
125
126            case JalaliDate::ESCAPE:
127                return new JalaliDate([$y, $m, $d]);
128
129            case RomanDate::ESCAPE:
130                return new RomanDate([$y, $m, $d]);
131
132            default:
133                throw new DomainException('Invalid calendar');
134        }
135    }
136
137    /**
138     * A list of supported calendars and their names.
139     *
140     * @return array<string,string>
141     */
142    public function supportedCalendars(): array
143    {
144        return [
145            /* I18N: The gregorian calendar */
146            'gregorian' => I18N::translate('Gregorian'),
147            /* I18N: The julian calendar */
148            'julian'    => I18N::translate('Julian'),
149            /* I18N: The French calendar */
150            'french'    => I18N::translate('French'),
151            /* I18N: The Hebrew/Jewish calendar */
152            'jewish'    => I18N::translate('Jewish'),
153            /* I18N: The Arabic/Hijri calendar */
154            'hijri'     => I18N::translate('Hijri'),
155            /* I18N: The Persian/Jalali calendar */
156            'jalali'    => I18N::translate('Jalali'),
157        ];
158    }
159}
160