-
Notifications
You must be signed in to change notification settings - Fork 4
/
lineage-class.php
166 lines (144 loc) · 4.29 KB
/
lineage-class.php
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
<?php
class lineage {
//array that stores data making up the family tree
private $tree = array();
private $cousin_prefixes = array(1 => "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth");
private $cousin_suffixes = array("", "once removed", "twice removed", "thrice removed", "4 times removed", "5 times removed", "6 times removed", "7 times removed", "8 times removed", "9 times removed");
function __construct($input, $type = 'array') {
switch($type) {
case 'array':
$this->tree = $input;
break;
case 'xml':
$this->parse_xml($input);
break;
case 'csv':
$this->parse_csv($input);
break;
case 'json':
$this->parse_json($input);
break;
default:
echo "error: type does not exist";
break;
}
return TRUE;
}
/* Parsing Functions */
private function parse_xml($xml) {
$parser = new SimpleXMLElement($xml);
$i = 1;
//parse each element of XML into tree
foreach($parser->mem as $mem) {
$this->tree[(int)$mem['id']] = array("id"=>(int) $mem['id'], "parent"=>(int) $mem['parent'], "gender"=>(int) $mem['gender'], "name"=>(string) $mem);
$i++;
}
return TRUE;
}
private function parse_csv($csv) {
$this->tree = array();
//seperate lines in file
$data = str_getcsv($csv, "\n");
//assumes CSV file has a header row, and unsets it from the tree
unset($data[0]);
//parses each line in CSV into the tree
foreach($data as &$row) {
$row = str_getcsv($row, ",");
$this->tree[$row[0]] = array("id" => $row[0], "parent" => $row[1], "gender" => $row[2], "name" => $row[3]);
}
return TRUE;
}
private function parse_json($json) {
$data = json_decode($json, TRUE);
//for every item, set id as array key
foreach ($data as $row) {
$this->tree[$row["id"]] = $row;
}
return TRUE;
}
private function gen($a) {
$gen = 1;
while($a != 1) {
$a = $this->tree[$a]["parent"];
$gen++;
}
return $gen;
}
private function gendif($a, $b) {
$dif = $this->gen($a) - $this->gen($b);
return abs($dif);
}
private function gen_prefix($g, $grand = FALSE) {
if($g == 1) {
return "";
}
if($g == 2) {
return ($grand == FALSE) ? "grand" : "great ";
}
if($g > 2) {
$return = ($grand == FALSE) ? "grand" : "great ";
for($i = 1; $i <= $g - 2; $i++) {
$return = "great ".$return;
}
return $return;
}
}
private function gen_anc($a, $gen) {
if($this->gen($a) <= $gen) {
return $a;
}
while($this->gen($a) != $gen) {
$a = $this->tree[$a]["parent"];
}
return $a;
}
private function common($a, $b) {
if($a == $b) {
return 0;
}
$ga = $this->gen($a);
$gb = $this->gen($b);
while($a != $b) {
if($ga < $gb) {
$b = $this->tree[$b]["parent"];
$gb = $gb - 1;
} elseif($ga > $gb) {
$a = $this->tree[$a]["parent"];
$ga = $ga - 1;
} elseif($ga == $gb) {
$a = $this->tree[$a]["parent"];
$b = $this->tree[$b]["parent"];
}
if($a == 1 or $b == 1) {
return 1;
}
}
return $a;
}
public function findRelation($a, $b) {
if($this->tree[$a]["parent"] == $this->tree[$b]["parent"]) {
return ($a != $b ? "sibling" : "self");
}
if($a == $this->common($a, $b)) {
return $this->gen_prefix($this->gendif($a, $b))."parent";
}
if($b == $this->common($a, $b)) {
return $this->gen_prefix($this->gendif($a, $b))."child";
}
if($this->tree[$a]["parent"] == $this->tree[$this->gen_anc($b, $this->gen($a))]["parent"]) {
return $this->gen_prefix($this->gendif($a, $b))."aunt/uncle";
}
if($this->tree[$b]["parent"] == $this->tree[$this->gen_anc($a, $this->gen($b))]["parent"]) {
return $this->gen_prefix($this->gendif($a, $b))."niece/nephew";
}
if(2 <= $this->gen($a) - $this->gen($this->common($a, $b))) {
if($this->gen($a) <= $this->gen($b)) {
$deg = ($this->gen($a) - $this->gen($this->common($a, $b))) - 1;
} else {
$deg = ($this->gen($b) - $this->gen($this->common($a, $b))) - 1;
}
}
return $this->cousin_prefixes[$deg]." cousin ".$this->cousin_suffixes[$this->gendif($a, $b)];
}
}
?>