1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPAlgorithms\GraphTools\Graph\Node; |
4
|
|
|
|
5
|
|
|
use PHPAlgorithms\GraphTools\Abstracts\BuilderAbstract; |
6
|
|
|
use PHPAlgorithms\GraphTools\Graph\Builder as GraphBuilder; |
7
|
|
|
use PHPAlgorithms\GraphTools\Graph\Node; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Builder |
11
|
|
|
* @package PHPAlgorithms\GraphTools\Graph\Node |
12
|
|
|
* @method integer|null getX() |
13
|
|
|
* @method integer|null getY() |
14
|
|
|
* @method integer|null getZ() |
15
|
|
|
* @method Builder setX(integer|null $x) |
16
|
|
|
* @method Builder setY(integer|null $y) |
17
|
|
|
* @method Builder setZ(integer|null $z) |
18
|
|
|
* @property integer|null $x |
19
|
|
|
* @property integer|null $y |
20
|
|
|
* @property integer|null $z |
21
|
|
|
*/ |
22
|
|
|
class Builder extends BuilderAbstract |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string[] $allowedGet |
26
|
|
|
*/ |
27
|
|
|
protected $allowedGet = array('x', 'y', 'z'); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string[] $allowedSet |
31
|
|
|
*/ |
32
|
|
|
protected $allowedSet = array('x', 'y', 'z'); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var GraphBuilder|null $builder |
36
|
|
|
*/ |
37
|
|
|
private $builder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var integer $x |
41
|
|
|
*/ |
42
|
|
|
protected $x; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer $y |
46
|
|
|
*/ |
47
|
|
|
protected $y; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var integer $z |
51
|
|
|
*/ |
52
|
|
|
protected $z; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Builder constructor. |
56
|
|
|
* @param GraphBuilder|null $builder |
57
|
|
|
*/ |
58
|
|
|
public function __construct(?GraphBuilder $builder = null) |
59
|
|
|
{ |
60
|
|
|
$this->builder = $builder; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Node|GraphBuilder |
65
|
|
|
*/ |
66
|
|
|
public function build() |
67
|
|
|
{ |
68
|
|
|
$node = new Node($this->x, $this->y, $this->z); |
69
|
|
|
|
70
|
|
|
if (empty($this->builder)) { |
71
|
|
|
return $node; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->builder->addNode($node); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.