| Conditions | 9 |
| Paths | 27 |
| Total Lines | 42 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 29 |
| CRAP Score | 9 |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 30 | 4 | public function fromArray(array $array) |
|
| 31 | { |
||
| 32 | 4 | $root = new Node(null); |
|
| 33 | 4 | $map = array(); |
|
| 34 | 4 | $map[0] = $root; |
|
| 35 | |||
| 36 | // Create an entry in $map for every item in $a |
||
| 37 | 4 | foreach ($array as $element) { |
|
| 38 | 4 | if (3 !== count($element)) { |
|
| 39 | 1 | throw new Exception('Each array must have 3 elements.'); |
|
| 40 | } |
||
| 41 | |||
| 42 | 3 | $map[$element[0]] = new Node($element[2]); |
|
| 43 | 3 | } |
|
| 44 | |||
| 45 | // |
||
| 46 | 3 | foreach ($array as $element) { |
|
| 47 | 3 | if (empty($element[1])) { |
|
| 48 | 2 | $element[1] = 0; |
|
| 49 | 2 | } |
|
| 50 | |||
| 51 | 3 | $found = false; |
|
| 52 | 3 | $i = 0; |
|
| 53 | 3 | $keys = array_keys($map); |
|
| 54 | 3 | $cnt = count($keys); |
|
| 55 | 3 | while (!$found && $i < $cnt) { |
|
| 56 | 3 | if ($keys[$i] === $element[1]) { |
|
| 57 | 2 | $map[$keys[$i]]->addChild($map[$element[0]]); |
|
| 58 | 2 | $found = true; |
|
| 59 | 2 | } else { |
|
| 60 | 3 | $i++; |
|
| 61 | } |
||
| 62 | 3 | } |
|
| 63 | 3 | if (!$found) { |
|
| 64 | // Error |
||
| 65 | 1 | throw new Exception('Data structure does not seem to be consistent. ' |
|
| 66 | 1 | . 'Key "' . $element[1] . '" could not be found.'); |
|
| 67 | } |
||
| 68 | 2 | } |
|
| 69 | |||
| 70 | 2 | return $root; |
|
| 71 | } |
||
| 72 | } |
||
| 73 |