1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Habemus\Definition; |
5
|
|
|
|
6
|
|
|
use Habemus\Definition\Build\ClassDefinition; |
7
|
|
|
use Habemus\Definition\MethodCall\CallableMethod; |
8
|
|
|
use Habemus\Definition\Sharing\Shareable; |
9
|
|
|
use Habemus\Definition\Tag\Taggable; |
10
|
|
|
use Habemus\Exception\DefinitionException; |
11
|
|
|
|
12
|
|
|
final class DefinitionWrapper |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Definition |
16
|
|
|
*/ |
17
|
|
|
protected $definition; |
18
|
|
|
|
19
|
|
|
public function __construct(Definition $definition) |
20
|
|
|
{ |
21
|
|
|
$this->definition = $definition; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function constructor(string $param, $value): self |
25
|
|
|
{ |
26
|
|
|
if (!$this->acceptConstructorParameters()) { |
27
|
|
|
throw DefinitionException::unavailableConstructorParameters($this->definition); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->definition->constructor($param, $value); |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setShared(bool $share): self |
35
|
|
|
{ |
36
|
|
|
if (!$this->isShareable()) { |
37
|
|
|
throw DefinitionException::unshareable($this->definition); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->definition->setShared($share); |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function isShared(): ?bool |
45
|
|
|
{ |
46
|
|
|
if ($this->isShareable()) { |
47
|
|
|
return $this->definition->isShared(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function addMethodCall(string $method, array $parameters = []): self |
54
|
|
|
{ |
55
|
|
|
if (!$this->isCallableMethod()) { |
56
|
|
|
throw DefinitionException::unavailableMethodCall($this->definition); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->definition->addMethodCall($method, $parameters); |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function addTag(string ...$tag): self |
64
|
|
|
{ |
65
|
|
|
if (!$this->isTaggable()) { |
66
|
|
|
throw DefinitionException::untaggable($this->definition); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($tag as $_tag) { |
70
|
|
|
$this->definition->addTag($_tag); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isCallableMethod(): bool |
77
|
|
|
{ |
78
|
|
|
return $this->definition instanceof CallableMethod; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function isTaggable(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->definition instanceof Taggable; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isShareable(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->definition instanceof Shareable; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function acceptConstructorParameters(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->definition instanceof ClassDefinition; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|