xref: /webtrees/app/Age.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use function implode;
23use function preg_match;
24use function strtolower;
25use function substr;
26use function trim;
27
28/**
29 * Representation of a GEDCOM age.
30 *
31 * Ages may be a keyword (stillborn, infant, child) or a number of years,
32 * months and/or days such as "6y 3m".
33 */
34class Age
35{
36    // GEDCOM keyword: died just prior, at, or near birth, 0 years
37    private const KEYWORD_STILLBORN = 'stillborn';
38
39    // GEDCOM keyword: age < 1 year
40    private const KEYWORD_INFANT = 'infant';
41
42    // GEDCOM keyword: age < 8 years
43    private const KEYWORD_CHILD = 'child';
44
45    // GEDCOM symbol: aged less than
46    private const SYMBOL_LESS_THAN = '<';
47
48    // GEDCOM symbol: aged more than
49    private const SYMBOL_MORE_THAN = '>';
50
51    // GEDCOM symbol: number of years
52    private const SYMBOL_YEARS = 'y';
53
54    // GEDCOM symbol: number of months
55    private const SYMBOL_MONTHS = 'm';
56
57    // GEDCOM symbol: number of weeks (this is a non-standard extension)
58    private const SYMBOL_WEEKS = 'w';
59
60    // GEDCOM symbol: number of days
61    private const SYMBOL_DAYS = 'd';
62
63    /** @var string */
64    private $keyword = '';
65
66    /** @var string */
67    private $qualifier = '';
68
69    /** @var int */
70    private $years = 0;
71
72    /** @var int */
73    private $months = 0;
74
75    /** @var int */
76    private $weeks = 0;
77
78    /** @var int */
79    private $days = 0;
80
81    /**
82     * Age constructor.
83     *
84     * @param string $age
85     */
86    public function __construct(string $age)
87    {
88        $age = strtolower(trim($age));
89
90        // Keywords
91        if ($age === self::KEYWORD_STILLBORN || $age === self::KEYWORD_INFANT || $age === self::KEYWORD_CHILD) {
92            $this->keyword = $age;
93
94            return;
95        }
96
97        // Qualifier
98        $qualifier = substr($age, 0, 1);
99
100        if ($qualifier === self::SYMBOL_LESS_THAN || $qualifier === self::SYMBOL_MORE_THAN) {
101            $this->qualifier = $qualifier;
102        }
103
104        // Number of years, months, weeks and days.
105        $this->years  = $this->extractNumber($age, self::SYMBOL_YEARS);
106        $this->months = $this->extractNumber($age, self::SYMBOL_MONTHS);
107        $this->weeks  = $this->extractNumber($age, self::SYMBOL_WEEKS);
108        $this->days   = $this->extractNumber($age, self::SYMBOL_DAYS);
109    }
110
111    /**
112     * Convert an age to localised text.
113     */
114    public function asText(): string
115    {
116        if ($this->keyword === self::KEYWORD_STILLBORN) {
117            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (stillborn)
118            return I18N::translate('(stillborn)');
119        }
120
121        if ($this->keyword === self::KEYWORD_INFANT) {
122            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (in infancy)
123            return I18N::translate('(in infancy)');
124        }
125
126        if ($this->keyword === self::KEYWORD_CHILD) {
127            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (in childhood)
128            return I18N::translate('(in childhood)');
129        }
130
131        $age = [];
132
133        // Show a zero age as "0 years", not "0 days"
134        if ($this->years > 0 || $this->months === 0 && $this->weeks === 0 && $this->days === 0) {
135            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
136            $age[] = I18N::plural('%s year', '%s years', $this->years, I18N::number($this->years));
137        }
138
139        if ($this->months > 0) {
140            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
141            $age[] = I18N::plural('%s month', '%s months', $this->months, I18N::number($this->months));
142        }
143
144        if ($this->weeks > 0) {
145            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
146            $age[] = I18N::plural('%s week', '%s weeks', $this->weeks, I18N::number($this->weeks));
147        }
148
149        if ($this->days > 0) {
150            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
151            $age[] = I18N::plural('%s day', '%s days', $this->days, I18N::number($this->days));
152        }
153
154        // If an age is just a number of years, only show the number
155        if ($this->years > 0 && $this->months === 0 && $this->weeks === 0 && $this->days === 0) {
156            $age = [I18N::number($this->years)];
157        }
158
159        $age_string = implode(I18N::$list_separator, $age);
160
161        if ($this->qualifier === self::SYMBOL_LESS_THAN) {
162            // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged less than 21 years)
163            return I18N::translate('(aged less than %s)', $age_string);
164        }
165
166        if ($this->qualifier === self::SYMBOL_MORE_THAN) {
167            // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged more than 21 years)
168            return I18N::translate('(aged more than %s)', $age_string);
169        }
170
171        // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged 43 years)
172        return I18N::translate('(aged %s)', $age_string);
173    }
174
175    /**
176     * Extract a number of days/weeks/months/years from the age string.
177     *
178     * @param string $age
179     * @param string $suffix
180     *
181     * @return int
182     */
183    private function extractNumber(string $age, string $suffix): int
184    {
185        if (preg_match('/(\d+) *' . $suffix . '/', $age, $match)) {
186            return (int) $match[1];
187        }
188
189        return 0;
190    }
191}
192