xref: /webtrees/app/Elements/AgeAtEvent.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Elements;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Tree;
24
25use function in_array;
26use function preg_replace_callback_array;
27use function strtolower;
28use function strtoupper;
29
30/**
31 * AGE_AT_EVENT := {Size=1:12}
32 * [ < | > | <NULL>]
33 * [ YYy MMm DDDd | YYy | MMm | DDDd |
34 * YYy MMm | YYy DDDd | MMm DDDd |
35 * CHILD | INFANT | STILLBORN ]
36 * ]
37 * Where:
38 * >         = greater than indicated age
39 * <         = less than indicated age
40 * y         = a label indicating years
41 * m         = a label indicating months
42 * d         = a label indicating days
43 * YY        = number of full years
44 * MM        = number of months
45 * DDD       = number of days
46 * CHILD     = age < 8 years
47 * INFANT    = age <1year
48 * STILLBORN = died just prior, at, or near birth, 0 years
49 */
50class AgeAtEvent extends AbstractElement
51{
52    protected const MAXIMUM_LENGTH = 12;
53
54    protected const KEYWORDS = ['CHILD', 'INFANT', 'STILLBORN'];
55
56    /**
57     * Convert a value to a canonical form.
58     *
59     * @param string $value
60     *
61     * @return string
62     */
63    public function canonical(string $value): string
64    {
65        // Keywords are upper case.  Ages are lower case
66        $canonical = parent::canonical($value);
67        $upper     = strtoupper($canonical);
68
69        if (in_array($upper, self::KEYWORDS)) {
70            return $upper;
71        }
72
73        return strtolower($canonical);
74    }
75
76    /**
77     * Display the value of this type of element.
78     *
79     * @param string $value
80     * @param Tree   $tree
81     *
82     * @return string
83     */
84    public function value(string $value, Tree $tree): string
85    {
86        $canonical = $this->canonical($value);
87
88        switch ($canonical) {
89            case 'CHILD':
90                return I18N::translate('child');
91
92            case 'INFANT':
93                return I18N::translate('infant');
94
95            case 'STILLBORN':
96                return I18N::translate('stillborn');
97        }
98
99        return preg_replace_callback_array([
100            '/\b(\d+)y\b/' => fn (array $match) => I18N::plural('%s year', '%s years', (int) $match[1], I18N::number((float) $match[1])),
101            '/\b(\d+)m\b/' => fn (array $match) => I18N::plural('%s month', '%s months', (int) $match[1], I18N::number((float) $match[1])),
102            '/\b(\d+)w\b/' => fn (array $match) => I18N::plural('%s week', '%s weeks', (int) $match[1], I18N::number((float) $match[1])),
103            '/\b(\d+)d\b/' => fn (array $match) => I18N::plural('%s day', '%s days', (int) $match[1], I18N::number((float) $match[1])),
104        ], e($canonical));
105    }
106}
107