xref: /webtrees/app/Census/AbstractCensusColumn.php (revision 7d99559cb9c1475576caf7ef2fe79a5aad947d30)
14ccf2a72SGreg Roach<?php
24ccf2a72SGreg Roach/**
34ccf2a72SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
54ccf2a72SGreg Roach * This program is free software: you can redistribute it and/or modify
64ccf2a72SGreg Roach * it under the terms of the GNU General Public License as published by
74ccf2a72SGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ccf2a72SGreg Roach * (at your option) any later version.
94ccf2a72SGreg Roach * This program is distributed in the hope that it will be useful,
104ccf2a72SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ccf2a72SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ccf2a72SGreg Roach * GNU General Public License for more details.
134ccf2a72SGreg Roach * You should have received a copy of the GNU General Public License
144ccf2a72SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ccf2a72SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
1715d603e7SGreg Roach
184ccf2a72SGreg Roachnamespace Fisharebest\Webtrees\Census;
194ccf2a72SGreg Roach
204ccf2a72SGreg Roachuse Fisharebest\Webtrees\Date;
21b26116c9SGreg Roachuse Fisharebest\Webtrees\Family;
2240150762SGreg Roachuse Fisharebest\Webtrees\Individual;
234ccf2a72SGreg Roach
244ccf2a72SGreg Roach/**
254ccf2a72SGreg Roach * Definitions for a census column
264ccf2a72SGreg Roach */
27c1010edaSGreg Roachclass AbstractCensusColumn
28c1010edaSGreg Roach{
29ef21b467SGreg Roach    /** @var CensusInterface */
30ef21b467SGreg Roach    private $census;
31ef21b467SGreg Roach
32ef21b467SGreg Roach    /** @var string */
33ef21b467SGreg Roach    private $abbr;
34ef21b467SGreg Roach
35ef21b467SGreg Roach    /** @var string */
36ef21b467SGreg Roach    private $title;
374ccf2a72SGreg Roach
384ccf2a72SGreg Roach    /**
39db7d25eeSGreg Roach     * Create a column for a census
404ccf2a72SGreg Roach     *
41ef21b467SGreg Roach     * @param CensusInterface $census - The census to which this column forms part.
42ef21b467SGreg Roach     * @param string          $abbr   - The abbrievated on-screen name "BiC"
43ef21b467SGreg Roach     * @param string          $title  - The full column heading "Born in the county"
444ccf2a72SGreg Roach     */
45c1010edaSGreg Roach    public function __construct(CensusInterface $census, $abbr, $title)
46c1010edaSGreg Roach    {
47db7d25eeSGreg Roach        $this->census = $census;
48ef21b467SGreg Roach        $this->abbr   = $abbr;
49ef21b467SGreg Roach        $this->title  = $title;
50ef21b467SGreg Roach    }
51ef21b467SGreg Roach
52ef21b467SGreg Roach    /**
53ef21b467SGreg Roach     * A short version of the column's name.
54ef21b467SGreg Roach     *
55ef21b467SGreg Roach     * @return string
56ef21b467SGreg Roach     */
578f53f488SRico Sonntag    public function abbreviation(): string
58c1010edaSGreg Roach    {
59ef21b467SGreg Roach        return $this->abbr;
60db7d25eeSGreg Roach    }
61db7d25eeSGreg Roach
62db7d25eeSGreg Roach    /**
6340150762SGreg Roach     * Find the father of an individual
6440150762SGreg Roach     *
65b26116c9SGreg Roach     * @param Individual $individual
66b26116c9SGreg Roach     *
67b26116c9SGreg Roach     * @return Individual|null
6840150762SGreg Roach     */
69c1010edaSGreg Roach    public function father(Individual $individual)
70c1010edaSGreg Roach    {
7139ca88baSGreg Roach        $family = $individual->primaryChildFamily();
7240150762SGreg Roach
7340150762SGreg Roach        if ($family) {
7439ca88baSGreg Roach            return $family->husband();
7540150762SGreg Roach        }
76b2ce94c6SRico Sonntag
77b2ce94c6SRico Sonntag        return null;
7840150762SGreg Roach    }
7940150762SGreg Roach
8040150762SGreg Roach    /**
8140150762SGreg Roach     * Find the mother of an individual
8240150762SGreg Roach     *
83b26116c9SGreg Roach     * @param Individual $individual
84b26116c9SGreg Roach     *
85b26116c9SGreg Roach     * @return Individual|null
8640150762SGreg Roach     */
87c1010edaSGreg Roach    public function mother(Individual $individual)
88c1010edaSGreg Roach    {
8939ca88baSGreg Roach        $family = $individual->primaryChildFamily();
9040150762SGreg Roach
9140150762SGreg Roach        if ($family) {
9239ca88baSGreg Roach            return $family->wife();
9340150762SGreg Roach        }
94b2ce94c6SRico Sonntag
95b2ce94c6SRico Sonntag        return null;
9640150762SGreg Roach    }
9740150762SGreg Roach
9840150762SGreg Roach    /**
9915d603e7SGreg Roach     * Find the current spouse family of an individual
10015d603e7SGreg Roach     *
10115d603e7SGreg Roach     * @param Individual $individual
10215d603e7SGreg Roach     *
10315d603e7SGreg Roach     * @return Family|null
10415d603e7SGreg Roach     */
105c1010edaSGreg Roach    public function spouseFamily(Individual $individual)
106c1010edaSGreg Roach    {
10715d603e7SGreg Roach        // Exclude families that were created after this census date
10815d603e7SGreg Roach        $families = [];
10939ca88baSGreg Roach        foreach ($individual->spouseFamilies() as $family) {
11015d603e7SGreg Roach            if (Date::compare($family->getMarriageDate(), $this->date()) <= 0) {
11115d603e7SGreg Roach                $families[] = $family;
11215d603e7SGreg Roach            }
11315d603e7SGreg Roach        }
11415d603e7SGreg Roach
11515d603e7SGreg Roach        if (empty($families)) {
11615d603e7SGreg Roach            return null;
117b2ce94c6SRico Sonntag        }
118b2ce94c6SRico Sonntag
119492c7072SGreg Roach        usort($families, function (Family $x, Family $y): int {
12015d603e7SGreg Roach            return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
12115d603e7SGreg Roach        });
12215d603e7SGreg Roach
12315d603e7SGreg Roach        return end($families);
12415d603e7SGreg Roach    }
12515d603e7SGreg Roach
12615d603e7SGreg Roach    /**
12715d603e7SGreg Roach     * When did this census occur
12815d603e7SGreg Roach     *
12915d603e7SGreg Roach     * @return Date
13015d603e7SGreg Roach     */
1318f53f488SRico Sonntag    public function date(): Date
132c1010edaSGreg Roach    {
13315d603e7SGreg Roach        return new Date($this->census->censusDate());
13415d603e7SGreg Roach    }
13515d603e7SGreg Roach
13615d603e7SGreg Roach    /**
13715d603e7SGreg Roach     * The full version of the column's name.
13815d603e7SGreg Roach     *
13915d603e7SGreg Roach     * @return string
14015d603e7SGreg Roach     */
1418f53f488SRico Sonntag    public function title(): string
142c1010edaSGreg Roach    {
14315d603e7SGreg Roach        return $this->title;
14415d603e7SGreg Roach    }
14515d603e7SGreg Roach
14615d603e7SGreg Roach    /**
14715d603e7SGreg Roach     * Extract the country (last part) of a place name.
14815d603e7SGreg Roach     *
14915d603e7SGreg Roach     * @param string $place - e.g. "London, England"
15015d603e7SGreg Roach     *
15115d603e7SGreg Roach     * @return string - e.g. "England"
15215d603e7SGreg Roach     */
153*7d99559cSGreg Roach    protected function lastPartOfPlace(string $place): string
154c1010edaSGreg Roach    {
155*7d99559cSGreg Roach        $parts = explode(', ', $place);
15615d603e7SGreg Roach
157*7d99559cSGreg Roach        return end($parts);
15815d603e7SGreg Roach    }
15915d603e7SGreg Roach
16015d603e7SGreg Roach    /**
161a53db70dSGreg Roach     * Remove the country of a place name, where it is the same as the census place
162a53db70dSGreg Roach     *
163a53db70dSGreg Roach     * @param string $place - e.g. "London, England"
164a53db70dSGreg Roach     *
165a53db70dSGreg Roach     * @return string - e.g. "London" (for census of England) and "London, England" elsewhere
166a53db70dSGreg Roach     */
167*7d99559cSGreg Roach    protected function notCountry(string $place): string
168c1010edaSGreg Roach    {
169a53db70dSGreg Roach        $parts = explode(', ', $place);
170a53db70dSGreg Roach
171a53db70dSGreg Roach        if (end($parts) === $this->place()) {
172a53db70dSGreg Roach            return implode(', ', array_slice($parts, 0, -1));
173a53db70dSGreg Roach        }
174b2ce94c6SRico Sonntag
175b2ce94c6SRico Sonntag        return $place;
176a53db70dSGreg Roach    }
177a53db70dSGreg Roach
178a53db70dSGreg Roach    /**
179db7d25eeSGreg Roach     * Where did this census occur
180db7d25eeSGreg Roach     *
181ef21b467SGreg Roach     * @return string
182db7d25eeSGreg Roach     */
1838f53f488SRico Sonntag    public function place(): string
184c1010edaSGreg Roach    {
185db7d25eeSGreg Roach        return $this->census->censusPlace();
1864ccf2a72SGreg Roach    }
1874ccf2a72SGreg Roach}
188