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