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\Census; 19 20use Fisharebest\Webtrees\Date; 21use Fisharebest\Webtrees\Family; 22use Fisharebest\Webtrees\Individual; 23 24/** 25 * Definitions for a census column 26 */ 27class AbstractCensusColumn 28{ 29 /** @var CensusInterface */ 30 private $census; 31 32 /** @var string */ 33 private $abbr; 34 35 /** @var string */ 36 private $title; 37 38 /** 39 * Create a column for a census 40 * 41 * @param CensusInterface $census - The census to which this column forms part. 42 * @param string $abbr - The abbrievated on-screen name "BiC" 43 * @param string $title - The full column heading "Born in the county" 44 */ 45 public function __construct(CensusInterface $census, $abbr, $title) 46 { 47 $this->census = $census; 48 $this->abbr = $abbr; 49 $this->title = $title; 50 } 51 52 /** 53 * A short version of the column's name. 54 * 55 * @return string 56 */ 57 public function abbreviation(): string 58 { 59 return $this->abbr; 60 } 61 62 /** 63 * Find the father of an individual 64 * 65 * @param Individual $individual 66 * 67 * @return Individual|null 68 */ 69 public function father(Individual $individual) 70 { 71 $family = $individual->primaryChildFamily(); 72 73 if ($family) { 74 return $family->husband(); 75 } 76 77 return null; 78 } 79 80 /** 81 * Find the mother of an individual 82 * 83 * @param Individual $individual 84 * 85 * @return Individual|null 86 */ 87 public function mother(Individual $individual) 88 { 89 $family = $individual->primaryChildFamily(); 90 91 if ($family) { 92 return $family->wife(); 93 } 94 95 return null; 96 } 97 98 /** 99 * Find the current spouse family of an individual 100 * 101 * @param Individual $individual 102 * 103 * @return Family|null 104 */ 105 public function spouseFamily(Individual $individual) 106 { 107 // Exclude families that were created after this census date 108 $families = []; 109 foreach ($individual->spouseFamilies() as $family) { 110 if (Date::compare($family->getMarriageDate(), $this->date()) <= 0) { 111 $families[] = $family; 112 } 113 } 114 115 if (empty($families)) { 116 return null; 117 } 118 119 usort($families, function (Family $x, Family $y): int { 120 return Date::compare($x->getMarriageDate(), $y->getMarriageDate()); 121 }); 122 123 return end($families); 124 } 125 126 /** 127 * When did this census occur 128 * 129 * @return Date 130 */ 131 public function date(): Date 132 { 133 return new Date($this->census->censusDate()); 134 } 135 136 /** 137 * The full version of the column's name. 138 * 139 * @return string 140 */ 141 public function title(): string 142 { 143 return $this->title; 144 } 145 146 /** 147 * Extract the country (last part) of a place name. 148 * 149 * @param string $place - e.g. "London, England" 150 * 151 * @return string - e.g. "England" 152 */ 153 protected function lastPartOfPlace(string $place): string 154 { 155 $parts = explode(', ', $place); 156 157 return end($parts); 158 } 159 160 /** 161 * Remove the country of a place name, where it is the same as the census place 162 * 163 * @param string $place - e.g. "London, England" 164 * 165 * @return string - e.g. "London" (for census of England) and "London, England" elsewhere 166 */ 167 protected function notCountry(string $place): string 168 { 169 $parts = explode(', ', $place); 170 171 if (end($parts) === $this->place()) { 172 return implode(', ', array_slice($parts, 0, -1)); 173 } 174 175 return $place; 176 } 177 178 /** 179 * Where did this census occur 180 * 181 * @return string 182 */ 183 public function place(): string 184 { 185 return $this->census->censusPlace(); 186 } 187} 188