|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Frontend\Record; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
13
|
|
|
use Railt\Parser\Finder; |
|
14
|
|
|
use Railt\SDL\Frontend\Type\TypeInterface; |
|
15
|
|
|
use Railt\SDL\Frontend\Type\TypeNameInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Record |
|
19
|
|
|
*/ |
|
20
|
|
|
class Record implements RecordInterface, \JsonSerializable |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var TypeNameInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $name; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RuleInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $ast; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array|ArgumentInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $arguments = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var TypeInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $type; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Record constructor. |
|
43
|
|
|
* @param TypeNameInterface $name |
|
44
|
|
|
* @param TypeInterface $type |
|
45
|
|
|
* @param RuleInterface $ast |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(TypeNameInterface $name, TypeInterface $type, RuleInterface $ast) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->name = $name; |
|
50
|
|
|
$this->ast = $ast; |
|
51
|
|
|
$this->type = $type; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return TypeInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getType(): TypeInterface |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->type; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return iterable|array |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getArguments(): iterable |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->arguments; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ArgumentInterface $argument |
|
72
|
|
|
* @return RecordInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function addArgument(ArgumentInterface $argument): RecordInterface |
|
75
|
|
|
{ |
|
76
|
|
|
$this->arguments[$argument->getName()] = $argument; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return TypeNameInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getName(): TypeNameInterface |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $query |
|
91
|
|
|
* @return Finder |
|
92
|
|
|
*/ |
|
93
|
|
|
public function find(string $query): Finder |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->ast->find($query); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function jsonSerialize(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
|
|
'name' => $this->name, |
|
105
|
|
|
'type' => $this->type, |
|
106
|
|
|
'arguments' => $this->arguments, |
|
107
|
|
|
]; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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.