xref: /webtrees/app/Census/CensusColumnFullName.php (revision 52bcc40297e06e51c5cc74d5ddd66c750aefef51)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Census;
19
20use Fisharebest\Webtrees\Date;
21use Fisharebest\Webtrees\Individual;
22
23/**
24 * The individual's full name.
25 */
26class CensusColumnFullName extends AbstractCensusColumn implements CensusColumnInterface
27{
28    /**
29     * Generate the likely value of this census column, based on available information.
30     *
31     * @param Individual $individual
32     * @param Individual $head
33     *
34     * @return string
35     */
36    public function generate(Individual $individual, Individual $head): string
37    {
38        $name = $this->nameAtCensusDate($individual, $this->date());
39
40        return strip_tags($name['full']);
41    }
42
43    /**
44     * What was an individual's likely name on a given date, allowing
45     * for marriages and married names.
46     *
47     * @param Individual $individual
48     * @param Date       $census_date
49     *
50     * @return string[]
51     */
52    protected function nameAtCensusDate(Individual $individual, Date $census_date): array
53    {
54        $names = $individual->getAllNames();
55        $name  = $names[0];
56
57        foreach ($individual->getSpouseFamilies() as $family) {
58            foreach ($family->facts(['MARR']) as $marriage) {
59                if ($marriage->date()->isOK() && Date::compare($marriage->date(), $census_date) < 0) {
60                    $spouse = $family->getSpouse($individual);
61                    foreach ($names as $individual_name) {
62                        foreach ($spouse->getAllNames() as $spouse_name) {
63                            if ($individual_name['type'] === '_MARNM' && $individual_name['surn'] === $spouse_name['surn']) {
64                                return $individual_name;
65                            }
66                        }
67                    }
68                }
69            }
70        }
71
72        return $name;
73    }
74}
75