Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\EventDispatcher;
13
 
14
use Symfony\Contracts\EventDispatcher\Event;
15
 
16
/**
17
 * Event encapsulation class.
18
 *
19
 * Encapsulates events thus decoupling the observer from the subject they encapsulate.
20
 *
21
 * @author Drak <drak@zikula.org>
22
 *
23
 * @implements \ArrayAccess<string, mixed>
24
 * @implements \IteratorAggregate<string, mixed>
25
 */
26
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
27
{
28
    protected $subject;
29
    protected $arguments;
30
 
31
    /**
32
     * Encapsulate an event with $subject and $args.
33
     *
34
     * @param mixed $subject   The subject of the event, usually an object or a callable
35
     * @param array $arguments Arguments to store in the event
36
     */
37
    public function __construct(mixed $subject = null, array $arguments = [])
38
    {
39
        $this->subject = $subject;
40
        $this->arguments = $arguments;
41
    }
42
 
43
    /**
44
     * Getter for subject property.
45
     */
46
    public function getSubject(): mixed
47
    {
48
        return $this->subject;
49
    }
50
 
51
    /**
52
     * Get argument by key.
53
     *
54
     * @throws \InvalidArgumentException if key is not found
55
     */
56
    public function getArgument(string $key): mixed
57
    {
58
        if ($this->hasArgument($key)) {
59
            return $this->arguments[$key];
60
        }
61
 
62
        throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
63
    }
64
 
65
    /**
66
     * Add argument to event.
67
     *
68
     * @return $this
69
     */
70
    public function setArgument(string $key, mixed $value): static
71
    {
72
        $this->arguments[$key] = $value;
73
 
74
        return $this;
75
    }
76
 
77
    /**
78
     * Getter for all arguments.
79
     */
80
    public function getArguments(): array
81
    {
82
        return $this->arguments;
83
    }
84
 
85
    /**
86
     * Set args property.
87
     *
88
     * @return $this
89
     */
90
    public function setArguments(array $args = []): static
91
    {
92
        $this->arguments = $args;
93
 
94
        return $this;
95
    }
96
 
97
    /**
98
     * Has argument.
99
     */
100
    public function hasArgument(string $key): bool
101
    {
102
        return \array_key_exists($key, $this->arguments);
103
    }
104
 
105
    /**
106
     * ArrayAccess for argument getter.
107
     *
108
     * @param string $key Array key
109
     *
110
     * @throws \InvalidArgumentException if key does not exist in $this->args
111
     */
112
    public function offsetGet(mixed $key): mixed
113
    {
114
        return $this->getArgument($key);
115
    }
116
 
117
    /**
118
     * ArrayAccess for argument setter.
119
     *
120
     * @param string $key Array key to set
121
     */
122
    public function offsetSet(mixed $key, mixed $value): void
123
    {
124
        $this->setArgument($key, $value);
125
    }
126
 
127
    /**
128
     * ArrayAccess for unset argument.
129
     *
130
     * @param string $key Array key
131
     */
132
    public function offsetUnset(mixed $key): void
133
    {
134
        if ($this->hasArgument($key)) {
135
            unset($this->arguments[$key]);
136
        }
137
    }
138
 
139
    /**
140
     * ArrayAccess has argument.
141
     *
142
     * @param string $key Array key
143
     */
144
    public function offsetExists(mixed $key): bool
145
    {
146
        return $this->hasArgument($key);
147
    }
148
 
149
    /**
150
     * IteratorAggregate for iterating over the object like an array.
151
     *
152
     * @return \ArrayIterator<string, mixed>
153
     */
154
    public function getIterator(): \ArrayIterator
155
    {
156
        return new \ArrayIterator($this->arguments);
157
    }
158
}