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