xref: /webtrees/app/Census/AbstractCensusColumn.php (revision cf891017dd40a0de9e99bfa64db680a95043750a)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Census;
21
22use Fisharebest\Webtrees\Date;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Individual;
25
26use function array_slice;
27use function explode;
28use function implode;
29
30/**
31 * Definitions for a census column
32 */
33class AbstractCensusColumn
34{
35    private CensusInterface $census;
36
37    private string $abbr;
38
39    private string $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->childFamilies()->first();
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->childFamilies()->first();
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     * What was an individual's likely name on a given date, allowing
121     * for marriages and married names.
122     *
123     * @param Individual $individual
124     *
125     * @return array<string>
126     */
127    protected function nameAtCensusDate(Individual $individual): array
128    {
129        $names  = $individual->getAllNames();
130        $name   = $names[0];
131        $family = $this->spouseFamily($individual);
132
133        if ($family instanceof Family) {
134            $spouse = $family->spouse($individual);
135
136            if ($spouse instanceof Individual) {
137                foreach ($family->facts(['MARR']) as $marriage) {
138                    if ($marriage->date()->isOK()) {
139                        foreach ($names as $individual_name) {
140                            foreach ($spouse->getAllNames() as $spouse_name) {
141                                if ($individual_name['type'] === '_MARNM' && $individual_name['surn'] === $spouse_name['surn']) {
142                                    return $individual_name;
143                                }
144                            }
145                        }
146                    }
147                }
148            }
149        }
150
151        return $name;
152    }
153
154    /**
155     * When did this census occur
156     *
157     * @return Date
158     */
159    public function date(): Date
160    {
161        return new Date($this->census->censusDate());
162    }
163
164    /**
165     * The full version of the column's name.
166     *
167     * @return string
168     */
169    public function title(): string
170    {
171        return $this->title;
172    }
173
174    /**
175     * Extract the country (last part) of a place name.
176     *
177     * @param string $place e.g. "London, England"
178     *
179     * @return string e.g. "England"
180     */
181    protected function lastPartOfPlace(string $place): string
182    {
183        $parts = explode(', ', $place);
184
185        return end($parts);
186    }
187
188    /**
189     * Remove the country of a place name, where it is the same as the census place
190     *
191     * @param string $place e.g. "London, England"
192     *
193     * @return string e.g. "London" (for census of England) and "London, England" elsewhere
194     */
195    protected function notCountry(string $place): string
196    {
197        $parts = explode(', ', $place);
198
199        if (end($parts) === $this->place()) {
200            return implode(', ', array_slice($parts, 0, -1));
201        }
202
203        return $place;
204    }
205
206    /**
207     * Where did this census occur
208     *
209     * @return string
210     */
211    public function place(): string
212    {
213        return $this->census->censusPlace();
214    }
215}
216