-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
RelationStats.php
110 lines (94 loc) · 2.99 KB
/
RelationStats.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
<?php
/**
* Contains abstract class to hold relation preferences/statistics
*/
declare(strict_types=1);
namespace PhpMyAdmin\Plugins\Schema;
use function abs;
use function array_search;
use function min;
/**
* Relations preferences/statistics
*
* This class fetches the table master and foreign fields positions
* and helps in generating the Table references and then connects
* master table's master field to foreign table's foreign key.
*
* @abstract
*/
abstract class RelationStats
{
public mixed $xSrc;
public mixed $ySrc;
public int $srcDir;
public int $destDir;
public mixed $xDest;
public mixed $yDest;
public int $wTick = 0;
/**
* @param object $diagram The diagram
* @param TableStats $masterTable The master table name
* @param string $masterField The relation field in the master table
* @param TableStats $foreignTable The foreign table name
* @param string $foreignField The relation field in the foreign table
*/
public function __construct(
protected object $diagram,
TableStats $masterTable,
string $masterField,
TableStats $foreignTable,
string $foreignField,
) {
$srcPos = $this->getXy($masterTable, $masterField);
$destPos = $this->getXy($foreignTable, $foreignField);
// [0] is x-left
// [1] is x-right
// [2] is y
$srcLeft = $srcPos[0] - $this->wTick;
$srcRight = $srcPos[1] + $this->wTick;
$destLeft = $destPos[0] - $this->wTick;
$destRight = $destPos[1] + $this->wTick;
$d1 = abs($srcLeft - $destLeft);
$d2 = abs($srcRight - $destLeft);
$d3 = abs($srcLeft - $destRight);
$d4 = abs($srcRight - $destRight);
$d = min($d1, $d2, $d3, $d4);
if ($d == $d1) {
$this->xSrc = $srcPos[0];
$this->srcDir = -1;
$this->xDest = $destPos[0];
$this->destDir = -1;
} elseif ($d == $d2) {
$this->xSrc = $srcPos[1];
$this->srcDir = 1;
$this->xDest = $destPos[0];
$this->destDir = -1;
} elseif ($d == $d3) {
$this->xSrc = $srcPos[0];
$this->srcDir = -1;
$this->xDest = $destPos[1];
$this->destDir = 1;
} else {
$this->xSrc = $srcPos[1];
$this->srcDir = 1;
$this->xDest = $destPos[1];
$this->destDir = 1;
}
$this->ySrc = $srcPos[2];
$this->yDest = $destPos[2];
}
/**
* Gets arrows coordinates
*
* @param TableStats $table The table
* @param string $column The relation column name
*
* @return mixed[] Arrows coordinates
*/
private function getXy(TableStats $table, string $column): array
{
$pos = array_search($column, $table->fields);
// x_left, x_right, y
return [$table->x, $table->x + $table->width, $table->y + ($pos + 1.5) * $table->heightCell];
}
}