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