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