|
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\Base; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
|
13
|
|
|
use Railt\SDL\Base\Definitions\BaseDefinition; |
|
14
|
|
|
use Railt\SDL\Base\Invocations\Directive\BaseDirectivesContainer; |
|
15
|
|
|
use Railt\SDL\Contracts\Definitions\Definition; |
|
16
|
|
|
use Railt\SDL\Contracts\Definitions\SchemaDefinition; |
|
17
|
|
|
use Railt\SDL\Contracts\Definitions\TypeDefinition; |
|
18
|
|
|
use Railt\SDL\Contracts\Document; |
|
19
|
|
|
use Railt\SDL\Contracts\Type; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class BaseDocument |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class BaseDocument extends BaseDefinition implements Document |
|
25
|
|
|
{ |
|
26
|
|
|
use BaseDirectivesContainer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Document type name |
|
30
|
|
|
*/ |
|
31
|
|
|
protected const TYPE_NAME = Type::DOCUMENT; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var SchemaDefinition|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $schema; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array|TypeDefinition[] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $types = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array|Definition[] |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $definitions = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Readable |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $file; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return null|SchemaDefinition |
|
55
|
|
|
*/ |
|
56
|
48 |
|
public function getSchema(): ?SchemaDefinition |
|
57
|
|
|
{ |
|
58
|
48 |
|
if ($this->schema === null) { |
|
59
|
48 |
|
foreach ($this->types as $type) { |
|
60
|
48 |
|
if ($type instanceof SchemaDefinition) { |
|
61
|
48 |
|
return $this->schema = $type; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->schema; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return iterable |
|
71
|
|
|
*/ |
|
72
|
7189 |
|
public function getTypeDefinitions(): iterable |
|
73
|
|
|
{ |
|
74
|
7189 |
|
return \array_values($this->types); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @return null|TypeDefinition |
|
80
|
|
|
*/ |
|
81
|
307 |
|
public function getTypeDefinition(string $name): ?TypeDefinition |
|
82
|
|
|
{ |
|
83
|
307 |
|
return $this->types[$name] ?? null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
18 |
|
public function hasTypeDefinition(string $name): bool |
|
91
|
|
|
{ |
|
92
|
18 |
|
return \array_key_exists($name, $this->types); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
18 |
|
public function getNumberOfTypeDefinitions(): int |
|
99
|
|
|
{ |
|
100
|
18 |
|
return \count($this->types); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return iterable |
|
105
|
|
|
*/ |
|
106
|
7165 |
|
public function getDefinitions(): iterable |
|
107
|
|
|
{ |
|
108
|
7165 |
|
return \array_values(\array_merge($this->types, $this->definitions)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Readable |
|
113
|
|
|
*/ |
|
114
|
3651 |
|
public function getFile(): Readable |
|
115
|
|
|
{ |
|
116
|
3651 |
|
return $this->file; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
3651 |
|
final public function getFileName(): string |
|
123
|
|
|
{ |
|
124
|
3651 |
|
return $this->file->getPathname(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getContents(): string |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->file->getContents(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
1257 |
|
public function __sleep(): array |
|
139
|
|
|
{ |
|
140
|
1257 |
|
return \array_merge(parent::__sleep(), [ |
|
141
|
|
|
// File |
|
142
|
1257 |
|
'file', |
|
143
|
|
|
|
|
144
|
|
|
// instanceof TypeDefinition |
|
145
|
|
|
'types', |
|
146
|
|
|
|
|
147
|
|
|
// instanceof Definition |
|
148
|
|
|
'definitions', |
|
149
|
|
|
|
|
150
|
|
|
// Directives |
|
151
|
|
|
'directives', |
|
152
|
|
|
]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
66 |
|
public function getTypeName(): string |
|
159
|
|
|
{ |
|
160
|
66 |
|
return self::TYPE_NAME; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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.