xref: /webtrees/app/Census/AbstractCensusColumn.php (revision e74bdd66f1fd8bd9fdd094461960da49042747ce)
14ccf2a72SGreg Roach<?php
23976b470SGreg Roach
34ccf2a72SGreg Roach/**
44ccf2a72SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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{
3505edc757SGreg Roach    private CensusInterface $census;
36ef21b467SGreg Roach
3705edc757SGreg Roach    private string $abbr;
38ef21b467SGreg Roach
3905edc757SGreg Roach    private string $title;
404ccf2a72SGreg Roach
414ccf2a72SGreg Roach    /**
42db7d25eeSGreg Roach     * Create a column for a census
434ccf2a72SGreg Roach     *
44e5766395SGreg Roach     * @param CensusInterface $census The census to which this column forms part.
45e5766395SGreg Roach     * @param string          $abbr   The abbreviated on-screen name "BiC"
46e5766395SGreg Roach     * @param string          $title  The full column heading "Born in the county"
474ccf2a72SGreg Roach     */
484ecdc95fSGreg Roach    public function __construct(CensusInterface $census, string $abbr, string $title)
49c1010edaSGreg Roach    {
50db7d25eeSGreg Roach        $this->census = $census;
51ef21b467SGreg Roach        $this->abbr   = $abbr;
52ef21b467SGreg Roach        $this->title  = $title;
53ef21b467SGreg Roach    }
54ef21b467SGreg Roach
55ef21b467SGreg Roach    /**
56ef21b467SGreg Roach     * A short version of the column's name.
57ef21b467SGreg Roach     *
58ef21b467SGreg Roach     * @return string
59ef21b467SGreg Roach     */
608f53f488SRico Sonntag    public function abbreviation(): string
61c1010edaSGreg Roach    {
62ef21b467SGreg Roach        return $this->abbr;
63db7d25eeSGreg Roach    }
64db7d25eeSGreg Roach
65db7d25eeSGreg Roach    /**
6640150762SGreg Roach     * Find the father of an individual
6740150762SGreg Roach     */
68*e74bdd66SGreg Roach    public function father(Individual $individual): Individual|null
69c1010edaSGreg Roach    {
701afbbc50SGreg Roach        $family = $individual->childFamilies()->first();
7140150762SGreg Roach
724ecdc95fSGreg Roach        if ($family instanceof Family) {
7339ca88baSGreg Roach            return $family->husband();
7440150762SGreg Roach        }
75b2ce94c6SRico Sonntag
76b2ce94c6SRico Sonntag        return null;
7740150762SGreg Roach    }
7840150762SGreg Roach
7940150762SGreg Roach    /**
8040150762SGreg Roach     * Find the mother of an individual
8140150762SGreg Roach     */
82*e74bdd66SGreg Roach    public function mother(Individual $individual): Individual|null
83c1010edaSGreg Roach    {
841afbbc50SGreg Roach        $family = $individual->childFamilies()->first();
8540150762SGreg Roach
864ecdc95fSGreg Roach        if ($family instanceof Family) {
8739ca88baSGreg Roach            return $family->wife();
8840150762SGreg Roach        }
89b2ce94c6SRico Sonntag
90b2ce94c6SRico Sonntag        return null;
9140150762SGreg Roach    }
9240150762SGreg Roach
9340150762SGreg Roach    /**
9415d603e7SGreg Roach     * Find the current spouse family of an individual
9515d603e7SGreg Roach     */
96*e74bdd66SGreg Roach    public function spouseFamily(Individual $individual): Family|null
97c1010edaSGreg Roach    {
984ecdc95fSGreg Roach        return $individual->spouseFamilies()
9915d603e7SGreg Roach                // Exclude families that were created after this census date
100f25fc0f9SGreg Roach            ->filter(fn (Family $family): bool => Date::compare($family->getMarriageDate(), $this->date()) <= 0)
1014ecdc95fSGreg Roach            ->sort(Family::marriageDateComparator())
1024ecdc95fSGreg Roach            ->last();
10315d603e7SGreg Roach    }
10415d603e7SGreg Roach
10515d603e7SGreg Roach    /**
10613488723SDavid Drury     * What was an individual's likely name on a given date, allowing
10713488723SDavid Drury     * for marriages and married names.
10813488723SDavid Drury     *
10924f2a3afSGreg Roach     * @return array<string>
11013488723SDavid Drury     */
11113488723SDavid Drury    protected function nameAtCensusDate(Individual $individual): array
11213488723SDavid Drury    {
11313488723SDavid Drury        $names  = $individual->getAllNames();
11413488723SDavid Drury        $name   = $names[0];
11513488723SDavid Drury        $family = $this->spouseFamily($individual);
11613488723SDavid Drury
11713488723SDavid Drury        if ($family instanceof Family) {
1189d722675SGreg Roach            $spouse = $family->spouse($individual);
1199d722675SGreg Roach
1209d722675SGreg Roach            if ($spouse instanceof Individual) {
12113488723SDavid Drury                foreach ($family->facts(['MARR']) as $marriage) {
12213488723SDavid Drury                    if ($marriage->date()->isOK()) {
12313488723SDavid Drury                        foreach ($names as $individual_name) {
12413488723SDavid Drury                            foreach ($spouse->getAllNames() as $spouse_name) {
12513488723SDavid Drury                                if ($individual_name['type'] === '_MARNM' && $individual_name['surn'] === $spouse_name['surn']) {
12613488723SDavid Drury                                    return $individual_name;
12713488723SDavid Drury                                }
12813488723SDavid Drury                            }
12913488723SDavid Drury                        }
13013488723SDavid Drury                    }
13113488723SDavid Drury                }
13213488723SDavid Drury            }
1339d722675SGreg Roach        }
13413488723SDavid Drury
13513488723SDavid Drury        return $name;
13613488723SDavid Drury    }
13713488723SDavid Drury
13813488723SDavid Drury    /**
13915d603e7SGreg Roach     * When did this census occur
14015d603e7SGreg Roach     */
1418f53f488SRico Sonntag    public function date(): Date
142c1010edaSGreg Roach    {
14315d603e7SGreg Roach        return new Date($this->census->censusDate());
14415d603e7SGreg Roach    }
14515d603e7SGreg Roach
14615d603e7SGreg Roach    /**
14715d603e7SGreg Roach     * The full version of the column's name.
14815d603e7SGreg Roach     *
14915d603e7SGreg Roach     * @return string
15015d603e7SGreg Roach     */
1518f53f488SRico Sonntag    public function title(): string
152c1010edaSGreg Roach    {
15315d603e7SGreg Roach        return $this->title;
15415d603e7SGreg Roach    }
15515d603e7SGreg Roach
15615d603e7SGreg Roach    /**
15715d603e7SGreg Roach     * Extract the country (last part) of a place name.
15815d603e7SGreg Roach     *
159e5766395SGreg Roach     * @param string $place e.g. "London, England"
16015d603e7SGreg Roach     *
161e5766395SGreg Roach     * @return string e.g. "England"
16215d603e7SGreg Roach     */
1637d99559cSGreg Roach    protected function lastPartOfPlace(string $place): string
164c1010edaSGreg Roach    {
1657d99559cSGreg Roach        $parts = explode(', ', $place);
16615d603e7SGreg Roach
1677d99559cSGreg Roach        return end($parts);
16815d603e7SGreg Roach    }
16915d603e7SGreg Roach
17015d603e7SGreg Roach    /**
171a53db70dSGreg Roach     * Remove the country of a place name, where it is the same as the census place
172a53db70dSGreg Roach     *
173e5766395SGreg Roach     * @param string $place e.g. "London, England"
174a53db70dSGreg Roach     *
175e5766395SGreg Roach     * @return string e.g. "London" (for census of England) and "London, England" elsewhere
176a53db70dSGreg Roach     */
1777d99559cSGreg Roach    protected function notCountry(string $place): string
178c1010edaSGreg Roach    {
179a53db70dSGreg Roach        $parts = explode(', ', $place);
180a53db70dSGreg Roach
181a53db70dSGreg Roach        if (end($parts) === $this->place()) {
182a53db70dSGreg Roach            return implode(', ', array_slice($parts, 0, -1));
183a53db70dSGreg Roach        }
184b2ce94c6SRico Sonntag
185b2ce94c6SRico Sonntag        return $place;
186a53db70dSGreg Roach    }
187a53db70dSGreg Roach
188a53db70dSGreg Roach    /**
189db7d25eeSGreg Roach     * Where did this census occur
190db7d25eeSGreg Roach     *
191ef21b467SGreg Roach     * @return string
192db7d25eeSGreg Roach     */
1938f53f488SRico Sonntag    public function place(): string
194c1010edaSGreg Roach    {
195db7d25eeSGreg Roach        return $this->census->censusPlace();
1964ccf2a72SGreg Roach    }
1974ccf2a72SGreg Roach}
198