xref: /webtrees/app/Census/AbstractCensusColumn.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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    /** @var CensusInterface */
36    private $census;
37
38    /** @var string */
39    private $abbr;
40
41    /** @var string */
42    private $title;
43
44    /**
45     * Create a column for a census
46     *
47     * @param CensusInterface $census - The census to which this column forms part.
48     * @param string          $abbr   - The abbreviated on-screen name "BiC"
49     * @param string          $title  - The full column heading "Born in the county"
50     */
51    public function __construct(CensusInterface $census, string $abbr, string $title)
52    {
53        $this->census = $census;
54        $this->abbr   = $abbr;
55        $this->title  = $title;
56    }
57
58    /**
59     * A short version of the column's name.
60     *
61     * @return string
62     */
63    public function abbreviation(): string
64    {
65        return $this->abbr;
66    }
67
68    /**
69     * Find the father of an individual
70     *
71     * @param Individual $individual
72     *
73     * @return Individual|null
74     */
75    public function father(Individual $individual): ?Individual
76    {
77        $family = $individual->childFamilies()->first();
78
79        if ($family instanceof Family) {
80            return $family->husband();
81        }
82
83        return null;
84    }
85
86    /**
87     * Find the mother of an individual
88     *
89     * @param Individual $individual
90     *
91     * @return Individual|null
92     */
93    public function mother(Individual $individual): ?Individual
94    {
95        $family = $individual->childFamilies()->first();
96
97        if ($family instanceof Family) {
98            return $family->wife();
99        }
100
101        return null;
102    }
103
104    /**
105     * Find the current spouse family of an individual
106     *
107     * @param Individual $individual
108     *
109     * @return Family|null
110     */
111    public function spouseFamily(Individual $individual): ?Family
112    {
113        return $individual->spouseFamilies()
114            ->filter(function (Family $family): bool {
115                // Exclude families that were created after this census date
116                return Date::compare($family->getMarriageDate(), $this->date()) <= 0;
117            })
118            ->sort(Family::marriageDateComparator())
119            ->last();
120    }
121
122    /**
123     * When did this census occur
124     *
125     * @return Date
126     */
127    public function date(): Date
128    {
129        return new Date($this->census->censusDate());
130    }
131
132    /**
133     * The full version of the column's name.
134     *
135     * @return string
136     */
137    public function title(): string
138    {
139        return $this->title;
140    }
141
142    /**
143     * Extract the country (last part) of a place name.
144     *
145     * @param string $place - e.g. "London, England"
146     *
147     * @return string - e.g. "England"
148     */
149    protected function lastPartOfPlace(string $place): string
150    {
151        $parts = explode(', ', $place);
152
153        return end($parts);
154    }
155
156    /**
157     * Remove the country of a place name, where it is the same as the census place
158     *
159     * @param string $place - e.g. "London, England"
160     *
161     * @return string - e.g. "London" (for census of England) and "London, England" elsewhere
162     */
163    protected function notCountry(string $place): string
164    {
165        $parts = explode(', ', $place);
166
167        if (end($parts) === $this->place()) {
168            return implode(', ', array_slice($parts, 0, -1));
169        }
170
171        return $place;
172    }
173
174    /**
175     * Where did this census occur
176     *
177     * @return string
178     */
179    public function place(): string
180    {
181        return $this->census->censusPlace();
182    }
183}
184