1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Utils |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Utils { |
11
|
|
|
|
12
|
|
|
use Template; |
13
|
|
|
|
14
|
|
|
abstract class Messages { |
15
|
|
|
|
16
|
|
|
protected static $view = 'Blocks/Utils/Message'; |
17
|
|
|
|
18
|
|
|
protected static $types = ['info', 'warning', 'error', 'success']; |
19
|
|
|
|
20
|
|
|
protected static $items = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Initialize the messages list |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
public static function init() { |
27
|
|
|
|
28
|
|
|
static::$items = []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set a message |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
public static function set(string $type, string $text, string $title = null) { |
36
|
|
|
|
37
|
|
|
if (!in_array($type, static::$types, true) || isset(static::$items[$type]) || ('' === $text)) return; |
38
|
|
|
|
39
|
|
|
static::$items[$type] = ['text' => $text, 'title' => (('' !== $title) ? $title : null)]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get a message |
44
|
|
|
* |
45
|
|
|
* @return array|false : the message data or false if the message was not set |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
public static function get(string $type) { |
49
|
|
|
|
50
|
|
|
return (static::$items[$type] ?? false); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get a messages block |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
public static function getBlock() : Template\Block { |
58
|
|
|
|
59
|
|
|
$messages = Template::createBlock(); |
60
|
|
|
|
61
|
|
|
foreach (static::$items as $type => $item) { |
62
|
|
|
|
63
|
|
|
$messages->addItem($message = View::get(static::$view)); |
64
|
|
|
|
65
|
|
|
$message->type = $type; $message->text = Template::createBlock($item['text']); |
|
|
|
|
66
|
|
|
|
67
|
|
|
if (isset($item['title'])) $message->getBlock('title')->set('text', $item['title'])->enable(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
# ------------------------ |
71
|
|
|
|
72
|
|
|
return $messages; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.