xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 873953697c930fadbf3243d2b8c0029fd684da0e)
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    /* Text to display for married males */
28    protected $husband = '';
29
30    /* Text to display for married females */
31    protected $wife = '';
32
33    /* Text to display for unmarried males */
34    protected $bachelor = '';
35
36    /* Text to display for unmarried females */
37    protected $spinster = '';
38
39    /* Text to display for male children */
40    protected $boy = '';
41
42    /* Text to display for female children */
43    protected $girl = '';
44
45    /* Text to display for divorced males */
46    protected $divorce = '';
47
48    /* Text to display for divorced females */
49    protected $divorcee = '';
50
51    /* Text to display for widowed males */
52    protected $widower = '';
53
54    /* Text to display for widowed females */
55    protected $widow = '';
56
57    /* 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            } else {
77                return $this->conditionSingle($sex);
78            }
79        } elseif (count($family->getFacts('DIV')) > 0) {
80            return $this->conditionDivorced($sex);
81        } else {
82            $spouse = $family->getSpouse($individual);
83            if ($spouse instanceof Individual && $this->isDead($spouse)) {
84                return $this->conditionWidowed($sex);
85            } else {
86                return $this->conditionMarried($sex);
87            }
88        }
89    }
90
91    /**
92     * Is the individual a child.
93     *
94     * @param Individual $individual
95     *
96     * @return bool
97     */
98    private function isChild(Individual $individual): bool
99    {
100        $age = (int)Date::getAge($individual->getEstimatedBirthDate(), $this->date(), 0);
101
102        return $age < $this->age_adult;
103    }
104
105    /**
106     * How is this condition written in a census column.
107     *
108     * @param string $sex
109     *
110     * @return string
111     */
112    private function conditionChild($sex)
113    {
114        if ($sex === 'F') {
115            return $this->girl;
116        } else {
117            return $this->boy;
118        }
119    }
120
121    /**
122     * How is this condition written in a census column.
123     *
124     * @param string $sex
125     *
126     * @return string
127     */
128    private function conditionSingle($sex)
129    {
130        if ($sex === 'F') {
131            return $this->spinster;
132        } else {
133            return $this->bachelor;
134        }
135    }
136
137    /**
138     * How is this condition written in a census column.
139     *
140     * @param string $sex
141     *
142     * @return string
143     */
144    private function conditionDivorced($sex)
145    {
146        if ($sex === 'F') {
147            return $this->divorcee;
148        } else {
149            return $this->divorce;
150        }
151    }
152
153    /**
154     * Is the individual dead.
155     *
156     * @param Individual $individual
157     *
158     * @return bool
159     */
160    private function isDead(Individual $individual): bool
161    {
162        return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
163    }
164
165    /**
166     * How is this condition written in a census column.
167     *
168     * @param string $sex
169     *
170     * @return string
171     */
172    private function conditionWidowed($sex)
173    {
174        if ($sex === 'F') {
175            return $this->widow;
176        } else {
177            return $this->widower;
178        }
179    }
180
181    /**
182     * How is this condition written in a census column.
183     *
184     * @param string $sex
185     *
186     * @return string
187     */
188    private function conditionMarried($sex)
189    {
190        if ($sex === 'F') {
191            return $this->wife;
192        } else {
193            return $this->husband;
194        }
195    }
196}
197