|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPAlgorithms\GraphTools; |
|
4
|
|
|
|
|
5
|
|
|
use PHPAlgorithms\GraphTools\Exceptions\LineException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Line { |
|
8
|
|
|
private static function checkPoint($point) |
|
9
|
|
|
{ |
|
10
|
|
|
if (!($point instanceof AbstractPoint)) { |
|
11
|
|
|
throw new LineException('This is not a point'); |
|
12
|
|
|
} |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
private static function getDimensionNumber($point) |
|
16
|
|
|
{ |
|
17
|
|
|
$pointDimensions = array(AbstractPoint::class, Point1D::class, Point2D::class, Point3D::class, Point4D::class); |
|
18
|
|
|
|
|
19
|
|
|
foreach ($pointDimensions as $dimension => $pointDimension) |
|
20
|
|
|
{ |
|
21
|
|
|
if (!($point instanceof $pointDimension)) { |
|
22
|
|
|
return --$dimension; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return $dimension; |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private static function getLowestPointsDimension($from, $to) |
|
30
|
|
|
{ |
|
31
|
|
|
$fromDimension = self::getDimensionNumber($from); |
|
32
|
|
|
$toDimension = self::getDimensionNumber($to); |
|
33
|
|
|
|
|
34
|
|
|
switch (true) { |
|
35
|
|
|
case $fromDimension <= $toDimension: |
|
36
|
|
|
return $fromDimension; |
|
37
|
|
|
case $fromDimension > $toDimension: |
|
38
|
|
|
return $toDimension; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function create($from, $to) |
|
43
|
|
|
{ |
|
44
|
|
|
self::checkPoint($from); |
|
45
|
|
|
self::checkPoint($to); |
|
46
|
|
|
|
|
47
|
|
|
$lowestDimension = self::getLowestPointsDimension($from, $to); |
|
48
|
|
|
|
|
49
|
|
|
switch ($lowestDimension) { |
|
50
|
|
|
case 0: |
|
|
|
|
|
|
51
|
|
|
return new AbstractLine($from, $to); |
|
52
|
|
|
case 1: |
|
53
|
|
|
return new Line1D($from, $to); |
|
54
|
|
|
case 2: |
|
55
|
|
|
return new Line2D($from, $to); |
|
56
|
|
|
case 3: |
|
57
|
|
|
return new Line3D($from, $to); |
|
58
|
|
|
case 4: |
|
59
|
|
|
return new Line4D($from, $to); |
|
60
|
|
|
default: |
|
61
|
|
|
throw new LineException('Unknown dimension'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: