|
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\Definitions; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Base\Dependent\Field\BaseFieldsContainer; |
|
13
|
|
|
use Railt\SDL\Base\Invocations\Directive\BaseDirectivesContainer; |
|
14
|
|
|
use Railt\SDL\Contracts\Definitions\InterfaceDefinition; |
|
15
|
|
|
use Railt\SDL\Contracts\Definitions\ObjectDefinition; |
|
16
|
|
|
use Railt\SDL\Contracts\Type; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class BaseObject |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class BaseObject extends BaseTypeDefinition implements ObjectDefinition |
|
22
|
|
|
{ |
|
23
|
|
|
use BaseDirectivesContainer; |
|
24
|
|
|
use BaseFieldsContainer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Object type name |
|
28
|
|
|
*/ |
|
29
|
|
|
protected const TYPE_NAME = Type::OBJECT; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array|InterfaceDefinition[] |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $interfaces = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return iterable|InterfaceDefinition[] |
|
38
|
|
|
*/ |
|
39
|
2822 |
|
public function getInterfaces(): iterable |
|
40
|
|
|
{ |
|
41
|
2822 |
|
return \array_values($this->interfaces); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $name |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
26 |
|
public function hasInterface(string $name): bool |
|
49
|
|
|
{ |
|
50
|
26 |
|
return \array_key_exists($name, $this->interfaces); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
* @return null|InterfaceDefinition |
|
56
|
|
|
*/ |
|
57
|
30 |
|
public function getInterface(string $name): ?InterfaceDefinition |
|
58
|
|
|
{ |
|
59
|
30 |
|
return $this->interfaces[$name] ?? null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return int |
|
64
|
|
|
*/ |
|
65
|
24 |
|
public function getNumberOfInterfaces(): int |
|
66
|
|
|
{ |
|
67
|
24 |
|
return \count($this->interfaces); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
739 |
|
public function __sleep(): array |
|
74
|
|
|
{ |
|
75
|
739 |
|
return \array_merge(parent::__sleep(), [ |
|
76
|
|
|
// self class |
|
77
|
739 |
|
'interfaces', |
|
78
|
|
|
|
|
79
|
|
|
// trait HasFields |
|
80
|
|
|
'fields', |
|
81
|
|
|
|
|
82
|
|
|
// trait HasDirectives |
|
83
|
|
|
'directives', |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
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.