1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* event (https://github.com/phpgears/event). |
5
|
|
|
* Event handling. |
6
|
|
|
* |
7
|
|
|
* @license MIT |
8
|
|
|
* @link https://github.com/phpgears/event |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Gears\Event; |
15
|
|
|
|
16
|
|
|
use Gears\DTO\Exception\InvalidScalarParameterException; |
17
|
|
|
use Gears\DTO\ScalarPayloadBehaviour; |
18
|
|
|
use Gears\Immutability\ImmutabilityBehaviour; |
19
|
|
|
|
20
|
|
|
use function DeepCopy\deep_copy; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Event metadata behaviour. |
24
|
|
|
*/ |
25
|
|
|
trait EventBehaviour |
26
|
|
|
{ |
27
|
|
|
use ImmutabilityBehaviour, ScalarPayloadBehaviour { |
28
|
|
|
ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Event metadata. |
33
|
|
|
* |
34
|
|
|
* @var array<string, mixed> |
35
|
|
|
*/ |
36
|
|
|
private $metadata = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \DateTimeImmutable |
40
|
|
|
*/ |
41
|
|
|
private $createdAt; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get event metadata. |
45
|
|
|
* |
46
|
|
|
* @return array<string, mixed> |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
final public function getMetadata(): array |
49
|
|
|
{ |
50
|
|
|
return $this->metadata; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get event with new metadata. |
55
|
|
|
* |
56
|
|
|
* @param array<string, mixed> $metadata |
57
|
|
|
* |
58
|
|
|
* @return mixed|self |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
final public function withMetadata(array $metadata) |
61
|
|
|
{ |
62
|
|
|
/* @var self $self */ |
63
|
|
|
$self = deep_copy($this); |
64
|
|
|
$self->setMetadata($metadata); |
65
|
|
|
|
66
|
|
|
return $self; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set event metadata. |
71
|
|
|
* |
72
|
|
|
* @param array<string, mixed> $parameters |
73
|
|
|
* |
74
|
|
|
* @throws InvalidScalarParameterException |
75
|
|
|
*/ |
76
|
|
|
private function setMetadata(array $parameters): void |
77
|
|
|
{ |
78
|
|
|
$this->metadata = []; |
79
|
|
|
|
80
|
|
|
foreach ($parameters as $parameter => $value) { |
81
|
|
|
try { |
82
|
|
|
$this->checkParameterType($value); |
83
|
|
|
} catch (InvalidScalarParameterException $exception) { |
84
|
|
|
throw new InvalidScalarParameterException( |
85
|
|
|
\sprintf( |
86
|
|
|
'Class %s can only accept scalar metadata parameters, %s given', |
87
|
|
|
self::class, |
88
|
|
|
\is_object($value) ? \get_class($value) : \gettype($value) |
89
|
|
|
), |
90
|
|
|
$exception->getCode(), |
91
|
|
|
$exception |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->metadata[$parameter] = $value; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get event creation time. |
101
|
|
|
* |
102
|
|
|
* @return \DateTimeImmutable |
103
|
|
|
*/ |
104
|
|
|
final public function getCreatedAt(): \DateTimeImmutable |
105
|
|
|
{ |
106
|
|
|
return $this->createdAt; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.