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