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