LineageNode::addFamily()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage PatronymicLineage
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\PatronymicLineage\Model;
16
17
use Fisharebest\Webtrees\Family;
18
use Fisharebest\Webtrees\Individual;
19
use Illuminate\Support\Collection;
20
use stdClass;
21
22
/**
23
 * Node of the lineage tree, holding data about individuals forming it, and their descendency.
24
 */
25
class LineageNode
26
{
27
    /**
28
     * @var Collection<string, stdClass>  $linked_fams Spouse families linked to the node
29
     */
30
    private Collection $linked_fams;
31
32
    private ?Individual $node_indi;
33
    private LineageRootNode $root_node;
34
    private ?string $alt_surname;
35
36
    /**
37
     * Constructor for Lineage node
38
     *
39
     * @param Individual $node_indi Main individual
40
     * @param LineageRootNode $root_node Node of the lineage root
41
     * @param null|string $alt_surname Follow-up surname
42
     */
43
    public function __construct(?Individual $node_indi = null, LineageRootNode $root_node, $alt_surname = null)
44
    {
45
        $this->node_indi = $node_indi;
46
        $this->root_node = $root_node;
47
        $this->alt_surname = $alt_surname;
48
        $this->linked_fams = new Collection();
49
    }
50
51
    /**
52
     * Add a spouse family to the node
53
     *
54
     * @param Family $fams
55
     * @return stdClass
56
     */
57
    public function addFamily(Family $fams): object
58
    {
59
        if (!$this->linked_fams->has($fams->xref())) {
60
            $this->linked_fams->put($fams->xref(), (object) [
61
                'family'   =>  $fams,
62
                'children' =>  new Collection()
63
            ]);
64
        }
65
        return $this->linked_fams->get($fams->xref());
66
    }
67
68
    /**
69
     * Add a child LineageNode to the node
70
     *
71
     * @param Family $fams
72
     * @param LineageNode $child
73
     */
74
    public function addChild(Family $fams, LineageNode $child = null): void
75
    {
76
        $this->addFamily($fams)->children->push($child);
77
        $this->root_node->incrementChildNodes();
78
    }
79
80
    /**
81
     * Returns the node individual
82
     *
83
     * @return Individual|NULL
84
     */
85
    public function individual(): ?Individual
86
    {
87
        return $this->node_indi;
88
    }
89
90
    /**
91
     * Returns the lineage root node individual
92
     *
93
     * @return LineageRootNode
94
     */
95
    public function rootNode(): LineageRootNode
96
    {
97
        return $this->root_node;
98
    }
99
100
    /**
101
     * Returns the spouse families linked to the node
102
     *
103
     * @return Collection<string, \stdClass>
104
     */
105
    public function families(): Collection
106
    {
107
        return $this->linked_fams;
108
    }
109
110
    /**
111
     * Returns the follow-up surname
112
     *
113
     * @return string
114
     */
115
    public function followUpSurname(): string
116
    {
117
        return $this->alt_surname ?? '';
118
    }
119
120
    /**
121
     * Indicates whether the node has a follow up surname
122
     *
123
     * @return boolean
124
     */
125
    public function hasFollowUpSurname(): bool
126
    {
127
        return mb_strlen($this->followUpSurname()) > 0 ;
128
    }
129
}
130