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