aboutsummaryrefslogtreecommitdiffstats
path: root/includes/Notification/Notification.php
blob: 97b594fe29e8849f09c83dc43acc44f2bea2eee8 (plain) (blame)
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
<?php
namespace MediaWiki\Notification;

use Serializable;
use Wikimedia\NonSerializable\NonSerializableTrait;

/**
 * @since 1.44
 * @unstable
 */
class Notification {

	use NonSerializableTrait;

	/**
	 * @TODO Idea: handle future types in format `namespace.type`, like `mediawiki.message`,
	 * `growth.welcome`. This way we can easily "override" some notifications, for example
	 * we can have `echo.mention`, and the `echo.mention` could supersede `mediawiki.mention`. Also
	 * it will be more difficult for notifications to conflict and we will be able to easily filter
	 * apply logic depends on the namespace (for example hide if extension not present).
	 *
	 * @var string Required, Read-only - The Notification type
	 */
	protected string $type;

	/**
	 * Internal, for now let's allow extensions to use generic notification and pass everything in
	 * custom array, but in future the notifications should have specific types.
	 * @var array An array of mixed data
	 */
	private array $custom;

	/**
	 * Base for notifications. Type is the only required property.
	 * All additional notification data can be passed via $custom array
	 *
	 * @param string $type Notification type
	 * @param array $custom Custom notification data
	 */
	public function __construct( string $type, array $custom = [] ) {
		$this->type = $type;
		$this->custom = $custom;
	}

	/**
	 * Get Notification type
	 * @return string
	 */
	public function getType(): string {
		return $this->type;
	}

	/**
	 * Sets a custom property to the notification.
	 *
	 * The following keys and their types are known and should be used consistently:
	 * - `msg` (MessageSpecifier) Localisable message body.
	 * - `agent` (UserIdentity) User who caused the event.
	 * - `title` (PageIdentity) Page on which the event was triggered.
	 * Any other keys may be used to pass additional data handled by specific extensions.
	 *
	 * @param string $key
	 * @param scalar|Serializable $value
	 * @return void
	 */
	protected function setProperty( string $key, $value ): void {
		$this->custom[$key] = $value;
	}

	/**
	 * Retrieve Notification properties
	 * @return array
	 */
	public function getProperties(): array {
		return $this->custom;
	}

}