|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPAlgorithms\GraphTools\Graph\Connection; |
|
4
|
|
|
|
|
5
|
|
|
use PHPAlgorithms\GraphTools\Abstracts\BuilderAbstract; |
|
6
|
|
|
use PHPAlgorithms\GraphTools\Graph\Builder as GraphBuilder; |
|
7
|
|
|
use PHPAlgorithms\GraphTools\Graph\Connection; |
|
8
|
|
|
use PHPAlgorithms\GraphTools\Graph\Node; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Builder |
|
12
|
|
|
* @package PHPAlgorithms\GraphTools\Graph\Connection |
|
13
|
|
|
* @method Node|null getFrom() |
|
14
|
|
|
* @method Node|null getTo() |
|
15
|
|
|
* @method float|null getWeight() |
|
16
|
|
|
* @method Builder setFrom(Node|null $from) |
|
17
|
|
|
* @method Builder setTo(Node|null $to) |
|
18
|
|
|
* @method Builder setWeight(float|null $weight) |
|
19
|
|
|
* @property Node|null $from |
|
20
|
|
|
* @property Node|null $to |
|
21
|
|
|
* @property float|null $weight |
|
22
|
|
|
*/ |
|
23
|
|
|
class Builder extends BuilderAbstract |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string[] $allowedGet |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $allowedGet = array('from', 'to', 'weight'); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string[] $allowedSet |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $allowedSet = array('from', 'to', 'weight'); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var GraphBuilder|null $builder |
|
37
|
|
|
*/ |
|
38
|
|
|
private $builder; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Node $from |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $from; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Node $to |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $to; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var float|null $weight |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $weight; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Builder constructor. |
|
57
|
|
|
* @param GraphBuilder|null $builder |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(?GraphBuilder $builder = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->builder = $builder; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Connection|GraphBuilder |
|
66
|
|
|
*/ |
|
67
|
|
|
public function build() |
|
68
|
|
|
{ |
|
69
|
|
|
$connection = new Connection($this->from, $this->to, $this->weight); |
|
70
|
|
|
|
|
71
|
|
|
if (empty($this->builder)) { |
|
72
|
|
|
return $connection; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->builder->addConnection($connection); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.