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