xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 8fe32d0d463aad11cb638c8191897145e9a9a1ba)
100225b98SGreg Roach<?php
200225b98SGreg Roach/**
300225b98SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
500225b98SGreg Roach * This program is free software: you can redistribute it and/or modify
600225b98SGreg Roach * it under the terms of the GNU General Public License as published by
700225b98SGreg Roach * the Free Software Foundation, either version 3 of the License, or
800225b98SGreg Roach * (at your option) any later version.
900225b98SGreg Roach * This program is distributed in the hope that it will be useful,
1000225b98SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1100225b98SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1200225b98SGreg Roach * GNU General Public License for more details.
1300225b98SGreg Roach * You should have received a copy of the GNU General Public License
1400225b98SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1500225b98SGreg Roach */
1615d603e7SGreg Roach
1700225b98SGreg Roachnamespace Fisharebest\Webtrees\Census;
1800225b98SGreg Roach
1900225b98SGreg Roachuse Fisharebest\Webtrees\Date;
2000225b98SGreg Roachuse Fisharebest\Webtrees\Individual;
2100225b98SGreg Roach
2200225b98SGreg Roach/**
2300225b98SGreg Roach * Marital status.
2400225b98SGreg Roach */
25c1010edaSGreg Roachabstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface
26c1010edaSGreg Roach{
27cfe766afSGreg Roach    /** @var string Text to display for married males */
2800225b98SGreg Roach    protected $husband = '';
290a921d1eSGreg Roach
30cfe766afSGreg Roach    /** @var string Text to display for married females */
3100225b98SGreg Roach    protected $wife = '';
3200225b98SGreg Roach
33cfe766afSGreg Roach    /** @var string Text to display for unmarried males */
3400225b98SGreg Roach    protected $bachelor = '';
350a921d1eSGreg Roach
36cfe766afSGreg Roach    /** @var string Text to display for unmarried females */
3700225b98SGreg Roach    protected $spinster = '';
3800225b98SGreg Roach
39cfe766afSGreg Roach    /** @var string Text to display for male children */
4000225b98SGreg Roach    protected $boy = '';
410a921d1eSGreg Roach
42cfe766afSGreg Roach    /** @var string Text to display for female children */
4300225b98SGreg Roach    protected $girl = '';
4400225b98SGreg Roach
45cfe766afSGreg Roach    /** @var string Text to display for divorced males */
4600225b98SGreg Roach    protected $divorce = '';
470a921d1eSGreg Roach
48cfe766afSGreg Roach    /** @var string Text to display for divorced females */
4900225b98SGreg Roach    protected $divorcee = '';
5000225b98SGreg Roach
51cfe766afSGreg Roach    /** @var string Text to display for widowed males */
5200225b98SGreg Roach    protected $widower = '';
530a921d1eSGreg Roach
54cfe766afSGreg Roach    /** @var string Text to display for widowed females */
5500225b98SGreg Roach    protected $widow = '';
5600225b98SGreg Roach
57cfe766afSGreg Roach    /** @var int At what age is this individual recorded as an adult */
5800225b98SGreg Roach    protected $age_adult = 15;
5900225b98SGreg Roach
6000225b98SGreg Roach    /**
6100225b98SGreg Roach     * Generate the likely value of this census column, based on available information.
6200225b98SGreg Roach     *
6300225b98SGreg Roach     * @param Individual $individual
6415d603e7SGreg Roach     * @param Individual $head
6500225b98SGreg Roach     *
6600225b98SGreg Roach     * @return string
6700225b98SGreg Roach     */
685a62e0a6SGreg Roach    public function generate(Individual $individual, Individual $head): string
69c1010edaSGreg Roach    {
7000225b98SGreg Roach        $family = $this->spouseFamily($individual);
7100225b98SGreg Roach        $sex    = $individual->getSex();
7200225b98SGreg Roach
732a6fda60SGreg Roach        if ($family === null || count($family->getFacts('MARR')) === 0) {
7400225b98SGreg Roach            if ($this->isChild($individual)) {
7500225b98SGreg Roach                return $this->conditionChild($sex);
7600225b98SGreg Roach            } else {
7700225b98SGreg Roach                return $this->conditionSingle($sex);
7800225b98SGreg Roach            }
7900225b98SGreg Roach        } elseif (count($family->getFacts('DIV')) > 0) {
8000225b98SGreg Roach            return $this->conditionDivorced($sex);
8100225b98SGreg Roach        } else {
82e76c0cf0SGreg Roach            $spouse = $family->getSpouse($individual);
83e76c0cf0SGreg Roach            if ($spouse instanceof Individual && $this->isDead($spouse)) {
84e76c0cf0SGreg Roach                return $this->conditionWidowed($sex);
85e76c0cf0SGreg Roach            } else {
8600225b98SGreg Roach                return $this->conditionMarried($sex);
8700225b98SGreg Roach            }
8800225b98SGreg Roach        }
89e76c0cf0SGreg Roach    }
9000225b98SGreg Roach
9100225b98SGreg Roach    /**
9215d603e7SGreg Roach     * Is the individual a child.
9315d603e7SGreg Roach     *
9415d603e7SGreg Roach     * @param Individual $individual
9515d603e7SGreg Roach     *
9615d603e7SGreg Roach     * @return bool
9715d603e7SGreg Roach     */
988f53f488SRico Sonntag    private function isChild(Individual $individual): bool
99c1010edaSGreg Roach    {
100*8fe32d0dSGreg Roach        $age = Date::getAgeYears($individual->getEstimatedBirthDate(), $this->date());
10115d603e7SGreg Roach
10215d603e7SGreg Roach        return $age < $this->age_adult;
10315d603e7SGreg Roach    }
10415d603e7SGreg Roach
10515d603e7SGreg Roach    /**
10600225b98SGreg Roach     * How is this condition written in a census column.
10700225b98SGreg Roach     *
10807902ed6SScrutinizer Auto-Fixer     * @param string $sex
10900225b98SGreg Roach     *
11000225b98SGreg Roach     * @return string
11100225b98SGreg Roach     */
112c1010edaSGreg Roach    private function conditionChild($sex)
113c1010edaSGreg Roach    {
11400225b98SGreg Roach        if ($sex === 'F') {
11500225b98SGreg Roach            return $this->girl;
11600225b98SGreg Roach        } else {
11700225b98SGreg Roach            return $this->boy;
11800225b98SGreg Roach        }
11900225b98SGreg Roach    }
12000225b98SGreg Roach
12100225b98SGreg Roach    /**
12200225b98SGreg Roach     * How is this condition written in a census column.
12300225b98SGreg Roach     *
12407902ed6SScrutinizer Auto-Fixer     * @param string $sex
12500225b98SGreg Roach     *
12600225b98SGreg Roach     * @return string
12700225b98SGreg Roach     */
128c1010edaSGreg Roach    private function conditionSingle($sex)
129c1010edaSGreg Roach    {
13000225b98SGreg Roach        if ($sex === 'F') {
13100225b98SGreg Roach            return $this->spinster;
13200225b98SGreg Roach        } else {
13300225b98SGreg Roach            return $this->bachelor;
13400225b98SGreg Roach        }
13500225b98SGreg Roach    }
13600225b98SGreg Roach
13700225b98SGreg Roach    /**
138e76c0cf0SGreg Roach     * How is this condition written in a census column.
139e76c0cf0SGreg Roach     *
14007902ed6SScrutinizer Auto-Fixer     * @param string $sex
141e76c0cf0SGreg Roach     *
142e76c0cf0SGreg Roach     * @return string
143e76c0cf0SGreg Roach     */
144c1010edaSGreg Roach    private function conditionDivorced($sex)
145c1010edaSGreg Roach    {
146e76c0cf0SGreg Roach        if ($sex === 'F') {
14715d603e7SGreg Roach            return $this->divorcee;
148e76c0cf0SGreg Roach        } else {
14915d603e7SGreg Roach            return $this->divorce;
150e76c0cf0SGreg Roach        }
151e76c0cf0SGreg Roach    }
152e76c0cf0SGreg Roach
153e76c0cf0SGreg Roach    /**
154e76c0cf0SGreg Roach     * Is the individual dead.
155e76c0cf0SGreg Roach     *
156e76c0cf0SGreg Roach     * @param Individual $individual
157e76c0cf0SGreg Roach     *
158e76c0cf0SGreg Roach     * @return bool
159e76c0cf0SGreg Roach     */
1608f53f488SRico Sonntag    private function isDead(Individual $individual): bool
161c1010edaSGreg Roach    {
162fef3019cSGreg Roach        return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
163e76c0cf0SGreg Roach    }
16415d603e7SGreg Roach
16515d603e7SGreg Roach    /**
16615d603e7SGreg Roach     * How is this condition written in a census column.
16715d603e7SGreg Roach     *
16815d603e7SGreg Roach     * @param string $sex
16915d603e7SGreg Roach     *
17015d603e7SGreg Roach     * @return string
17115d603e7SGreg Roach     */
172c1010edaSGreg Roach    private function conditionWidowed($sex)
173c1010edaSGreg Roach    {
17415d603e7SGreg Roach        if ($sex === 'F') {
17515d603e7SGreg Roach            return $this->widow;
17615d603e7SGreg Roach        } else {
17715d603e7SGreg Roach            return $this->widower;
17815d603e7SGreg Roach        }
17915d603e7SGreg Roach    }
18015d603e7SGreg Roach
18115d603e7SGreg Roach    /**
18215d603e7SGreg Roach     * How is this condition written in a census column.
18315d603e7SGreg Roach     *
18415d603e7SGreg Roach     * @param string $sex
18515d603e7SGreg Roach     *
18615d603e7SGreg Roach     * @return string
18715d603e7SGreg Roach     */
188c1010edaSGreg Roach    private function conditionMarried($sex)
189c1010edaSGreg Roach    {
19015d603e7SGreg Roach        if ($sex === 'F') {
19115d603e7SGreg Roach            return $this->wife;
19215d603e7SGreg Roach        } else {
19315d603e7SGreg Roach            return $this->husband;
19415d603e7SGreg Roach        }
19515d603e7SGreg Roach    }
19600225b98SGreg Roach}
197