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