1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Helper.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4+ |
6
|
|
|
* |
7
|
|
|
* @author pgaultier |
8
|
|
|
* @copyright 2010-2016 Ibitux |
9
|
|
|
* @license http://www.ibitux.com/license license |
10
|
|
|
* @version XXX |
11
|
|
|
* @link http://www.ibitux.com |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace sweelix\tree; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Helper class to ease node creation / management |
18
|
|
|
* |
19
|
|
|
* @author Philippe Gaultier <[email protected]> |
20
|
|
|
* @copyright 2010-2016 Philippe Gaultier |
21
|
|
|
* @license http://www.sweelix.net/license license |
22
|
|
|
* @version XXX |
23
|
|
|
* @link http://www.sweelix.net |
24
|
|
|
* @package sweelix\tree |
25
|
|
|
* @since XXX |
26
|
|
|
*/ |
27
|
|
|
class Helper |
28
|
|
|
{ |
29
|
|
|
// MatrixNode helpers |
30
|
|
|
|
31
|
|
|
public static function buildMoveMatrix(Matrix $fromMatrix, Matrix $toMatrix, $bump = 0) |
32
|
|
|
{ |
33
|
|
|
$from = clone $fromMatrix; |
34
|
|
|
$to = clone $toMatrix; |
35
|
|
|
$from->adjugate(); |
36
|
|
|
$from->multiply(-1); |
37
|
|
|
$bumpMatrix = self::buildBumpMatrix($bump); |
38
|
|
|
$to->multiply($bumpMatrix); |
39
|
|
|
$to->multiply($from); |
40
|
|
|
return $to; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $path path in dot notation |
45
|
|
|
* @return array matrix notation |
46
|
|
|
* @since XXX |
47
|
|
|
*/ |
48
|
|
|
public static function convertPathToMatrix($path) |
49
|
|
|
{ |
50
|
|
|
$matrix = new Matrix([ |
51
|
|
|
0, 1, |
52
|
|
|
1, 0, |
53
|
|
|
]); |
54
|
|
|
$nodePath = explode('.', $path); |
55
|
|
|
foreach ($nodePath as $segment) { |
56
|
|
|
$matrix->multiply(self::buildSegmentMatrix($segment)); |
57
|
|
|
} |
58
|
|
|
return $matrix; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $matrix path in matrix notation |
63
|
|
|
* @return string dot notation |
64
|
|
|
* @since XXX |
65
|
|
|
*/ |
66
|
|
|
public static function convertMatrixToPath(Matrix $matrix) |
67
|
|
|
{ |
68
|
|
|
$nodePath = []; |
69
|
|
|
$currentMatrix = clone $matrix; |
70
|
|
|
do { |
71
|
|
|
$nodePath[] = self::getLastSegment($currentMatrix); |
72
|
|
|
$currentMatrix = self::extractParentMatrixFromMatrix($currentMatrix); |
73
|
|
|
} while ($currentMatrix !== null); |
74
|
|
|
$nodePath = array_reverse($nodePath); |
75
|
|
|
return implode('.', $nodePath); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Extract parent matrix from matrix. null if current matrix is root |
80
|
|
|
* @param array $matrix |
81
|
|
|
* @return array|null |
82
|
|
|
* @since XXX |
83
|
|
|
*/ |
84
|
|
|
public static function extractParentMatrixFromMatrix(Matrix $matrix) |
85
|
|
|
{ |
86
|
|
|
$parentMatrix = null; |
87
|
|
|
if (($matrix->c > 0) && ($matrix->d > 0)) { |
88
|
|
|
$leafMatrix = self::buildSegmentMatrix(self::getLastSegment($matrix)); |
89
|
|
|
$leafMatrix->inverse(); |
90
|
|
|
$matrix->multiply($leafMatrix); |
91
|
|
|
if ($matrix->a > 0) { |
92
|
|
|
$parentMatrix = $matrix; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return $parentMatrix; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function getLastSegment($element) |
99
|
|
|
{ |
100
|
|
|
if ($element instanceof Matrix) { |
101
|
|
|
$lastSegment = (int) ($element->a / ($element->b - $element->a)); |
102
|
|
|
} else { |
103
|
|
|
$path = explode('.', $element); |
104
|
|
|
$lastSegment = end($path); |
105
|
|
|
} |
106
|
|
|
return $lastSegment; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function buildSegmentMatrix($segment) |
110
|
|
|
{ |
111
|
|
|
if ($segment <= 0) { |
112
|
|
|
throw new InvalidSegmentException(); |
113
|
|
|
} |
114
|
|
|
return new Matrix([ |
115
|
|
|
1, 1, |
116
|
|
|
$segment, $segment + 1, |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function buildBumpMatrix($offset = 0) |
121
|
|
|
{ |
122
|
|
|
return new Matrix([ |
123
|
|
|
1, 0, |
124
|
|
|
$offset, 1, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
} |