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