xref: /webtrees/app/Census/AbstractCensusColumn.php (revision 8f53f488f13e53e44dc48778e8f51ec9f91352dd)
14ccf2a72SGreg Roach<?php
24ccf2a72SGreg Roach/**
34ccf2a72SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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 */
1615d603e7SGreg Roach
174ccf2a72SGreg Roachnamespace Fisharebest\Webtrees\Census;
184ccf2a72SGreg Roach
194ccf2a72SGreg Roachuse Fisharebest\Webtrees\Date;
20b26116c9SGreg Roachuse Fisharebest\Webtrees\Family;
2140150762SGreg Roachuse Fisharebest\Webtrees\Individual;
224ccf2a72SGreg Roach
234ccf2a72SGreg Roach/**
244ccf2a72SGreg Roach * Definitions for a census column
254ccf2a72SGreg Roach */
26c1010edaSGreg Roachclass AbstractCensusColumn
27c1010edaSGreg Roach{
28ef21b467SGreg Roach    /** @var CensusInterface */
29ef21b467SGreg Roach    private $census;
30ef21b467SGreg Roach
31ef21b467SGreg Roach    /** @var string */
32ef21b467SGreg Roach    private $abbr;
33ef21b467SGreg Roach
34ef21b467SGreg Roach    /** @var string */
35ef21b467SGreg Roach    private $title;
364ccf2a72SGreg Roach
374ccf2a72SGreg Roach    /**
38db7d25eeSGreg Roach     * Create a column for a census
394ccf2a72SGreg Roach     *
40ef21b467SGreg Roach     * @param CensusInterface $census - The census to which this column forms part.
41ef21b467SGreg Roach     * @param string          $abbr   - The abbrievated on-screen name "BiC"
42ef21b467SGreg Roach     * @param string          $title  - The full column heading "Born in the county"
434ccf2a72SGreg Roach     */
44c1010edaSGreg Roach    public function __construct(CensusInterface $census, $abbr, $title)
45c1010edaSGreg Roach    {
46db7d25eeSGreg Roach        $this->census = $census;
47ef21b467SGreg Roach        $this->abbr   = $abbr;
48ef21b467SGreg Roach        $this->title  = $title;
49ef21b467SGreg Roach    }
50ef21b467SGreg Roach
51ef21b467SGreg Roach    /**
52ef21b467SGreg Roach     * A short version of the column's name.
53ef21b467SGreg Roach     *
54ef21b467SGreg Roach     * @return string
55ef21b467SGreg Roach     */
56*8f53f488SRico Sonntag    public function abbreviation(): string
57c1010edaSGreg Roach    {
58ef21b467SGreg Roach        return $this->abbr;
59db7d25eeSGreg Roach    }
60db7d25eeSGreg Roach
61db7d25eeSGreg Roach    /**
6240150762SGreg Roach     * Find the father of an individual
6340150762SGreg Roach     *
64b26116c9SGreg Roach     * @param Individual $individual
65b26116c9SGreg Roach     *
66b26116c9SGreg Roach     * @return Individual|null
6740150762SGreg Roach     */
68c1010edaSGreg Roach    public function father(Individual $individual)
69c1010edaSGreg Roach    {
7040150762SGreg Roach        $family = $individual->getPrimaryChildFamily();
7140150762SGreg Roach
7240150762SGreg Roach        if ($family) {
7340150762SGreg Roach            return $family->getHusband();
7440150762SGreg Roach        } else {
7540150762SGreg Roach            return null;
7640150762SGreg Roach        }
7740150762SGreg Roach    }
7840150762SGreg Roach
7940150762SGreg Roach    /**
8040150762SGreg Roach     * Find the mother of an individual
8140150762SGreg Roach     *
82b26116c9SGreg Roach     * @param Individual $individual
83b26116c9SGreg Roach     *
84b26116c9SGreg Roach     * @return Individual|null
8540150762SGreg Roach     */
86c1010edaSGreg Roach    public function mother(Individual $individual)
87c1010edaSGreg Roach    {
8840150762SGreg Roach        $family = $individual->getPrimaryChildFamily();
8940150762SGreg Roach
9040150762SGreg Roach        if ($family) {
9140150762SGreg Roach            return $family->getWife();
9240150762SGreg Roach        } else {
9340150762SGreg Roach            return null;
9440150762SGreg Roach        }
9540150762SGreg Roach    }
9640150762SGreg Roach
9740150762SGreg Roach    /**
9815d603e7SGreg Roach     * Find the current spouse family of an individual
9915d603e7SGreg Roach     *
10015d603e7SGreg Roach     * @param Individual $individual
10115d603e7SGreg Roach     *
10215d603e7SGreg Roach     * @return Family|null
10315d603e7SGreg Roach     */
104c1010edaSGreg Roach    public function spouseFamily(Individual $individual)
105c1010edaSGreg Roach    {
10615d603e7SGreg Roach        // Exclude families that were created after this census date
10715d603e7SGreg Roach        $families = [];
10815d603e7SGreg Roach        foreach ($individual->getSpouseFamilies() as $family) {
10915d603e7SGreg Roach            if (Date::compare($family->getMarriageDate(), $this->date()) <= 0) {
11015d603e7SGreg Roach                $families[] = $family;
11115d603e7SGreg Roach            }
11215d603e7SGreg Roach        }
11315d603e7SGreg Roach
11415d603e7SGreg Roach        if (empty($families)) {
11515d603e7SGreg Roach            return null;
11615d603e7SGreg Roach        } else {
117492c7072SGreg Roach            usort($families, function (Family $x, Family $y): int {
11815d603e7SGreg Roach                return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
11915d603e7SGreg Roach            });
12015d603e7SGreg Roach
12115d603e7SGreg Roach            return end($families);
12215d603e7SGreg Roach        }
12315d603e7SGreg Roach    }
12415d603e7SGreg Roach
12515d603e7SGreg Roach    /**
12615d603e7SGreg Roach     * When did this census occur
12715d603e7SGreg Roach     *
12815d603e7SGreg Roach     * @return Date
12915d603e7SGreg Roach     */
130*8f53f488SRico Sonntag    public function date(): Date
131c1010edaSGreg Roach    {
13215d603e7SGreg Roach        return new Date($this->census->censusDate());
13315d603e7SGreg Roach    }
13415d603e7SGreg Roach
13515d603e7SGreg Roach    /**
13615d603e7SGreg Roach     * The full version of the column's name.
13715d603e7SGreg Roach     *
13815d603e7SGreg Roach     * @return string
13915d603e7SGreg Roach     */
140*8f53f488SRico Sonntag    public function title(): string
141c1010edaSGreg Roach    {
14215d603e7SGreg Roach        return $this->title;
14315d603e7SGreg Roach    }
14415d603e7SGreg Roach
14515d603e7SGreg Roach    /**
14615d603e7SGreg Roach     * Extract the country (last part) of a place name.
14715d603e7SGreg Roach     *
14815d603e7SGreg Roach     * @param string $place - e.g. "London, England"
14915d603e7SGreg Roach     *
15015d603e7SGreg Roach     * @return string - e.g. "England"
15115d603e7SGreg Roach     */
152*8f53f488SRico Sonntag    protected function lastPartOfPlace($place): string
153c1010edaSGreg Roach    {
15415d603e7SGreg Roach        $place = explode(', ', $place);
15515d603e7SGreg Roach
15615d603e7SGreg Roach        return end($place);
15715d603e7SGreg Roach    }
15815d603e7SGreg Roach
15915d603e7SGreg Roach    /**
160a53db70dSGreg Roach     * Remove the country of a place name, where it is the same as the census place
161a53db70dSGreg Roach     *
162a53db70dSGreg Roach     * @param string $place - e.g. "London, England"
163a53db70dSGreg Roach     *
164a53db70dSGreg Roach     * @return string - e.g. "London" (for census of England) and "London, England" elsewhere
165a53db70dSGreg Roach     */
166c1010edaSGreg Roach    protected function notCountry($place)
167c1010edaSGreg Roach    {
168a53db70dSGreg Roach        $parts = explode(', ', $place);
169a53db70dSGreg Roach
170a53db70dSGreg Roach        if (end($parts) === $this->place()) {
171a53db70dSGreg Roach            return implode(', ', array_slice($parts, 0, -1));
172a53db70dSGreg Roach        } else {
173a53db70dSGreg Roach            return $place;
174a53db70dSGreg Roach        }
175a53db70dSGreg Roach    }
176a53db70dSGreg Roach
177a53db70dSGreg Roach    /**
178db7d25eeSGreg Roach     * Where did this census occur
179db7d25eeSGreg Roach     *
180ef21b467SGreg Roach     * @return string
181db7d25eeSGreg Roach     */
182*8f53f488SRico Sonntag    public function place(): string
183c1010edaSGreg Roach    {
184db7d25eeSGreg Roach        return $this->census->censusPlace();
1854ccf2a72SGreg Roach    }
1864ccf2a72SGreg Roach}
187