xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 0f97530b568da7b969b729d6803c91fba4652cde)
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    // Text to display for married males
29    protected const HUSBAND = '';
30
31    // Text to display for married females
32    protected const WIFE = '';
33
34    // Text to display for married unmarried males
35    protected const BACHELOR = '';
36
37    // Text to display for married unmarried females
38    protected const SPINSTER = '';
39
40    // Text to display for male children
41    protected const BOY = '';
42
43    // Text to display for female children
44    protected const GIRL = '';
45
46    // Text to display for divorced males
47    protected const DIVORCE = '';
48
49    // Text to display for divorced females
50    protected const DIVORCEE = '';
51
52    // Text to display for widowed males
53    protected const WIDOWER = '';
54
55    // Text to display for widowed females
56    protected const WIDOW = '';
57
58    // At what age is this individual recorded as an adult
59    protected const 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 < static::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(string $sex): string
116    {
117        if ($sex === 'F') {
118            return static::GIRL;
119        }
120
121        return static::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(string $sex): string
132    {
133        if ($sex === 'F') {
134            return static::SPINSTER;
135        }
136
137        return static::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(string $sex): string
148    {
149        if ($sex === 'F') {
150            return static::DIVORCEE;
151        }
152
153        return static::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(string $sex): string
176    {
177        if ($sex === 'F') {
178            return static::WIDOW;
179        }
180
181        return static::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(string $sex): string
192    {
193        if ($sex === 'F') {
194            return static::WIFE;
195        }
196
197        return static::HUSBAND;
198    }
199}
200