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