xref: /webtrees/app/Census/AbstractCensusColumn.php (revision 24f2a3af38709f9bf0a739b30264240d20ba34e8)
14ccf2a72SGreg Roach<?php
23976b470SGreg Roach
34ccf2a72SGreg Roach/**
44ccf2a72SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
64ccf2a72SGreg Roach * This program is free software: you can redistribute it and/or modify
74ccf2a72SGreg Roach * it under the terms of the GNU General Public License as published by
84ccf2a72SGreg Roach * the Free Software Foundation, either version 3 of the License, or
94ccf2a72SGreg Roach * (at your option) any later version.
104ccf2a72SGreg Roach * This program is distributed in the hope that it will be useful,
114ccf2a72SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124ccf2a72SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134ccf2a72SGreg Roach * GNU General Public License for more details.
144ccf2a72SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164ccf2a72SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
1915d603e7SGreg Roach
204ccf2a72SGreg Roachnamespace Fisharebest\Webtrees\Census;
214ccf2a72SGreg Roach
224ccf2a72SGreg Roachuse Fisharebest\Webtrees\Date;
23b26116c9SGreg Roachuse Fisharebest\Webtrees\Family;
2440150762SGreg Roachuse Fisharebest\Webtrees\Individual;
253976b470SGreg Roach
264ecdc95fSGreg Roachuse function array_slice;
274ecdc95fSGreg Roachuse function explode;
284ecdc95fSGreg Roachuse function implode;
294ccf2a72SGreg Roach
304ccf2a72SGreg Roach/**
314ccf2a72SGreg Roach * Definitions for a census column
324ccf2a72SGreg Roach */
33c1010edaSGreg Roachclass AbstractCensusColumn
34c1010edaSGreg Roach{
35ef21b467SGreg Roach    /** @var CensusInterface */
36ef21b467SGreg Roach    private $census;
37ef21b467SGreg Roach
38ef21b467SGreg Roach    /** @var string */
39ef21b467SGreg Roach    private $abbr;
40ef21b467SGreg Roach
41ef21b467SGreg Roach    /** @var string */
42ef21b467SGreg Roach    private $title;
434ccf2a72SGreg Roach
444ccf2a72SGreg Roach    /**
45db7d25eeSGreg Roach     * Create a column for a census
464ccf2a72SGreg Roach     *
47ef21b467SGreg Roach     * @param CensusInterface $census - The census to which this column forms part.
484ecdc95fSGreg Roach     * @param string          $abbr   - The abbreviated on-screen name "BiC"
49ef21b467SGreg Roach     * @param string          $title  - The full column heading "Born in the county"
504ccf2a72SGreg Roach     */
514ecdc95fSGreg Roach    public function __construct(CensusInterface $census, string $abbr, string $title)
52c1010edaSGreg Roach    {
53db7d25eeSGreg Roach        $this->census = $census;
54ef21b467SGreg Roach        $this->abbr   = $abbr;
55ef21b467SGreg Roach        $this->title  = $title;
56ef21b467SGreg Roach    }
57ef21b467SGreg Roach
58ef21b467SGreg Roach    /**
59ef21b467SGreg Roach     * A short version of the column's name.
60ef21b467SGreg Roach     *
61ef21b467SGreg Roach     * @return string
62ef21b467SGreg Roach     */
638f53f488SRico Sonntag    public function abbreviation(): string
64c1010edaSGreg Roach    {
65ef21b467SGreg Roach        return $this->abbr;
66db7d25eeSGreg Roach    }
67db7d25eeSGreg Roach
68db7d25eeSGreg Roach    /**
6940150762SGreg Roach     * Find the father of an individual
7040150762SGreg Roach     *
71b26116c9SGreg Roach     * @param Individual $individual
72b26116c9SGreg Roach     *
73b26116c9SGreg Roach     * @return Individual|null
7440150762SGreg Roach     */
75e364afe4SGreg Roach    public function father(Individual $individual): ?Individual
76c1010edaSGreg Roach    {
771afbbc50SGreg Roach        $family = $individual->childFamilies()->first();
7840150762SGreg Roach
794ecdc95fSGreg Roach        if ($family instanceof Family) {
8039ca88baSGreg Roach            return $family->husband();
8140150762SGreg Roach        }
82b2ce94c6SRico Sonntag
83b2ce94c6SRico Sonntag        return null;
8440150762SGreg Roach    }
8540150762SGreg Roach
8640150762SGreg Roach    /**
8740150762SGreg Roach     * Find the mother of an individual
8840150762SGreg Roach     *
89b26116c9SGreg Roach     * @param Individual $individual
90b26116c9SGreg Roach     *
91b26116c9SGreg Roach     * @return Individual|null
9240150762SGreg Roach     */
93e364afe4SGreg Roach    public function mother(Individual $individual): ?Individual
94c1010edaSGreg Roach    {
951afbbc50SGreg Roach        $family = $individual->childFamilies()->first();
9640150762SGreg Roach
974ecdc95fSGreg Roach        if ($family instanceof Family) {
9839ca88baSGreg Roach            return $family->wife();
9940150762SGreg Roach        }
100b2ce94c6SRico Sonntag
101b2ce94c6SRico Sonntag        return null;
10240150762SGreg Roach    }
10340150762SGreg Roach
10440150762SGreg Roach    /**
10515d603e7SGreg Roach     * Find the current spouse family of an individual
10615d603e7SGreg Roach     *
10715d603e7SGreg Roach     * @param Individual $individual
10815d603e7SGreg Roach     *
10915d603e7SGreg Roach     * @return Family|null
11015d603e7SGreg Roach     */
111e364afe4SGreg Roach    public function spouseFamily(Individual $individual): ?Family
112c1010edaSGreg Roach    {
1134ecdc95fSGreg Roach        return $individual->spouseFamilies()
1144ecdc95fSGreg Roach            ->filter(function (Family $family): bool {
11515d603e7SGreg Roach                // Exclude families that were created after this census date
1164ecdc95fSGreg Roach                return Date::compare($family->getMarriageDate(), $this->date()) <= 0;
1174ecdc95fSGreg Roach            })
1184ecdc95fSGreg Roach            ->sort(Family::marriageDateComparator())
1194ecdc95fSGreg Roach            ->last();
12015d603e7SGreg Roach    }
12115d603e7SGreg Roach
12215d603e7SGreg Roach    /**
12313488723SDavid Drury     * What was an individual's likely name on a given date, allowing
12413488723SDavid Drury     * for marriages and married names.
12513488723SDavid Drury     *
12613488723SDavid Drury     * @param Individual $individual
12713488723SDavid Drury     *
128*24f2a3afSGreg Roach     * @return array<string>
12913488723SDavid Drury     */
13013488723SDavid Drury    protected function nameAtCensusDate(Individual $individual): array
13113488723SDavid Drury    {
13213488723SDavid Drury        $names  = $individual->getAllNames();
13313488723SDavid Drury        $name   = $names[0];
13413488723SDavid Drury        $family = $this->spouseFamily($individual);
13513488723SDavid Drury
13613488723SDavid Drury        if ($family instanceof Family) {
1379d722675SGreg Roach            $spouse = $family->spouse($individual);
1389d722675SGreg Roach
1399d722675SGreg Roach            if ($spouse instanceof Individual) {
14013488723SDavid Drury                foreach ($family->facts(['MARR']) as $marriage) {
14113488723SDavid Drury                    if ($marriage->date()->isOK()) {
14213488723SDavid Drury                        foreach ($names as $individual_name) {
14313488723SDavid Drury                            foreach ($spouse->getAllNames() as $spouse_name) {
14413488723SDavid Drury                                if ($individual_name['type'] === '_MARNM' && $individual_name['surn'] === $spouse_name['surn']) {
14513488723SDavid Drury                                    return $individual_name;
14613488723SDavid Drury                                }
14713488723SDavid Drury                            }
14813488723SDavid Drury                        }
14913488723SDavid Drury                    }
15013488723SDavid Drury                }
15113488723SDavid Drury            }
1529d722675SGreg Roach        }
15313488723SDavid Drury
15413488723SDavid Drury        return $name;
15513488723SDavid Drury    }
15613488723SDavid Drury
15713488723SDavid Drury    /**
15815d603e7SGreg Roach     * When did this census occur
15915d603e7SGreg Roach     *
16015d603e7SGreg Roach     * @return Date
16115d603e7SGreg Roach     */
1628f53f488SRico Sonntag    public function date(): Date
163c1010edaSGreg Roach    {
16415d603e7SGreg Roach        return new Date($this->census->censusDate());
16515d603e7SGreg Roach    }
16615d603e7SGreg Roach
16715d603e7SGreg Roach    /**
16815d603e7SGreg Roach     * The full version of the column's name.
16915d603e7SGreg Roach     *
17015d603e7SGreg Roach     * @return string
17115d603e7SGreg Roach     */
1728f53f488SRico Sonntag    public function title(): string
173c1010edaSGreg Roach    {
17415d603e7SGreg Roach        return $this->title;
17515d603e7SGreg Roach    }
17615d603e7SGreg Roach
17715d603e7SGreg Roach    /**
17815d603e7SGreg Roach     * Extract the country (last part) of a place name.
17915d603e7SGreg Roach     *
18015d603e7SGreg Roach     * @param string $place - e.g. "London, England"
18115d603e7SGreg Roach     *
18215d603e7SGreg Roach     * @return string - e.g. "England"
18315d603e7SGreg Roach     */
1847d99559cSGreg Roach    protected function lastPartOfPlace(string $place): string
185c1010edaSGreg Roach    {
1867d99559cSGreg Roach        $parts = explode(', ', $place);
18715d603e7SGreg Roach
1887d99559cSGreg Roach        return end($parts);
18915d603e7SGreg Roach    }
19015d603e7SGreg Roach
19115d603e7SGreg Roach    /**
192a53db70dSGreg Roach     * Remove the country of a place name, where it is the same as the census place
193a53db70dSGreg Roach     *
194a53db70dSGreg Roach     * @param string $place - e.g. "London, England"
195a53db70dSGreg Roach     *
196a53db70dSGreg Roach     * @return string - e.g. "London" (for census of England) and "London, England" elsewhere
197a53db70dSGreg Roach     */
1987d99559cSGreg Roach    protected function notCountry(string $place): string
199c1010edaSGreg Roach    {
200a53db70dSGreg Roach        $parts = explode(', ', $place);
201a53db70dSGreg Roach
202a53db70dSGreg Roach        if (end($parts) === $this->place()) {
203a53db70dSGreg Roach            return implode(', ', array_slice($parts, 0, -1));
204a53db70dSGreg Roach        }
205b2ce94c6SRico Sonntag
206b2ce94c6SRico Sonntag        return $place;
207a53db70dSGreg Roach    }
208a53db70dSGreg Roach
209a53db70dSGreg Roach    /**
210db7d25eeSGreg Roach     * Where did this census occur
211db7d25eeSGreg Roach     *
212ef21b467SGreg Roach     * @return string
213db7d25eeSGreg Roach     */
2148f53f488SRico Sonntag    public function place(): string
215c1010edaSGreg Roach    {
216db7d25eeSGreg Roach        return $this->census->censusPlace();
2174ccf2a72SGreg Roach    }
2184ccf2a72SGreg Roach}
219