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