Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GenericEvent.php « EventDispatcher « public « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4607e6621ae48673e7b9f77dace262436f545165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
/**
 * @copyright Copyright (c) 2019 Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCP\EventDispatcher;

use ArrayAccess;
use ArrayIterator;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
use function array_key_exists;

/**
 * Class GenericEvent
 *
 * convenience reimplementation of \Symfony\Component\GenericEvent against
 * \OCP\EventDispatcher\Event
 *
 * @package OCP\EventDispatcher
 * @since 18.0.0
 */
class GenericEvent extends Event implements ArrayAccess, IteratorAggregate {
	protected $subject;
	protected $arguments;

	/**
	 * Encapsulate an event with $subject and $args.
	 *
	 * @since 18.0.0
	 */
	public function __construct($subject = null, array $arguments = []) {
		$this->subject = $subject;
		$this->arguments = $arguments;
	}

	/**
	 * Getter for subject property.
	 *
	 * @since 18.0.0
	 */
	public function getSubject() {
		return $this->subject;
	}

	/**
	 * Get argument by key.
	 *
	 * @throws InvalidArgumentException if key is not found
	 * @since 18.0.0
	 */
	public function getArgument(string $key) {
		if ($this->hasArgument($key)) {
			return $this->arguments[$key];
		}

		throw new InvalidArgumentException(sprintf('Argument "%s" not found.', $key));
	}

	/**
	 * Add argument to event.
	 *
	 * @since 18.0.0
	 */
	public function setArgument($key, $value): GenericEvent {
		$this->arguments[$key] = $value;
		return $this;
	}

	/**
	 * Getter for all arguments.
	 *
	 * @since 18.0.0
	 */
	public function getArguments(): array {
		return $this->arguments;
	}

	/**
	 * Set args property.
	 *
	 * @since 18.0.0
	 */
	public function setArguments(array $args = []): GenericEvent {
		$this->arguments = $args;
		return $this;
	}

	/**
	 * Has argument.
	 *
	 * @since 18.0.0
	 */
	public function hasArgument($key): bool {
		return array_key_exists($key, $this->arguments);
	}

	/**
	 * Retrieve an external iterator
	 *
	 * @link https://php.net/manual/en/iteratoraggregate.getiterator.php
	 * @since 18.0.0
	 */
	public function getIterator(): Traversable {
		return new ArrayIterator($this->arguments);
	}

	/**
	 * Whether a offset exists
	 *
	 * @link https://php.net/manual/en/arrayaccess.offsetexists.php
	 * @since 18.0.0
	 */
	public function offsetExists($offset): bool {
		return $this->hasArgument($offset);
	}

	/**
	 * Offset to retrieve
	 *
	 * @link https://php.net/manual/en/arrayaccess.offsetget.php
	 * @since 18.0.0
	 */
	public function offsetGet($offset) {
		return $this->arguments[$offset];
	}

	/**
	 * Offset to set
	 *
	 * @link https://php.net/manual/en/arrayaccess.offsetset.php
	 * @since 18.0.0
	 */
	public function offsetSet($offset, $value): void {
		$this->setArgument($offset, $value);
	}

	/**
	 * Offset to unset
	 *
	 * @link https://php.net/manual/en/arrayaccess.offsetunset.php
	 * @since 18.0.0
	 */
	public function offsetUnset($offset): void {
		if ($this->hasArgument($offset)) {
			unset($this->arguments[$offset]);
		}
	}
}