xref: /webtrees/app/Age.php (revision 696755093df43b466490c9156517a3ffb2d391a2)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 view;
23
24/**
25 * The difference between two GEDCOM dates.
26 */
27class Age
28{
29    /** @var int */
30    private $years;
31
32    /** @var int */
33    private $months;
34
35    /** @var int */
36    private $days;
37
38    /** @var int */
39    private $total_days;
40
41    /** @var bool */
42    private $is_exact;
43
44    /** @var bool */
45    private $is_valid;
46
47    /**
48     * Age constructor.
49     *
50     * @param Date $x - The first date
51     * @param Date $y - The second date
52     */
53    public function __construct(Date $x, Date $y)
54    {
55        // If the dates are ranges, use the start/end calendar dates.
56        $start = $x->minimumDate();
57        $end   = $y->maximumDate();
58
59        [$this->years, $this->months, $this->days] = $start->ageDifference($end);
60
61        $this->total_days = $end->minimumJulianDay() - $start->minimumJulianDay();
62
63        // Use the same precision as found in the dates.
64        if ($start->day() === 0 || $end->day() === 0) {
65            $this->days = 0;
66        }
67
68        if ($start->month() === 0 || $end->month() === 0) {
69            $this->months = 0;
70        }
71
72        // Are the dates exact?
73        $this->is_exact = $start->day() !== 0 && $end->day() !== 0;
74
75        // Are the dates valid?
76        $this->is_valid = $x->isOK() && $y->isOK();
77    }
78
79    /**
80     * Show an age in a human-friendly form, such as "34 years", "8 months", "20 days".
81     * Show an empty string for invalid/missing dates.
82     * Show a warning icon for negative ages.
83     * Show zero ages without any units.
84     *
85     * @return string
86     */
87    public function ageString(): string
88    {
89        if (!$this->is_valid) {
90            return '';
91        }
92
93        if ($this->years < 0) {
94            return view('icons/warning');
95        }
96
97        if ($this->years > 0) {
98            return I18N::plural('%s year', '%s years', $this->years, I18N::number($this->years));
99        }
100
101        if ($this->months > 0) {
102            return I18N::plural('%s month', '%s months', $this->months, I18N::number($this->months));
103        }
104
105        if ($this->days > 0 || $this->is_exact) {
106            return I18N::plural('%s day', '%s days', $this->days, I18N::number($this->days));
107        }
108
109        return I18N::number(0);
110    }
111
112    /**
113     * How many days between two events?
114     * If either date is invalid return -1.
115     *
116     * @return int
117     */
118    public function ageDays(): int
119    {
120        if ($this->is_valid) {
121            return $this->total_days;
122        }
123
124        return -1;
125    }
126
127    /**
128     * How many years between two events?
129     * Return -1 for invalid or reversed dates.
130     *
131     * @return int
132     */
133    public function ageYears(): int
134    {
135        if ($this->is_valid) {
136            return $this->years;
137        }
138
139        return -1;
140    }
141
142    /**
143     * How many years between two events?
144     * If either date is invalid return -1.
145     *
146     * @return string
147     */
148    public function ageYearsString(): string
149    {
150        if (!$this->is_valid) {
151            return '';
152        }
153
154        if ($this->years < 0) {
155            return view('icons/warning');
156        }
157
158
159        return I18N::number($this->years);
160    }
161
162    /**
163     * @param bool $living
164     *
165     * @return string
166     */
167    public function ageAtEvent(bool $living): string
168    {
169        $age = $this->ageString();
170
171        if ($age === '') {
172            return '';
173        }
174
175        if ($living) {
176            /* I18N: The current age of a living individual */
177            return I18N::translate('(age %s)', $age);
178        }
179
180        /* I18N: The age of an individual at a given date */
181        return I18N::translate('(aged %s)', $age);
182    }
183
184    /**
185     * Similar to ageAtEvent, but for events such as burial, cremation, etc.
186     *
187     * @return string
188     */
189    public function timeAfterDeath(): string
190    {
191        if (!$this->is_valid) {
192            return '';
193        }
194
195        if ($this->years === 0 && $this->months === 0 && $this->days === 0) {
196            if ($this->is_exact) {
197                return I18N::translate('(on the date of death)');
198            }
199
200            return '';
201        }
202
203        return I18N::translate('(%s after death)', $this->ageString());
204    }
205}
206