xref: /webtrees/app/Services/ChartService.php (revision 0c0910bf0f275a14f35d2ccdf698f91f79e269d4)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Services;
20
21use Fisharebest\Webtrees\Family;
22use Fisharebest\Webtrees\Individual;
23use Illuminate\Support\Collection;
24
25/**
26 * Find ancestors, descendants, cousins, etc for drawing charts.
27 */
28class ChartService
29{
30    /**
31     * Find the ancestors of an individual, indexed by their Sosa-Stradonitz number.
32     *
33     * @param Individual $individual  Start with this individual
34     * @param int        $generations Fetch this number of generations
35     *
36     * @return Collection
37     */
38    public function sosaStradonitzAncestors(Individual $individual, int $generations): Collection
39    {
40        $ancestors = [1 => $individual];
41
42        $queue = [1];
43
44        $max = 2 ** ($generations - 1);
45
46        while (!empty($queue)) {
47            $sosa_stradonitz_number = array_shift($queue);
48
49            if ($sosa_stradonitz_number >= $max) {
50                break;
51            }
52
53            $family = $ancestors[$sosa_stradonitz_number]->primaryChildFamily();
54
55            if ($family instanceof Family) {
56                if ($family->husband() instanceof Individual) {
57                    $ancestors[$sosa_stradonitz_number * 2] = $family->husband();
58                    $queue[] = $sosa_stradonitz_number * 2;
59                }
60
61                if ($family->wife() instanceof Individual) {
62                    $ancestors[$sosa_stradonitz_number * 2 + 1] = $family->wife();
63                    $queue[] = $sosa_stradonitz_number * 2 + 1;
64                }
65            }
66        }
67
68        return new Collection($ancestors);
69    }
70
71    /**
72     * Find the descendants of an individual.
73     *
74     * @param Individual $individual  Start with this individual
75     * @param int        $generations Fetch this number of generations
76     *
77     * @return Collection
78     */
79    public function descendants(Individual $individual, int $generations): Collection
80    {
81        $descendants = new Collection([$individual]);
82
83        if ($generations > 0) {
84            foreach ($individual->spouseFamilies() as $family) {
85                foreach ($family->children() as $child) {
86                    $descendants = $descendants->merge($this->descendants($child, $generations - 1));
87                }
88            }
89        }
90
91        return $descendants;
92    }
93
94    /**
95     * Find the descendants of an individual.
96     *
97     * @param Individual $individual  Start with this individual
98     * @param int        $generations Fetch this number of generations
99     *
100     * @return Collection
101     */
102    public function descendantFamilies(Individual $individual, int $generations): Collection
103    {
104        $descendants = new Collection($individual->spouseFamilies());
105
106        if ($generations > 0) {
107            foreach ($descendants as $family) {
108                foreach ($family->children() as $child) {
109                    $descendants = $descendants->merge($this->descendantFamilies($child, $generations - 1));
110                }
111            }
112        }
113
114        return $descendants;
115    }
116}
117