xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 376123c414a36e5151c2e51777a7c6eb53564481)
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 * Marital status.
25 */
26abstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface
27{
28    /** @var string Text to display for married males */
29    protected $husband = '';
30
31    /** @var string Text to display for married females */
32    protected $wife = '';
33
34    /** @var string Text to display for unmarried males */
35    protected $bachelor = '';
36
37    /** @var string Text to display for unmarried females */
38    protected $spinster = '';
39
40    /** @var string Text to display for male children */
41    protected $boy = '';
42
43    /** @var string Text to display for female children */
44    protected $girl = '';
45
46    /** @var string Text to display for divorced males */
47    protected $divorce = '';
48
49    /** @var string Text to display for divorced females */
50    protected $divorcee = '';
51
52    /** @var string Text to display for widowed males */
53    protected $widower = '';
54
55    /** @var string Text to display for widowed females */
56    protected $widow = '';
57
58    /** @var int At what age is this individual recorded as an adult */
59    protected $age_adult = 15;
60
61    /**
62     * Generate the likely value of this census column, based on available information.
63     *
64     * @param Individual $individual
65     * @param Individual $head
66     *
67     * @return string
68     */
69    public function generate(Individual $individual, Individual $head): string
70    {
71        $family = $this->spouseFamily($individual);
72        $sex    = $individual->sex();
73
74        if ($family === null || $family->facts(['MARR'])->isEmpty()) {
75            if ($this->isChild($individual)) {
76                return $this->conditionChild($sex);
77            }
78
79            return $this->conditionSingle($sex);
80        }
81
82        if ($family->facts(['DIV'])->isNotEmpty()) {
83            return $this->conditionDivorced($sex);
84        }
85
86        $spouse = $family->spouse($individual);
87        if ($spouse instanceof Individual && $this->isDead($spouse)) {
88            return $this->conditionWidowed($sex);
89        }
90
91        return $this->conditionMarried($sex);
92    }
93
94    /**
95     * Is the individual a child.
96     *
97     * @param Individual $individual
98     *
99     * @return bool
100     */
101    private function isChild(Individual $individual): bool
102    {
103        $age = Date::getAgeYears($individual->getEstimatedBirthDate(), $this->date());
104
105        return $age < $this->age_adult;
106    }
107
108    /**
109     * How is this condition written in a census column.
110     *
111     * @param string $sex
112     *
113     * @return string
114     */
115    private function conditionChild($sex): string
116    {
117        if ($sex === 'F') {
118            return $this->girl;
119        }
120
121        return $this->boy;
122    }
123
124    /**
125     * How is this condition written in a census column.
126     *
127     * @param string $sex
128     *
129     * @return string
130     */
131    private function conditionSingle($sex): string
132    {
133        if ($sex === 'F') {
134            return $this->spinster;
135        }
136
137        return $this->bachelor;
138    }
139
140    /**
141     * How is this condition written in a census column.
142     *
143     * @param string $sex
144     *
145     * @return string
146     */
147    private function conditionDivorced($sex): string
148    {
149        if ($sex === 'F') {
150            return $this->divorcee;
151        }
152
153        return $this->divorce;
154    }
155
156    /**
157     * Is the individual dead.
158     *
159     * @param Individual $individual
160     *
161     * @return bool
162     */
163    private function isDead(Individual $individual): bool
164    {
165        return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
166    }
167
168    /**
169     * How is this condition written in a census column.
170     *
171     * @param string $sex
172     *
173     * @return string
174     */
175    private function conditionWidowed($sex): string
176    {
177        if ($sex === 'F') {
178            return $this->widow;
179        }
180
181        return $this->widower;
182    }
183
184    /**
185     * How is this condition written in a census column.
186     *
187     * @param string $sex
188     *
189     * @return string
190     */
191    private function conditionMarried($sex): string
192    {
193        if ($sex === 'F') {
194            return $this->wife;
195        }
196
197        return $this->husband;
198    }
199}
200