@@ -24,169 +24,200 @@ class FlashNotifier
2424 */
2525 public $ messages ;
2626
27+ /**
28+ * Collection of allowed methods
29+ * @var array
30+ */
31+ protected $ allowedMethods = ['primary ' , 'secondary ' , 'success ' , 'warning ' , 'info ' , 'error ' , 'danger ' , 'light ' , 'dark ' ];
32+
33+
2734 /**
2835 * Constructor - creates a new instance
2936 *
3037 * @param SessionFlashStore $session
3138 */
32- function __construct (SessionFlashStore $ session )
39+ public function __construct (SessionFlashStore $ session )
3340 {
3441 $ this ->session = $ session ;
3542 $ this ->messages = collect ();
3643 }
3744
3845 /**
39- * Return a primary message
46+ * Magic method implementing methods overloading
4047 *
41- * @param string|null $message
42- * @param bool $important
48+ * @param $name
49+ * @param $arguments
4350 * @return $this
4451 */
45- public function primary ( $ message = null , $ important = false )
52+ public function __call ( $ name = '' , $ arguments = [] )
4653 {
47- return $ this ->message ($ message , 'primary ' , $ important );
54+ if (in_array ($ name , $ this ->allowedMethods )) {
55+
56+ // Return the same object without changes if no argument passed
57+ if (empty ($ arguments )) {
58+ return $ this ;
59+ }
60+
61+ if (count ($ arguments ) > 1 ) {
62+ return $ this ->$ name ($ arguments [0 ], $ arguments [1 ]);
63+ } else {
64+ return $ this ->$ name ($ arguments [0 ]);
65+ }
66+ }
4867 }
4968
5069 /**
51- * Return a secondary message
70+ * Save a message
5271 *
5372 * @param string|null $message
73+ * @param string|null $level
5474 * @param bool $important
5575 * @return $this
5676 */
57- public function secondary ($ message = null , $ important = false )
77+ public function message ($ message = null , $ level = null , $ important = false )
5878 {
59- return $ this ->message ($ message , 'secondary ' , $ important );
79+ if ( ! $ message instanceof Message) {
80+ $ message = new Message (compact ('message ' , 'level ' ));
81+ }
82+
83+ $ this ->messages ->push ($ message );
84+
85+ if ($ important ) {
86+ $ this ->important ();
87+ }
88+
89+ return $ this ->flash ();
6090 }
6191
6292 /**
63- * Return a success message
93+ * Change flash to be important
6494 *
65- * @param string|null $message
66- * @param bool $important
6795 * @return $this
6896 */
69- public function success ( $ message = null , $ important = false )
97+ public function important ( )
7098 {
71- return $ this ->message ( $ message , ' success ' , $ important );
99+ return $ this ->updateLastMessage ([ ' important ' => true ] );
72100 }
73101
74102 /**
75- * Return an error message
103+ * Clear all messages
104+ *
105+ * @return $this
106+ */
107+ public function clear ()
108+ {
109+ $ this ->messages = collect ();
110+
111+ return $ this ;
112+ }
113+
114+ /**
115+ * Return a primary message
76116 *
77117 * @param string|null $message
78118 * @param bool $important
79119 * @return $this
80120 */
81- public function error ($ message = null , $ important = false )
121+ protected function primary ($ message = null , $ important = false )
82122 {
83- return $ this ->danger ($ message , $ important );
123+ return $ this ->message ($ message, ' primary ' , $ important );
84124 }
85125
86126 /**
87- * Return a danger message
88- * Alias to error()
127+ * Return a secondary message
89128 *
90129 * @param string|null $message
91130 * @param bool $important
92131 * @return $this
93132 */
94- public function danger ($ message = null , $ important = false )
133+ protected function secondary ($ message = null , $ important = false )
95134 {
96- return $ this ->message ($ message , 'danger ' , $ important );
135+ return $ this ->message ($ message , 'secondary ' , $ important );
97136 }
98137
99138 /**
100- * Return a warning message
139+ * Return a success message
101140 *
102141 * @param string|null $message
103142 * @param bool $important
104143 * @return $this
105144 */
106- public function warning ($ message = null , $ important = false )
145+ protected function success ($ message = null , $ important = false )
107146 {
108- return $ this ->message ($ message , 'warning ' , $ important );
147+ return $ this ->message ($ message , 'success ' , $ important );
109148 }
110149
111150 /**
112- * Return an information message
151+ * Return a warning message
113152 *
114153 * @param string|null $message
115154 * @param bool $important
116155 * @return $this
117156 */
118- public function info ($ message = null , $ important = false )
157+ protected function warning ($ message = null , $ important = false )
119158 {
120- return $ this ->message ($ message , 'info ' , $ important );
159+ return $ this ->message ($ message , 'warning ' , $ important );
121160 }
122161
123162 /**
124- * Return a simple light message
163+ * Return an information message
125164 *
126165 * @param string|null $message
127166 * @param bool $important
128167 * @return $this
129168 */
130- public function light ($ message = null , $ important = false )
169+ protected function info ($ message = null , $ important = false )
131170 {
132- return $ this ->message ($ message , 'light ' , $ important );
171+ return $ this ->message ($ message , 'info ' , $ important );
133172 }
134173
135174 /**
136- * Return a simple dark message
175+ * Return an error message
137176 *
138177 * @param string|null $message
139178 * @param bool $important
140179 * @return $this
141180 */
142- public function dark ($ message = null , $ important = false )
181+ protected function error ($ message = null , $ important = false )
143182 {
144- return $ this ->message ($ message, ' light ' , $ important );
183+ return $ this ->danger ($ message , $ important );
145184 }
146185
147186 /**
148- * Save a message
187+ * Return a danger message
188+ * Alias to error()
149189 *
150190 * @param string|null $message
151- * @param string|null $level
152191 * @param bool $important
153192 * @return $this
154193 */
155- public function message ($ message = null , $ level = null , $ important = false )
194+ protected function danger ($ message = null , $ important = false )
156195 {
157- if ( ! $ message instanceof Message) {
158- $ message = new Message (compact ('message ' , 'level ' ));
159- }
160-
161- $ this ->messages ->push ($ message );
162-
163- if ($ important ) {
164- $ this ->important ();
165- }
166-
167- return $ this ->flash ();
196+ return $ this ->message ($ message , 'danger ' , $ important );
168197 }
169198
170199 /**
171- * Change flash to be important
200+ * Return a simple light message
172201 *
202+ * @param string|null $message
203+ * @param bool $important
173204 * @return $this
174205 */
175- public function important ( )
206+ protected function light ( $ message = null , $ important = false )
176207 {
177- return $ this ->updateLastMessage ([ ' important ' => true ] );
208+ return $ this ->message ( $ message , ' light ' , $ important );
178209 }
179210
180211 /**
181- * Clear all messages
212+ * Return a simple dark message
182213 *
214+ * @param string|null $message
215+ * @param bool $important
183216 * @return $this
184217 */
185- public function clear ( )
218+ protected function dark ( $ message = null , $ important = false )
186219 {
187- $ this ->messages = collect ();
188-
189- return $ this ;
220+ return $ this ->message ($ message , 'light ' , $ important );
190221 }
191222
192223 /**
0 commit comments