xref: /webtrees/app/Age.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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;
21
22use function view;
23
24/**
25 * The difference between two GEDCOM dates.
26 */
27class Age
28{
29    private int $years;
30
31    private int $months;
32
33    private int $days;
34
35    private int $total_days;
36
37    private bool $is_exact;
38
39    private bool $is_valid;
40
41    /**
42     * @param Date $x The first date
43     * @param Date $y The second date
44     */
45    public function __construct(Date $x, Date $y)
46    {
47        // If the dates are ranges, use the start/end calendar dates.
48        $start = $x->minimumDate();
49        $end   = $y->maximumDate();
50
51        [$this->years, $this->months, $this->days] = $start->ageDifference($end);
52
53        $this->total_days = $end->minimumJulianDay() - $start->minimumJulianDay();
54
55        // Use the same precision as found in the dates.
56        if ($start->day() === 0 || $end->day() === 0) {
57            $this->days = 0;
58        }
59
60        if ($start->month() === 0 || $end->month() === 0) {
61            $this->months = 0;
62        }
63
64        // Are the dates exact?
65        $this->is_exact = $start->day() !== 0 && $end->day() !== 0;
66
67        // Are the dates valid?
68        $this->is_valid = $x->isOK() && $y->isOK();
69    }
70
71    /**
72     * Show an age in a human-friendly form, such as "34 years", "8 months", "20 days".
73     * Show an empty string for invalid/missing dates.
74     * Show a warning icon for negative ages.
75     * Show zero ages without any units.
76     *
77     * @return string
78     */
79    public function __toString(): string
80    {
81        if (!$this->is_valid) {
82            return '';
83        }
84
85        if ($this->years < 0) {
86            return view('icons/warning');
87        }
88
89        if ($this->years > 0) {
90            return I18N::plural('%s year', '%s years', $this->years, I18N::number($this->years));
91        }
92
93        if ($this->months > 0) {
94            return I18N::plural('%s month', '%s months', $this->months, I18N::number($this->months));
95        }
96
97        if ($this->days > 0 || $this->is_exact) {
98            return I18N::plural('%s day', '%s days', $this->days, I18N::number($this->days));
99        }
100
101        return I18N::number(0);
102    }
103
104    /**
105     * How many days between two events?
106     * If either date is invalid return -1.
107     *
108     * @return int
109     */
110    public function ageDays(): int
111    {
112        if ($this->is_valid) {
113            return $this->total_days;
114        }
115
116        return -1;
117    }
118
119    /**
120     * How many years between two events?
121     * Return -1 for invalid or reversed dates.
122     *
123     * @return int
124     */
125    public function ageYears(): int
126    {
127        if ($this->is_valid) {
128            return $this->years;
129        }
130
131        return -1;
132    }
133
134    /**
135     * How many years between two events?
136     * If either date is invalid return -1.
137     *
138     * @return string
139     */
140    public function ageYearsString(): string
141    {
142        if (!$this->is_valid) {
143            return '';
144        }
145
146        if ($this->years < 0) {
147            return view('icons/warning');
148        }
149
150        return I18N::number($this->years);
151    }
152}
153