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