100225b98SGreg Roach<?php 23976b470SGreg Roach 300225b98SGreg Roach/** 400225b98SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 600225b98SGreg Roach * This program is free software: you can redistribute it and/or modify 700225b98SGreg Roach * it under the terms of the GNU General Public License as published by 800225b98SGreg Roach * the Free Software Foundation, either version 3 of the License, or 900225b98SGreg Roach * (at your option) any later version. 1000225b98SGreg Roach * This program is distributed in the hope that it will be useful, 1100225b98SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1200225b98SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1300225b98SGreg Roach * GNU General Public License for more details. 1400225b98SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1600225b98SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 1915d603e7SGreg Roach 2000225b98SGreg Roachnamespace Fisharebest\Webtrees\Census; 2100225b98SGreg Roach 22054771e9SGreg Roachuse Fisharebest\Webtrees\Age; 2300225b98SGreg Roachuse Fisharebest\Webtrees\Date; 2400225b98SGreg Roachuse Fisharebest\Webtrees\Individual; 2500225b98SGreg Roach 2600225b98SGreg Roach/** 2700225b98SGreg Roach * Marital status. 2800225b98SGreg Roach */ 29c1010edaSGreg Roachabstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface 30c1010edaSGreg Roach{ 31918e47c5SGreg Roach // Text to display for married males 32918e47c5SGreg Roach protected const HUSBAND = ''; 330a921d1eSGreg Roach 34918e47c5SGreg Roach // Text to display for married females 35918e47c5SGreg Roach protected const WIFE = ''; 3600225b98SGreg Roach 37918e47c5SGreg Roach // Text to display for married unmarried males 38918e47c5SGreg Roach protected const BACHELOR = ''; 390a921d1eSGreg Roach 40918e47c5SGreg Roach // Text to display for married unmarried females 41918e47c5SGreg Roach protected const SPINSTER = ''; 4200225b98SGreg Roach 43918e47c5SGreg Roach // Text to display for male children 44918e47c5SGreg Roach protected const BOY = ''; 450a921d1eSGreg Roach 46918e47c5SGreg Roach // Text to display for female children 47918e47c5SGreg Roach protected const GIRL = ''; 4800225b98SGreg Roach 49918e47c5SGreg Roach // Text to display for divorced males 50918e47c5SGreg Roach protected const DIVORCE = ''; 510a921d1eSGreg Roach 52918e47c5SGreg Roach // Text to display for divorced females 53918e47c5SGreg Roach protected const DIVORCEE = ''; 5400225b98SGreg Roach 55918e47c5SGreg Roach // Text to display for widowed males 56918e47c5SGreg Roach protected const WIDOWER = ''; 570a921d1eSGreg Roach 58918e47c5SGreg Roach // Text to display for widowed females 59918e47c5SGreg Roach protected const WIDOW = ''; 6000225b98SGreg Roach 61918e47c5SGreg Roach // At what age is this individual recorded as an adult 62918e47c5SGreg Roach protected const AGE_ADULT = 15; 6300225b98SGreg Roach 6400225b98SGreg Roach /** 6500225b98SGreg Roach * Generate the likely value of this census column, based on available information. 6600225b98SGreg Roach * 6700225b98SGreg Roach * @param Individual $individual 6815d603e7SGreg Roach * @param Individual $head 6900225b98SGreg Roach * 7000225b98SGreg Roach * @return string 7100225b98SGreg Roach */ 725a62e0a6SGreg Roach public function generate(Individual $individual, Individual $head): string 73c1010edaSGreg Roach { 7400225b98SGreg Roach $family = $this->spouseFamily($individual); 7539ca88baSGreg Roach $sex = $individual->sex(); 7600225b98SGreg Roach 7739ca88baSGreg Roach if ($family === null || $family->facts(['MARR'])->isEmpty()) { 7800225b98SGreg Roach if ($this->isChild($individual)) { 7900225b98SGreg Roach return $this->conditionChild($sex); 80b2ce94c6SRico Sonntag } 81b2ce94c6SRico Sonntag 8200225b98SGreg Roach return $this->conditionSingle($sex); 8300225b98SGreg Roach } 84b2ce94c6SRico Sonntag 8539ca88baSGreg Roach if ($family->facts(['DIV'])->isNotEmpty()) { 8600225b98SGreg Roach return $this->conditionDivorced($sex); 87b2ce94c6SRico Sonntag } 88b2ce94c6SRico Sonntag 8939ca88baSGreg Roach $spouse = $family->spouse($individual); 90e76c0cf0SGreg Roach if ($spouse instanceof Individual && $this->isDead($spouse)) { 91e76c0cf0SGreg Roach return $this->conditionWidowed($sex); 92b2ce94c6SRico Sonntag } 93b2ce94c6SRico Sonntag 9400225b98SGreg Roach return $this->conditionMarried($sex); 9500225b98SGreg Roach } 9600225b98SGreg Roach 9700225b98SGreg Roach /** 9815d603e7SGreg Roach * Is the individual a child. 9915d603e7SGreg Roach * 10015d603e7SGreg Roach * @param Individual $individual 10115d603e7SGreg Roach * 10215d603e7SGreg Roach * @return bool 10315d603e7SGreg Roach */ 1048f53f488SRico Sonntag private function isChild(Individual $individual): bool 105c1010edaSGreg Roach { 106054771e9SGreg Roach $age = new Age($individual->getEstimatedBirthDate(), $this->date()); 10715d603e7SGreg Roach 108054771e9SGreg Roach return $age->ageYears() < static::AGE_ADULT; 10915d603e7SGreg Roach } 11015d603e7SGreg Roach 11115d603e7SGreg Roach /** 11200225b98SGreg Roach * How is this condition written in a census column. 11300225b98SGreg Roach * 11407902ed6SScrutinizer Auto-Fixer * @param string $sex 11500225b98SGreg Roach * 11600225b98SGreg Roach * @return string 11700225b98SGreg Roach */ 118918e47c5SGreg Roach private function conditionChild(string $sex): string 119c1010edaSGreg Roach { 12000225b98SGreg Roach if ($sex === 'F') { 121918e47c5SGreg Roach return static::GIRL; 12200225b98SGreg Roach } 123b2ce94c6SRico Sonntag 124918e47c5SGreg Roach return static::BOY; 12500225b98SGreg Roach } 12600225b98SGreg Roach 12700225b98SGreg Roach /** 12800225b98SGreg Roach * How is this condition written in a census column. 12900225b98SGreg Roach * 13007902ed6SScrutinizer Auto-Fixer * @param string $sex 13100225b98SGreg Roach * 13200225b98SGreg Roach * @return string 13300225b98SGreg Roach */ 134918e47c5SGreg Roach private function conditionSingle(string $sex): string 135c1010edaSGreg Roach { 13600225b98SGreg Roach if ($sex === 'F') { 137918e47c5SGreg Roach return static::SPINSTER; 13800225b98SGreg Roach } 139b2ce94c6SRico Sonntag 140918e47c5SGreg Roach return static::BACHELOR; 14100225b98SGreg Roach } 14200225b98SGreg Roach 14300225b98SGreg Roach /** 144e76c0cf0SGreg Roach * How is this condition written in a census column. 145e76c0cf0SGreg Roach * 14607902ed6SScrutinizer Auto-Fixer * @param string $sex 147e76c0cf0SGreg Roach * 148e76c0cf0SGreg Roach * @return string 149e76c0cf0SGreg Roach */ 150918e47c5SGreg Roach private function conditionDivorced(string $sex): string 151c1010edaSGreg Roach { 152e76c0cf0SGreg Roach if ($sex === 'F') { 153918e47c5SGreg Roach return static::DIVORCEE; 154e76c0cf0SGreg Roach } 155b2ce94c6SRico Sonntag 156918e47c5SGreg Roach return static::DIVORCE; 157e76c0cf0SGreg Roach } 158e76c0cf0SGreg Roach 159e76c0cf0SGreg Roach /** 160e76c0cf0SGreg Roach * Is the individual dead. 161e76c0cf0SGreg Roach * 162e76c0cf0SGreg Roach * @param Individual $individual 163e76c0cf0SGreg Roach * 164e76c0cf0SGreg Roach * @return bool 165e76c0cf0SGreg Roach */ 1668f53f488SRico Sonntag private function isDead(Individual $individual): bool 167c1010edaSGreg Roach { 168fef3019cSGreg Roach return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0; 169e76c0cf0SGreg Roach } 17015d603e7SGreg Roach 17115d603e7SGreg Roach /** 17215d603e7SGreg Roach * How is this condition written in a census column. 17315d603e7SGreg Roach * 17415d603e7SGreg Roach * @param string $sex 17515d603e7SGreg Roach * 17615d603e7SGreg Roach * @return string 17715d603e7SGreg Roach */ 178918e47c5SGreg Roach private function conditionWidowed(string $sex): string 179c1010edaSGreg Roach { 18015d603e7SGreg Roach if ($sex === 'F') { 181918e47c5SGreg Roach return static::WIDOW; 18215d603e7SGreg Roach } 183b2ce94c6SRico Sonntag 184918e47c5SGreg Roach return static::WIDOWER; 18515d603e7SGreg Roach } 18615d603e7SGreg Roach 18715d603e7SGreg Roach /** 18815d603e7SGreg Roach * How is this condition written in a census column. 18915d603e7SGreg Roach * 19015d603e7SGreg Roach * @param string $sex 19115d603e7SGreg Roach * 19215d603e7SGreg Roach * @return string 19315d603e7SGreg Roach */ 194918e47c5SGreg Roach private function conditionMarried(string $sex): string 195c1010edaSGreg Roach { 19615d603e7SGreg Roach if ($sex === 'F') { 197918e47c5SGreg Roach return static::WIFE; 19815d603e7SGreg Roach } 199b2ce94c6SRico Sonntag 200918e47c5SGreg Roach return static::HUSBAND; 20115d603e7SGreg Roach } 20200225b98SGreg Roach} 203