xref: /webtrees/app/Census/CensusColumnYearsMarried.php (revision 8fcd0d32e56ee262912bbdb593202cfd1cbc1615)
1ef21b467SGreg Roach<?php
2ef21b467SGreg Roach/**
3ef21b467SGreg Roach * webtrees: online genealogy
4*8fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5ef21b467SGreg Roach * This program is free software: you can redistribute it and/or modify
6ef21b467SGreg Roach * it under the terms of the GNU General Public License as published by
7ef21b467SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8ef21b467SGreg Roach * (at your option) any later version.
9ef21b467SGreg Roach * This program is distributed in the hope that it will be useful,
10ef21b467SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11ef21b467SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12ef21b467SGreg Roach * GNU General Public License for more details.
13ef21b467SGreg Roach * You should have received a copy of the GNU General Public License
14ef21b467SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15ef21b467SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
1715d603e7SGreg Roach
18ef21b467SGreg Roachnamespace Fisharebest\Webtrees\Census;
19ef21b467SGreg Roach
207338314fSGreg Roachuse Fisharebest\Webtrees\Date;
21ef21b467SGreg Roachuse Fisharebest\Webtrees\Individual;
22ef21b467SGreg Roach
23ef21b467SGreg Roach/**
24ef21b467SGreg Roach * For how many years has the individual been married.
25ef21b467SGreg Roach */
26c1010edaSGreg Roachclass CensusColumnYearsMarried extends AbstractCensusColumn implements CensusColumnInterface
27c1010edaSGreg Roach{
28ef21b467SGreg Roach    /**
29ef21b467SGreg Roach     * Generate the likely value of this census column, based on available information.
30ef21b467SGreg Roach     *
31ef21b467SGreg Roach     * @param Individual $individual
3215d603e7SGreg Roach     * @param Individual $head
33ef21b467SGreg Roach     *
34ef21b467SGreg Roach     * @return string
35ef21b467SGreg Roach     */
365a62e0a6SGreg Roach    public function generate(Individual $individual, Individual $head): string
37c1010edaSGreg Roach    {
387338314fSGreg Roach        $marriage_date = null;
397338314fSGreg Roach
407338314fSGreg Roach        foreach ($individual->getSpouseFamilies() as $family) {
418d0ebef0SGreg Roach            foreach ($family->facts(['MARR'], true) as $fact) {
422decada7SGreg Roach                if ($fact->date()->isOK() && Date::compare($fact->date(), $this->date()) <= 0) {
432decada7SGreg Roach                    $marriage_date = $fact->date();
447338314fSGreg Roach                }
457338314fSGreg Roach            }
467338314fSGreg Roach        }
477338314fSGreg Roach
487338314fSGreg Roach        if ($marriage_date === null) {
49ef21b467SGreg Roach            return '';
507338314fSGreg Roach        }
51b2ce94c6SRico Sonntag
52b2ce94c6SRico Sonntag        return (string) Date::getAgeYears($marriage_date, $this->date());
53ef21b467SGreg Roach    }
54ef21b467SGreg Roach}
55