1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Bluz Framework Component |
||
5 | * |
||
6 | * @copyright Bluz PHP Team |
||
7 | * @link https://github.com/bluzphp/framework |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Bluz\Messages; |
||
13 | |||
14 | use ArrayObject; |
||
15 | use Bluz\Common\Options; |
||
16 | use Bluz\Proxy\Session; |
||
17 | use Bluz\Proxy\Translator; |
||
18 | use stdClass; |
||
19 | |||
20 | /** |
||
21 | * Realization of Flash Messages |
||
22 | * |
||
23 | * @package Bluz\Messages |
||
24 | * @author Anton Shevchuk |
||
25 | * @link https://github.com/bluzphp/framework/wiki/Messages |
||
26 | */ |
||
27 | class Messages |
||
28 | { |
||
29 | use Options; |
||
30 | |||
31 | private const TYPE_ERROR = 'error'; |
||
32 | private const TYPE_SUCCESS = 'success'; |
||
33 | private const TYPE_NOTICE = 'notice'; |
||
34 | |||
35 | /** |
||
36 | * @var array list of messages types |
||
37 | */ |
||
38 | protected $types = [ |
||
39 | self::TYPE_ERROR, |
||
40 | self::TYPE_SUCCESS, |
||
41 | self::TYPE_NOTICE |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Add notice |
||
46 | * |
||
47 | * @param string $message |
||
48 | * @param string[] $text |
||
49 | * |
||
50 | * @return void |
||
51 | * @since 1.0.0 added $text |
||
52 | */ |
||
53 | 4 | public function addNotice(string $message, ...$text): void |
|
54 | { |
||
55 | 4 | $this->add(self::TYPE_NOTICE, $message, ...$text); |
|
56 | 4 | } |
|
57 | |||
58 | /** |
||
59 | * Add success |
||
60 | * |
||
61 | * @param string $message |
||
62 | * @param string[] $text |
||
63 | * |
||
64 | * @return void |
||
65 | * @since 1.0.0 added $text |
||
66 | */ |
||
67 | 4 | public function addSuccess(string $message, ...$text): void |
|
68 | { |
||
69 | 4 | $this->add(self::TYPE_SUCCESS, $message, ...$text); |
|
70 | 4 | } |
|
71 | |||
72 | /** |
||
73 | * Add error |
||
74 | * |
||
75 | * @param string $message |
||
76 | * @param string[] $text |
||
77 | * |
||
78 | * @return void |
||
79 | * @since 1.0.0 added $text |
||
80 | */ |
||
81 | 4 | public function addError(string $message, ...$text): void |
|
82 | { |
||
83 | 4 | $this->add(self::TYPE_ERROR, $message, ...$text); |
|
84 | 4 | } |
|
85 | |||
86 | /** |
||
87 | * Add message to container |
||
88 | * |
||
89 | * @param string $type One of error, notice or success |
||
90 | * @param string $message |
||
91 | * @param string[] $text |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | 4 | protected function add(string $type, string $message, ...$text): void |
|
96 | { |
||
97 | 4 | $this->getMessagesStore()[$type][] = Translator::translate($message, ...$text); |
|
98 | 4 | } |
|
99 | |||
100 | /** |
||
101 | * Pop a message by type |
||
102 | * |
||
103 | * @param string $type |
||
104 | * |
||
105 | * @return stdClass|null |
||
106 | */ |
||
107 | 4 | public function pop(string $type): ?stdClass |
|
108 | { |
||
109 | 4 | $text = array_shift($this->getMessagesStore()[$type]); |
|
110 | 4 | if ($text) { |
|
111 | 3 | $message = new stdClass(); |
|
112 | 3 | $message->text = $text; |
|
113 | 3 | $message->type = $type; |
|
114 | 3 | return $message; |
|
115 | } |
||
116 | 1 | return null; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Pop all messages |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 2 | public function popAll() |
|
125 | { |
||
126 | 2 | $messages = $this->getMessagesStore()->getArrayCopy(); |
|
127 | 2 | $this->resetMessagesStore(); |
|
128 | 2 | return $messages; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get size of messages container |
||
133 | * |
||
134 | * @return integer |
||
135 | */ |
||
136 | 2 | public function count(): int |
|
137 | { |
||
138 | 2 | $size = 0; |
|
139 | 2 | if (!$store = $this->getMessagesStore()) { |
|
140 | return $size; |
||
141 | } |
||
142 | 2 | foreach ($store as $messages) { |
|
143 | 2 | $size += count($messages); |
|
144 | } |
||
145 | 2 | return $size; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Reset messages |
||
150 | * |
||
151 | * @param ArrayObject $store |
||
152 | * @return void |
||
153 | */ |
||
154 | 6 | protected function setMessagesStore(ArrayObject $store): void |
|
155 | { |
||
156 | 6 | Session::set('messages:store', $store); |
|
157 | 6 | } |
|
158 | |||
159 | /** |
||
160 | * Returns current or new messages store if it not exists |
||
161 | * |
||
162 | * @return ArrayObject |
||
163 | */ |
||
164 | 6 | protected function getMessagesStore(): ArrayObject |
|
165 | { |
||
166 | 6 | if (!$store = Session::get('messages:store')) { |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
167 | 6 | $this->resetMessagesStore(); |
|
168 | } |
||
169 | 6 | return Session::get('messages:store'); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Reset messages |
||
174 | * |
||
175 | * @return void |
||
176 | */ |
||
177 | 6 | protected function resetMessagesStore(): void |
|
178 | { |
||
179 | 6 | $this->setMessagesStore($this->createEmptyMessagesStore()); |
|
180 | 6 | } |
|
181 | |||
182 | /** |
||
183 | * Creates a new empty store for messages |
||
184 | * |
||
185 | * @return ArrayObject |
||
186 | */ |
||
187 | 6 | protected function createEmptyMessagesStore(): ArrayObject |
|
188 | { |
||
189 | 6 | return new ArrayObject( |
|
190 | [ |
||
191 | 6 | self::TYPE_ERROR => [], |
|
192 | 6 | self::TYPE_SUCCESS => [], |
|
193 | 6 | self::TYPE_NOTICE => [] |
|
194 | ] |
||
195 | ); |
||
196 | } |
||
197 | } |
||
198 |