1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
<?php
namespace Gedcomx\Rs\Client\Util;
use Gedcomx\Conclusion\Person;
use Gedcomx\Gedcomx;
/**
* A model representation of descendancy.
*
* Class DescendancyTree
*
* @package Gedcomx\Rs\Client\Util
*/
class DescendancyTree
{
private $root = null;
/**
* Constructs a new descendancy tree using the specified model.
*
* @param \Gedcomx\Gedcomx $gx
*/
function __construct(Gedcomx $gx)
{
$this->root = $this->buildTree($gx);
}
/**
* Builds an array of persons to be placed in the descendancy tree.
*
* @param \Gedcomx\Gedcomx $gx
*
* @return \Gedcomx\Rs\Client\Util\DescendancyNode|null
*/
protected function buildTree(Gedcomx $gx)
{
$root = null;
if ($gx->getPersons() != null && count($gx->getPersons()) > 0) {
$rootArray = array();
foreach ($gx->getPersons() as $person) {
if ($person->getDisplayExtension() != null && $person->getDisplayExtension()->getDescendancyNumber() != null) {
$number = $person->getDisplayExtension()->getDescendancyNumber();
$spouse = substr($number, -2) === "-S" || substr($number, -2) === "-s";
if ($spouse) {
$number = substr($number, 0, strlen($number) - 2);
}
$coordinates = $this->parseCoordinates($number);
$current =& $rootArray;
$i = 0;
$node = null;
while ($current !== null) {
$coordinate = $coordinates[$i];
while (count($current) < $coordinate) {
array_push($current, null);
}
$node = $current[$coordinate - 1];
if ($node == null) {
$node = new DescendancyNode();
$current[$coordinate - 1] = $node;
}
unset($current);
if (++$i < count($coordinates)) {
//if we still have another generation to descend, make sure the list is initialized.
$children =& $node->Children;
if ($children == null) {
$children = array();
$node->Children = $children;
}
$current =& $children;
} else {
$current = null;
}
}
if ($spouse) {
$node->Spouse = $person;
} else {
$node->Person = $person;
}
}
}
if (count($rootArray) > 0) {
$root = $rootArray[0];
}
}
return $root;
}
/**
* Gets the root person of the descendancy tree.
*
* @return \Gedcomx\Rs\Client\Util\DescendancyNode|null
*/
function getRoot()
{
return $this->root;
}
/**
* Parses the coordinates of the specified d'Aboville number. See remarks.
* More information on a d'Aboville number can be found here: {@link http://en.wikipedia.org/wiki/Genealogical_numbering_system#d.27Aboville_System}.
*
* @param $number
*
* @return array
*/
protected function parseCoordinates($number)
{
$coords = array();
$current = "";
for ($i = 0; $i < strlen($number); $i++) {
$ch = $number[$i];
if ($ch == '.') {
array_push($coords, $current);
$current = "";
} else {
$current = $current . $ch;
}
}
array_push($coords, $current);
$coordinates = array();
for ($i = 0; $i < count($coords); $i++) {
$num = $coords[$i];
$coordinates[$i] = (int)$num;
}
return $coordinates;
}
}
/**
* Represents a person, spouse, and descendancy in a tree.
*
* Class DescendancyNode
*
* @package Gedcomx\Rs\Client\Util
*/
class DescendancyNode
{
public $Person;
public $Spouse;
public $Children;
/**
* Gets the main person of a tree.
* @return mixed
*/
public function getPerson()
{
return $this->Person;
}
/**
* Sets the main person of a tree.
*
* @param \Gedcomx\Conclusion\Person $person
*/
public function setPerson(Person $person)
{
$this->Person = $person;
}
/**
* Gets the spouse of the main person.
*
* @return mixed
*/
public function getSpouse()
{
return $this->Spouse;
}
/**
* Sets the spouse of the main person.
*
* @param \Gedcomx\Conclusion\Person $spouse
*/
public function setSpouse(Person $spouse)
{
$this->Spouse = $spouse;
}
/**
* Gets the children of the main person
*
* @return mixed
*/
public function getChildren()
{
return $this->Children;
}
/**
* Sets the children of the main person
*
* @param array $children
*/
public function setChildren(array $children)
{
$this->Children = $children;
}
}