summaryrefslogtreecommitdiff
path: root/app/Age.php
blob: 400fc6e5f15cdc7966f3ba01c112d643976b43b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2019 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
declare(strict_types=1);

namespace Fisharebest\Webtrees;

use function implode;
use function preg_match;
use function strtolower;
use function substr;
use function trim;

/**
 * Representation of a GEDCOM age.
 *
 * Ages may be a keyword (stillborn, infant, child) or a number of years,
 * months and/or days such as "6y 3m".
 */
class Age
{
    // GEDCOM keyword: died just prior, at, or near birth, 0 years
    private const KEYWORD_STILLBORN = 'stillborn';

    // GEDCOM keyword: age < 1 year
    private const KEYWORD_INFANT = 'infant';

    // GEDCOM keyword: age < 8 years
    private const KEYWORD_CHILD = 'child';

    // GEDCOM symbol: aged less than
    private const SYMBOL_LESS_THAN = '<';

    // GEDCOM symbol: aged more than
    private const SYMBOL_MORE_THAN = '>';

    // GEDCOM symbol: number of years
    private const SYMBOL_YEARS = 'y';

    // GEDCOM symbol: number of months
    private const SYMBOL_MONTHS = 'm';

    // GEDCOM symbol: number of weeks (this is a non-standard extension)
    private const SYMBOL_WEEKS = 'w';

    // GEDCOM symbol: number of days
    private const SYMBOL_DAYS = 'd';

    /** @var string */
    private $keyword = '';

    /** @var string */
    private $qualifier = '';

    /** @var int */
    private $years = 0;

    /** @var int */
    private $months = 0;

    /** @var int */
    private $weeks = 0;

    /** @var int */
    private $days = 0;

    /**
     * Age constructor.
     *
     * @param string $age
     */
    public function __construct(string $age)
    {
        $age = strtolower(trim($age));

        // Keywords
        if ($age === self::KEYWORD_STILLBORN || $age === self::KEYWORD_INFANT || $age === self::KEYWORD_CHILD) {
            $this->keyword = $age;

            return;
        }

        // Qualifier
        $qualifier = substr($age, 0, 1);

        if ($qualifier === self::SYMBOL_LESS_THAN || $qualifier === self::SYMBOL_MORE_THAN) {
            $this->qualifier = $qualifier;
        }

        // Number of years, months, weeks and days.
        $this->years  = $this->extractNumber($age, self::SYMBOL_YEARS);
        $this->months = $this->extractNumber($age, self::SYMBOL_MONTHS);
        $this->weeks  = $this->extractNumber($age, self::SYMBOL_WEEKS);
        $this->days   = $this->extractNumber($age, self::SYMBOL_DAYS);
    }

    /**
     * Convert an age to localised text.
     */
    public function asText(): string
    {
        if ($this->keyword === self::KEYWORD_STILLBORN) {
            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (stillborn)
            return I18N::translate('(stillborn)');
        }

        if ($this->keyword === self::KEYWORD_INFANT) {
            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (in infancy)
            return I18N::translate('(in infancy)');
        }

        if ($this->keyword === self::KEYWORD_CHILD) {
            // I18N: An individual’s age at an event. e.g. Died 14 Jan 1900 (in childhood)
            return I18N::translate('(in childhood)');
        }

        $age = [];

        // Show a zero age as "0 years", not "0 days"
        if ($this->years > 0 || $this->months === 0 && $this->weeks === 0 && $this->days === 0) {
            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
            $age[] = I18N::plural('%s year', '%s years', $this->years, I18N::number($this->years));
        }

        if ($this->months > 0) {
            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
            $age[] = I18N::plural('%s month', '%s months', $this->months, I18N::number($this->months));
        }

        if ($this->weeks > 0) {
            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
            $age[] = I18N::plural('%s week', '%s weeks', $this->weeks, I18N::number($this->weeks));
        }

        if ($this->days > 0) {
            // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
            $age[] = I18N::plural('%s day', '%s days', $this->days, I18N::number($this->days));
        }

        // If an age is just a number of years, only show the number
        if ($this->years > 0 && $this->months === 0 && $this->weeks === 0 && $this->days === 0) {
            $age = [I18N::number($this->years)];
        }

        $age_string = implode(I18N::$list_separator, $age);

        if ($this->qualifier === self::SYMBOL_LESS_THAN) {
            // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged less than 21 years)
            return I18N::translate('(aged less than %s)', $age_string);
        }

        if ($this->qualifier === self::SYMBOL_MORE_THAN) {
            // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged more than 21 years)
            return I18N::translate('(aged more than %s)', $age_string);
        }

        // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged 43 years)
        return I18N::translate('(aged %s)', $age_string);
    }

    /**
     * Extract a number of days/weeks/months/years from the age string.
     *
     * @param string $age
     * @param string $suffix
     *
     * @return int
     */
    private function extractNumber(string $age, string $suffix): int
    {
        if (preg_match('/(\d+) *' . $suffix . '/', $age, $match)) {
            return (int) $match[1];
        }

        return 0;
    }
}