├── .github
└── workflows
│ └── php.yml
├── .gitignore
├── .php_cs
├── .styleci.yml
├── CHANGELOG.md
├── LICENSE
├── README.md
├── composer.json
├── demos
├── 1XySJiz.png
├── 4bvuJx9.png
├── AnRGDY2.png
├── DxKh3Yx.png
├── GsGOtOq.png
├── dj3y95K.png
├── pIeTEYz.png
├── pQz3ijJ.png
└── x44c12a.png
├── phpunit.xml
├── src
├── SweetAlert
│ ├── ConvertMessagesIntoSweetAlert.php
│ ├── LaravelSessionStore.php
│ ├── SessionStore.php
│ ├── SweetAlert.php
│ ├── SweetAlertNotifier.php
│ ├── SweetAlertServiceProvider.php
│ └── functions.php
├── config
│ └── sweet-alert.php
└── views
│ ├── alert.blade.php
│ └── alert.twig
├── tests
├── .gitkeep
└── SweetAlertNotifierTest.php
└── travis.yml
/.github/workflows/php.yml:
--------------------------------------------------------------------------------
1 | name: PHP Composer
2 |
3 | on: [push]
4 |
5 | jobs:
6 | build:
7 |
8 | runs-on: ubuntu-latest
9 |
10 | steps:
11 | - uses: actions/checkout@v1
12 |
13 | - name: Validate composer.json and composer.lock
14 | run: composer validate
15 |
16 | - name: Install dependencies
17 | run: composer install --prefer-dist --no-progress --no-suggest
18 |
19 | # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
20 | # Docs: https://getcomposer.org/doc/articles/scripts.md
21 |
22 | - name: Run test suite
23 | run: phpunit
24 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | composer.phar
3 | composer.lock
4 | .DS_Store
5 |
--------------------------------------------------------------------------------
/.php_cs:
--------------------------------------------------------------------------------
1 | exclude('vendor')
7 | ->exclude('bootstrap')
8 | ->exclude('config')
9 | ->exclude('storage')
10 | ->exclude('public')
11 | ->notPath('server.php')
12 | ->in(__DIR__);
13 |
14 | return PhpCsFixer\Config::create()
15 | ->setFinder($finder)
16 | ->setRules([
17 | '@PSR2' => true,
18 | 'array_syntax' => ['syntax' => 'short'],
19 | 'combine_consecutive_unsets' => true,
20 | 'method_separation' => true,
21 | 'no_multiline_whitespace_before_semicolons' => true,
22 | 'single_quote' => true,
23 | 'ordered_imports' => [
24 | 'sortAlgorithm' => 'length',
25 | ],
26 |
27 | 'binary_operator_spaces' => [
28 | 'default' => null,
29 | 'operators' => [
30 | '|' => 'no_space',
31 | '=>' => 'single_space',
32 | '=' => 'single_space',
33 | ],
34 | ],
35 | // 'blank_line_after_opening_tag' => true,
36 | // 'blank_line_before_return' => true,
37 | 'braces' => [
38 | 'allow_single_line_closure' => true,
39 | ],
40 | // 'cast_spaces' => true,
41 | // 'class_definition' => array('singleLine' => true),
42 | 'concat_space' => ['spacing' => 'one'],
43 | 'declare_equal_normalize' => true,
44 | 'function_typehint_space' => true,
45 | 'hash_to_slash_comment' => true,
46 | 'include' => true,
47 | 'lowercase_cast' => true,
48 | // 'native_function_casing' => true,
49 | // 'new_with_braces' => true,
50 | 'no_blank_lines_after_class_opening' => true,
51 | // 'no_blank_lines_after_phpdoc' => true,
52 | // 'no_empty_comment' => true,
53 | // 'no_empty_phpdoc' => true,
54 | // 'no_empty_statement' => true,
55 | 'no_extra_consecutive_blank_lines' => [
56 | 'curly_brace_block',
57 | 'extra',
58 | 'parenthesis_brace_block',
59 | 'square_brace_block',
60 | 'throw',
61 | 'use',
62 | ],
63 | // 'no_leading_import_slash' => true,
64 | // 'no_leading_namespace_whitespace' => true,
65 | // 'no_mixed_echo_print' => array('use' => 'echo'),
66 | 'no_multiline_whitespace_around_double_arrow' => true,
67 | // 'no_short_bool_cast' => true,
68 | // 'no_singleline_whitespace_before_semicolons' => true,
69 | 'no_spaces_around_offset' => true,
70 | 'no_trailing_comma_in_list_call' => true,
71 | 'no_trailing_comma_in_singleline_array' => true,
72 | // 'no_unneeded_control_parentheses' => true,
73 | 'no_unused_imports' => true,
74 | 'no_whitespace_before_comma_in_array' => true,
75 | 'no_whitespace_in_blank_line' => true,
76 | // 'normalize_index_brace' => true,
77 | 'object_operator_without_whitespace' => true,
78 | // 'php_unit_fqcn_annotation' => true,
79 | // 'phpdoc_align' => true,
80 | // 'phpdoc_annotation_without_dot' => true,
81 | // 'phpdoc_indent' => true,
82 | // 'phpdoc_inline_tag' => true,
83 | // 'phpdoc_no_access' => true,
84 | // 'phpdoc_no_alias_tag' => true,
85 | // 'phpdoc_no_empty_return' => true,
86 | // 'phpdoc_no_package' => true,
87 | // 'phpdoc_no_useless_inheritdoc' => true,
88 | // 'phpdoc_return_self_reference' => true,
89 | // 'phpdoc_scalar' => true,
90 | // 'phpdoc_separation' => true,
91 | // 'phpdoc_single_line_var_spacing' => true,
92 | // 'phpdoc_summary' => true,
93 | // 'phpdoc_to_comment' => true,
94 | // 'phpdoc_trim' => true,
95 | // 'phpdoc_types' => true,
96 | // 'phpdoc_var_without_name' => true,
97 | // 'pre_increment' => true,
98 | // 'return_type_declaration' => true,
99 | // 'self_accessor' => true,
100 | // 'short_scalar_cast' => true,
101 | 'single_blank_line_before_namespace' => true,
102 | // 'single_class_element_per_statement' => true,
103 | // 'space_after_semicolon' => true,
104 | 'standardize_not_equals' => true,
105 | 'ternary_operator_spaces' => true,
106 | 'trailing_comma_in_multiline_array' => true,
107 | // 'trim_array_spaces' => true,
108 | 'unary_operator_spaces' => true,
109 | 'visibility_required' => [ 'elements' => ['property']],
110 | 'whitespace_after_comma_in_array' => true,
111 | 'php_unit_method_casing' => ['case' => 'snake_case'],
112 | // 'method_chaining_indentation' => true,
113 | 'array_indentation' => true,
114 | 'not_operator_with_successor_space' => true,
115 | 'align_multiline_comment' => true,
116 | ])
117 | ->setLineEnding("\n");
118 |
--------------------------------------------------------------------------------
/.styleci.yml:
--------------------------------------------------------------------------------
1 | preset: laravel
2 |
3 | enabled:
4 | - length_ordered_imports
5 | - long_list_syntax
6 | - unalign_double_arrow
7 |
8 | disabled:
9 | - short_list_syntax
10 | - concat_without_spaces
11 | - alpha_ordered_imports
12 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file.
4 |
5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7 |
8 | ## [Unreleased](https://github.com/uxweb/sweet-alert/compare/v2.0.3...HEAD)
9 |
10 | ## [v2.0.3] - 2019-12-24
11 |
12 | ### Added
13 |
14 | - Minor code improvements.
15 |
16 | ## [2.0.2] - 2019-12-23
17 |
18 | ### Added
19 |
20 | - Added this changelog.
21 | - Facade dockblocks for ide intellisense.
22 |
23 | ## [2.0.1] - 2018-11-21
24 |
25 | ### Fixed
26 |
27 | - Bugfix related to persistent alert confirm button.
28 |
29 | ## [2.0.0] - 2018-07-24
30 |
31 | ### Added
32 |
33 | - Added method addButton to create new custom buttons.
34 | - Added method setConfig to customize the configuration "by hand".
35 |
36 | ### Changed
37 |
38 | - Upgraded to the new sweetalert js api according to THIS migration guide from 1.x to 2.0.
39 | - Only supports PHP >= 7.0.
40 |
41 | ## [1.4.3] - 2018-07-24
42 |
43 | ### Fixed
44 |
45 | - Minor documentation fixes
46 |
47 | ## [1.4.2] - 2017-09-11
48 |
49 | ### Added
50 |
51 | - Added package discovery for Laravel 5.5
52 |
53 | ### Fixed
54 |
55 | - Middleware class name type fixed.
56 |
57 | ## [1.4.1] - 2017-03-27
58 |
59 | ### Fixed
60 |
61 | - Fix readme typos
62 |
63 | ## [1.4.0] - 2016-11-24
64 |
65 | ### Added
66 |
67 | - Cancel button config added.
68 |
69 | ### Changed
70 |
71 | - Documentation improvements.
72 |
73 | ## [1.3.1] - 2016-11-24
74 |
75 | ## [1.3.0] - 2016-08-15
76 |
77 | ### Added
78 |
79 | - Test suite included.
80 | - Configuration file included to customize default autoclose timer.
81 |
82 | ## [1.2.0] - 2016-08-12
83 |
84 | ### Added
85 |
86 | - Alert middleware added.
87 |
88 | ## [1.1.6] - 2016-07-11
89 |
90 | ## [1.1.5] - 2016-05-29
91 |
92 | ## [1.1.4] - 2016-03-08
93 |
94 | ### Added
95 |
96 | - Added Twig template
97 |
98 | ## [1.1.3] - 2015-12-22
99 |
100 | ### Added
101 |
102 | - Added Html option. This will let sweetalert display html content.
103 |
104 | ## [1.1.2] - 2015-11-24
105 |
106 | ### Fixed
107 |
108 | - Some StyleCI fixes.
109 |
110 | ## [1.1.1] - 2015-08-04
111 |
112 | ### Added
113 |
114 | - Added warning alert message.
115 |
116 | ## [1.1.0] - 2015-08-02
117 |
118 | ### Added
119 |
120 | - Added a method to display an info alert.
121 |
122 | ### Changed
123 |
124 | - Changed behavior of message and title.
125 | - Now if title is not specified, the text message will be displayed as the title.
126 | - Default message now shows a simple alert instead of info.
127 |
128 | ## [1.0.7] - 2015-07-16
129 |
130 | ## [1.0.6] - 2015-07-12
131 |
132 | ### Fixed
133 |
134 | - Fix included view template
135 |
136 | ## [1.0.5] - 2015-07-12
137 |
138 | ### Fixed
139 |
140 | -Fixed composer.json
141 |
142 | ## [1.0.4] - 2015-07-12
143 |
144 | ### Fixed
145 |
146 | - Fix composer.json
147 |
148 | ## [1.0.3] - 2015-07-12
149 |
150 | ### Fixed
151 |
152 | - Fixed psr-4 autoload
153 |
154 | ## [1.0.2] - 2015-07-12
155 |
156 | ### Changed
157 |
158 | - Improved documentation
159 |
160 | ## [1.0.0] - 2015-07-12
161 |
162 | [unreleased]: https://github.com/uxweb/sweet-alert/compare/v2.0.3...HEAD
163 | [v2.0.3]: https://github.com/uxweb/sweet-alert/compare/2.0.2...v2.0.3
164 | [2.0.2]: https://github.com/uxweb/sweet-alert/compare/2.0.1...2.0.2
165 | [2.0.1]: https://github.com/uxweb/sweet-alert/compare/2.0.1...2.0.0
166 | [2.0.0]: https://github.com/uxweb/sweet-alert/compare/1.4.3...2.0.0
167 | [1.4.3]: https://github.com/uxweb/sweet-alert/compare/1.4.2...1.4.3
168 | [1.4.2]: https://github.com/uxweb/sweet-alert/compare/1.4.1...1.4.2
169 | [1.4.1]: https://github.com/uxweb/sweet-alert/compare/1.4.0...1.4.1
170 | [1.4.0]: https://github.com/uxweb/sweet-alert/compare/1.3.1...1.4.0
171 | [1.3.1]: https://github.com/uxweb/sweet-alert/compare/1.3.0...1.3.1
172 | [1.3.0]: https://github.com/uxweb/sweet-alert/compare/1.2.0...1.3.0
173 | [1.2.0]: https://github.com/uxweb/sweet-alert/compare/1.1.6...1.2.0
174 | [1.1.6]: https://github.com/uxweb/sweet-alert/compare/1.1.5...1.1.6
175 | [1.1.5]: https://github.com/uxweb/sweet-alert/compare/1.1.4...1.1.5
176 | [1.1.4]: https://github.com/uxweb/sweet-alert/compare/1.1.3...1.1.4
177 | [1.1.3]: https://github.com/uxweb/sweet-alert/compare/1.1.2...1.1.3
178 | [1.1.2]: https://github.com/uxweb/sweet-alert/compare/1.1.1...1.1.2
179 | [1.1.1]: https://github.com/uxweb/sweet-alert/compare/1.1.0...1.1.1
180 | [1.1.0]: https://github.com/uxweb/sweet-alert/compare/1.0.7...1.1.0
181 | [1.0.7]: https://github.com/uxweb/sweet-alert/compare/1.0.6...1.0.7
182 | [1.0.6]: https://github.com/uxweb/sweet-alert/compare/1.0.5...1.0.6
183 | [1.0.5]: https://github.com/uxweb/sweet-alert/compare/1.0.4...1.0.5
184 | [1.0.4]: https://github.com/uxweb/sweet-alert/compare/1.0.3...1.0.4
185 | [1.0.3]: https://github.com/uxweb/sweet-alert/compare/1.0.2...1.0.3
186 | [1.0.2]: https://github.com/uxweb/sweet-alert/compare/1.0.1...1.0.2
187 | [1.0.1]: https://github.com/uxweb/sweet-alert/compare/1.0.0...1.0.1
188 | [1.0.0]: https://github.com/uxweb/sweet-alert/releases/tag/1.0.0
189 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Uziel Bueno
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Easy Sweet Alert Messages for Laravel
2 |
3 | 
4 |
5 | [](https://github.com/uxweb/sweet-alert/releases)
6 | [](https://styleci.io/repos/38935942)
7 | [](https://packagist.org/packages/uxweb/sweet-alert)
8 |
9 | ## Installation
10 |
11 | Require the package using Composer.
12 |
13 | ```bash
14 | composer require uxweb/sweet-alert
15 | ```
16 |
17 | If using laravel < 5.5 include the service provider and alias within `config/app.php`.
18 |
19 | ```php
20 | 'providers' => [
21 | UxWeb\SweetAlert\SweetAlertServiceProvider::class,
22 | ];
23 |
24 | 'aliases' => [
25 | 'Alert' => UxWeb\SweetAlert\SweetAlert::class,
26 | ];
27 | ```
28 |
29 | ## Installing Frontend Dependency
30 |
31 | This package works only by using the [BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT"](https://sweetalert.js.org/).
32 |
33 | ### Using a CDN
34 |
35 | ```html
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | @include('sweet::alert')
44 |
45 |
46 | ```
47 |
48 | ### Using Laravel Mix
49 |
50 | Install using Yarn
51 |
52 | ```bash
53 | yarn add sweetalert --dev
54 | ```
55 |
56 | Install using NPM
57 |
58 | ```bash
59 | npm install sweetalert --save-dev
60 | ```
61 |
62 | Require sweetalert within your `resources/js/bootstrap.js` file.
63 |
64 | ```javascript
65 | // ...
66 |
67 | require("sweetalert");
68 |
69 | // ...
70 | ```
71 |
72 | Then make sure to include your scripts in your blade layout. Remove the `defer` attribute if your script tag contains it, `defer` will delay the execution of the script which will cause an error as the `sweet::alert` blade template is rendered first by the browser as html.
73 |
74 | ```html
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | @include('sweet::alert')
83 |
84 |
85 | ```
86 |
87 | Finally compile your assets with Mix
88 |
89 | ```bash
90 | npm run dev
91 | ```
92 |
93 | ## Usage
94 |
95 | ### Using the Facade
96 |
97 | First import the SweetAlert facade in your controller.
98 |
99 | ```php
100 | use SweetAlert;
101 | ```
102 |
103 | Within your controllers, before you perform a redirect...
104 |
105 | ```php
106 | public function store()
107 | {
108 | SweetAlert::message('Robots are working!');
109 |
110 | return Redirect::home();
111 | }
112 | ```
113 |
114 | Here are some examples on how you can use the facade:
115 |
116 | ```php
117 | SweetAlert::message('Message', 'Optional Title');
118 |
119 | SweetAlert::basic('Basic Message', 'Mandatory Title');
120 |
121 | SweetAlert::info('Info Message', 'Optional Title');
122 |
123 | SweetAlert::success('Success Message', 'Optional Title');
124 |
125 | SweetAlert::error('Error Message', 'Optional Title');
126 |
127 | SweetAlert::warning('Warning Message', 'Optional Title');
128 | ```
129 |
130 | ### Using the helper function
131 |
132 | `alert($message = null, $title = '')`
133 |
134 | In addition to the previous listed methods you can also just use the helper
135 | function without specifying any message type. Doing so is similar to:
136 |
137 | `alert()->message('Message', 'Optional Title')`
138 |
139 | Like with the Facade we can use the helper with the same methods:
140 |
141 | ```php
142 | alert()->message('Message', 'Optional Title');
143 |
144 | alert()->basic('Basic Message', 'Mandatory Title');
145 |
146 | alert()->info('Info Message', 'Optional Title');
147 |
148 | alert()->success('Success Message', 'Optional Title');
149 |
150 | alert()->error('Error Message', 'Optional Title');
151 |
152 | alert()->warning('Warning Message', 'Optional Title');
153 |
154 | alert()->basic('Basic Message', 'Mandatory Title')->autoclose(3500);
155 |
156 | alert()->error('Error Message', 'Optional Title')->persistent('Close');
157 | ```
158 |
159 | Within your controllers, before you perform a redirect...
160 |
161 | ```php
162 | /**
163 | * Destroy the user's session (logout).
164 | *
165 | * @return Response
166 | */
167 | public function destroy()
168 | {
169 | Auth::logout();
170 |
171 | alert()->success('You have been logged out.', 'Good bye!');
172 |
173 | return home();
174 | }
175 | ```
176 |
177 | For a general information alert, just do: `alert('Some message');` (same as `alert()->message('Some message');`).
178 |
179 | ### Using the Middleware
180 |
181 | #### Middleware Groups
182 |
183 | First register the middleware in web middleware groups by simply adding the middleware class `UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class` into the \$middlewareGroups of your app/Http/Kernel.php class:
184 |
185 | ```php
186 | protected $middlewareGroups = [
187 | 'web' => [
188 | \App\Http\Middleware\EncryptCookies::class,
189 | ...
190 | \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class,
191 | ],
192 |
193 | 'api' => [
194 | 'throttle:60,1',
195 | ],
196 | ];
197 | ```
198 |
199 | > Make sure you register the middleware within the 'web' group only.
200 |
201 | #### Route Middleware
202 |
203 | Or if you would like to assign the middleware to specific routes only, you should add the middleware to `$routeMiddleware` in `app/Http/Kernel.php` file:
204 |
205 | ```php
206 | protected $routeMiddleware = [
207 | 'auth' => \App\Http\Middleware\Authenticate::class,
208 | ....
209 | 'sweetalert' => \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class,
210 | ];
211 | ```
212 |
213 | Next step: within your controllers, set your return message (using `with()`) and send the proper message and proper type.
214 |
215 | ```PHP
216 | return redirect('dashboard')->with('success', 'Profile updated!');
217 | ```
218 |
219 | or
220 |
221 | ```PHP
222 | return redirect()->back()->with('error', 'Profile updated!');
223 | ```
224 |
225 | > **NOTE**: When using the middleware it will make an alert to display if it detects any of the following keys flashed into the session: `error`, `success`, `warning`, `info`, `message`, `basic`.
226 |
227 | ### Final Considerations
228 |
229 | By default, all alerts will dismiss after a sensible default number of seconds.
230 |
231 | But not to worry, if you need to specify a different time you can:
232 |
233 | ```php
234 | // -> Remember!, the number is set in milliseconds
235 | alert('Hello World!')->autoclose(3000);
236 | ```
237 |
238 | Also, if you need the alert to be persistent on the page until the user dismiss it by pressing the alert confirmation button:
239 |
240 | ```php
241 | // -> The text will appear in the button
242 | alert('Hello World!')->persistent("Close this");
243 | ```
244 |
245 | You can render html in your message with the html() method like this:
246 |
247 | ```php
248 | // -> html will be evaluated
249 | alert('Click me')->html()->persistent("No, thanks");
250 | ```
251 |
252 | ## Customize
253 |
254 | ### Config
255 |
256 | If you need to customize the default configuration options for this package just export the configuration file:
257 |
258 | ```bash
259 | php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=config
260 | ```
261 |
262 | A `sweet-alert.php` configuration file will be published to your `config` directory. By now, the only configuration that can be changed is the timer for all autoclose alerts.
263 |
264 | ### View
265 |
266 | If you need to customize the included alert message view, run:
267 |
268 | ```bash
269 | php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider" --tag=views
270 | ```
271 |
272 | The package view is located in the `resources/views/vendor/sweet/` directory.
273 |
274 | You can customize this view to fit your needs.
275 |
276 | #### Configuration Options
277 |
278 | You have access to the following configuration options to build a custom view:
279 |
280 | ```php
281 | Session::get('sweet_alert.text')
282 | Session::get('sweet_alert.title')
283 | Session::get('sweet_alert.icon')
284 | Session::get('sweet_alert.closeOnClickOutside')
285 | Session::get('sweet_alert.buttons')
286 | Session::get('sweet_alert.timer')
287 | ```
288 |
289 | Please check the CONFIGURATION section in the [website](https://sweetalert.js.org/docs/#configuration) for all other options available.
290 |
291 | ### Default View
292 |
293 | The `sweet_alert.alert` session key contains a JSON configuration object to pass it directly to Sweet Alert.
294 |
295 | ```html
296 | @if (Session::has('sweet_alert.alert'))
297 |
300 | @endif
301 | ```
302 |
303 | Note that `{!! !!}` are used to output the json configuration object unescaped, it will not work with `{{ }}` escaped output tags.
304 |
305 | ### Custom View
306 |
307 | This is an example of how you can customize your view to fit your needs:
308 |
309 | ```html
310 | @if (Session::has('sweet_alert.alert'))
311 |
322 | @endif
323 | ```
324 |
325 | Note that you must use `""` (double quotes) to wrap the values except for the timer option.
326 |
327 | ## Tests
328 |
329 | To run the included test suite:
330 |
331 | ```bash
332 | vendor/bin/phpunit
333 | ```
334 |
335 | ## Demo
336 |
337 | ```php
338 | SweetAlert::message('Welcome back!');
339 |
340 | return Redirect::home();
341 | ```
342 |
343 | 
344 |
345 | ```php
346 | SweetAlert::message('Your profile is up to date', 'Wonderful!');
347 |
348 | return Redirect::home();
349 | ```
350 |
351 | 
352 |
353 | ```php
354 | SweetAlert::message('Thanks for comment!')->persistent('Close');
355 |
356 | return Redirect::home();
357 | ```
358 |
359 | 
360 |
361 | ```php
362 | SweetAlert::info('Email was sent!');
363 |
364 | return Redirect::home();
365 | ```
366 |
367 | 
368 |
369 | ```php
370 | SweetAlert::error('Something went wrong', 'Oops!');
371 |
372 | return Redirect::home();
373 | ```
374 |
375 | 
376 |
377 | ```php
378 | SweetAlert::success('Good job!');
379 |
380 | return Redirect::home();
381 | ```
382 |
383 | 
384 |
385 | ```php
386 | SweetAlert::info('Random lorempixel.com :
')->html();
387 |
388 | return Redirect::home();
389 | ```
390 |
391 | 
392 |
393 | ```php
394 | SweetAlert::success('Good job!')->persistent("Close");
395 |
396 | return Redirect::home();
397 | ```
398 |
399 | 
400 |
401 | ## License
402 |
403 | Sweet Alert for Laravel is open-sourced software licensed under the [MIT license](LICENSE).
404 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "uxweb/sweet-alert",
3 | "description": "A simple PHP package to show Sweet Alerts with the Laravel Framework",
4 | "keywords": [
5 | "laravel",
6 | "alert",
7 | "sweet",
8 | "notifier"
9 | ],
10 | "license": "MIT",
11 | "authors": [
12 | {
13 | "name": "Uziel Bueno",
14 | "email": "ux.webs@gmail.com"
15 | }
16 | ],
17 | "require": {
18 | "php": ">=7.0",
19 | "illuminate/support": "~5.0|^6.0|^7.0|^8.0",
20 | "illuminate/session": "~5.0|^6.0|^7.0|^8.0"
21 | },
22 | "require-dev": {
23 | "phpunit/phpunit": "^7.0",
24 | "mockery/mockery": "^1.0",
25 | "friendsofphp/php-cs-fixer": "^2.16"
26 | },
27 | "autoload": {
28 | "psr-4": {
29 | "UxWeb\\SweetAlert\\": "src/SweetAlert/"
30 | },
31 | "files": [
32 | "src/SweetAlert/functions.php"
33 | ]
34 | },
35 | "extra": {
36 | "laravel": {
37 | "providers": [
38 | "UxWeb\\SweetAlert\\SweetAlertServiceProvider"
39 | ],
40 | "aliases": {
41 | "Alert": "UxWeb\\SweetAlert\\SweetAlert"
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/demos/1XySJiz.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/1XySJiz.png
--------------------------------------------------------------------------------
/demos/4bvuJx9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/4bvuJx9.png
--------------------------------------------------------------------------------
/demos/AnRGDY2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/AnRGDY2.png
--------------------------------------------------------------------------------
/demos/DxKh3Yx.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/DxKh3Yx.png
--------------------------------------------------------------------------------
/demos/GsGOtOq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/GsGOtOq.png
--------------------------------------------------------------------------------
/demos/dj3y95K.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/dj3y95K.png
--------------------------------------------------------------------------------
/demos/pIeTEYz.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/pIeTEYz.png
--------------------------------------------------------------------------------
/demos/pQz3ijJ.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/pQz3ijJ.png
--------------------------------------------------------------------------------
/demos/x44c12a.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/demos/x44c12a.png
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | ./tests
16 |
17 |
18 |
19 |
20 | ./src
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/SweetAlert/ConvertMessagesIntoSweetAlert.php:
--------------------------------------------------------------------------------
1 | session()->has('success')) {
20 | alert()->success($request->session()->get('success'))->persistent();
21 | }
22 |
23 | if ($request->session()->has('warning')) {
24 | alert()->warning($request->session()->get('warning'))->persistent();
25 | }
26 |
27 | if ($request->session()->has('info')) {
28 | alert()->info($request->session()->get('info'))->persistent();
29 | }
30 |
31 | if ($request->session()->has('message')) {
32 | alert()->message($request->session()->get('message'))->persistent();
33 | }
34 |
35 | if ($request->session()->has('basic')) {
36 | alert()->basic($request->session()->get('basic'));
37 | }
38 |
39 | if ($request->session()->has('errors')) {
40 | $message = $request->session()->get('errors');
41 |
42 | if (! is_string($message)) {
43 | $message = $this->prepareErrors($message->getMessages());
44 | }
45 |
46 | alert()->error($message)->html()->persistent();
47 | }
48 |
49 | return $next($request);
50 | }
51 |
52 | /**
53 | * Retrieve the errors from ViewErrorBag.
54 | *
55 | * @param $errors
56 | *
57 | * @return string
58 | */
59 | private function prepareErrors($errors)
60 | {
61 | $errors = collect($errors);
62 |
63 | return $errors->flatten()->implode('
');
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/SweetAlert/LaravelSessionStore.php:
--------------------------------------------------------------------------------
1 | session = $session;
17 | }
18 |
19 | /**
20 | * Flash some data into the session.
21 | *
22 | * @param string $key
23 | * @param $value
24 | */
25 | public function flash(string $key, $value = true)
26 | {
27 | $this->session->flash($key, $value);
28 | }
29 |
30 | /**
31 | * Remove an item from the session.
32 | *
33 | * @param string|array $keys
34 | */
35 | public function remove($keys)
36 | {
37 | $this->session->forget($keys);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/SweetAlert/SessionStore.php:
--------------------------------------------------------------------------------
1 | '',
26 | 'visible' => false,
27 | 'value' => null,
28 | 'className' => '',
29 | 'closeModal' => true,
30 | ];
31 |
32 | /**
33 | * Create a new SweetAlertNotifier instance.
34 | *
35 | * @param \UxWeb\SweetAlert\SessionStore $session
36 | */
37 | public function __construct(SessionStore $session)
38 | {
39 | $this->session = $session;
40 |
41 | $this->setDefaultConfig();
42 | }
43 |
44 | /**
45 | * Sets all default config options for an alert.
46 | *
47 | * @return void
48 | */
49 | protected function setDefaultConfig()
50 | {
51 | $this->setConfig([
52 | 'timer' => config('sweet-alert.autoclose'),
53 | 'text' => '',
54 | 'buttons' => [
55 | 'cancel' => false,
56 | 'confirm' => false,
57 | ],
58 | ]);
59 | }
60 |
61 | /**
62 | * Display an alert message with a text and an optional title.
63 | *
64 | * By default the alert is not typed.
65 | *
66 | * @param string $text
67 | * @param string $title
68 | * @param string $icon
69 | *
70 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
71 | */
72 | public function message($text = '', $title = null, $icon = null)
73 | {
74 | $this->config['text'] = $text;
75 |
76 | if (! is_null($title)) {
77 | $this->config['title'] = $title;
78 | }
79 |
80 | if (! is_null($icon)) {
81 | $this->config['icon'] = $icon;
82 | }
83 | $this->flashConfig();
84 | return $this;
85 | }
86 |
87 | /**
88 | * Display a not typed alert message with a text and a title.
89 | *
90 | * @param string $text
91 | * @param string $title
92 | *
93 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
94 | */
95 | public function basic($text, $title)
96 | {
97 | $this->message($text, $title);
98 |
99 | return $this;
100 | }
101 |
102 | /**
103 | * Display an info typed alert message with a text and an optional title.
104 | *
105 | * @param string $text
106 | * @param string $title
107 | *
108 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
109 | */
110 | public function info($text, $title = '')
111 | {
112 | $this->message($text, $title, self::ICON_INFO);
113 |
114 | return $this;
115 | }
116 |
117 | /**
118 | * Display a success typed alert message with a text and an optional title.
119 | *
120 | * @param string $text
121 | * @param string $title
122 | *
123 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
124 | */
125 | public function success($text, $title = '')
126 | {
127 | $this->message($text, $title, self::ICON_SUCCESS);
128 |
129 | return $this;
130 | }
131 |
132 | /**
133 | * Display an error typed alert message with a text and an optional title.
134 | *
135 | * @param string $text
136 | * @param string $title
137 | *
138 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
139 | */
140 | public function error($text, $title = '')
141 | {
142 | $this->message($text, $title, self::ICON_ERROR);
143 |
144 | return $this;
145 | }
146 |
147 | /**
148 | * Display a warning typed alert message with a text and an optional title.
149 | *
150 | * @param string $text
151 | * @param string $title
152 | *
153 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
154 | */
155 | public function warning($text, $title = '')
156 | {
157 | $this->message($text, $title, self::ICON_WARNING);
158 |
159 | return $this;
160 | }
161 |
162 | /**
163 | * Set the duration for this alert until it autocloses.
164 | *
165 | * @param int $milliseconds
166 | *
167 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
168 | */
169 | public function autoclose($milliseconds = null)
170 | {
171 | if (! is_null($milliseconds)) {
172 | $this->config['timer'] = $milliseconds;
173 | }
174 |
175 | return $this;
176 | }
177 |
178 | /**
179 | * Add a confirmation button to the alert.
180 | *
181 | * @param string $buttonText
182 | *
183 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
184 | */
185 | public function confirmButton($buttonText = 'OK', $overrides = [])
186 | {
187 | $this->addButton('confirm', $buttonText, $overrides);
188 |
189 | return $this;
190 | }
191 |
192 | /**
193 | * Add a cancel button to the alert.
194 | *
195 | * @param string $buttonText
196 | * @param array $overrides
197 | *
198 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
199 | */
200 | public function cancelButton($buttonText = 'Cancel', $overrides = [])
201 | {
202 | $this->addButton('cancel', $buttonText, $overrides);
203 |
204 | return $this;
205 | }
206 |
207 | /**
208 | * Add a new custom button to the alert.
209 | *
210 | * @param string $key
211 | * @param string $buttonText
212 | * @param array $overrides
213 | *
214 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
215 | */
216 | public function addButton($key, $buttonText, $overrides = [])
217 | {
218 | $this->config['buttons'][$key] = array_merge(
219 | $this->defaultButtonConfig,
220 | [
221 | 'text' => $buttonText,
222 | 'visible' => true,
223 | ],
224 | $overrides
225 | );
226 |
227 | $this->closeOnClickOutside(false);
228 | $this->removeTimer();
229 |
230 | return $this;
231 | }
232 |
233 | /**
234 | * Toggle close the alert message when clicking outside.
235 | *
236 | * @param string $buttonText
237 | *
238 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
239 | */
240 | public function closeOnClickOutside($value = true)
241 | {
242 | $this->config['closeOnClickOutside'] = $value;
243 |
244 | return $this;
245 | }
246 |
247 | /**
248 | * Make this alert persistent with a confirmation button.
249 | *
250 | * @param string $buttonText
251 | *
252 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
253 | */
254 | public function persistent($buttonText = 'OK')
255 | {
256 | $this->addButton('confirm', $buttonText);
257 | $this->closeOnClickOutside(false);
258 | $this->removeTimer();
259 |
260 | return $this;
261 | }
262 |
263 | /**
264 | * Remove the timer config option.
265 | *
266 | * @return void
267 | */
268 | protected function removeTimer()
269 | {
270 | if (array_key_exists('timer', $this->config)) {
271 | unset($this->config['timer']);
272 | }
273 | }
274 |
275 | /**
276 | * Make Message HTML view.
277 | *
278 | * @param bool|true $html
279 | *
280 | * @return \UxWeb\SweetAlert\SweetAlertNotifier $this
281 | */
282 | public function html()
283 | {
284 | $this->config['content'] = $this->config['text'];
285 |
286 | unset($this->config['text']);
287 |
288 | return $this;
289 | }
290 |
291 | /**
292 | * Flash the current alert configuration to the session store.
293 | *
294 | * @return void
295 | */
296 | protected function flashConfig()
297 | {
298 | $this->session->remove('sweet_alert');
299 |
300 | foreach ($this->config as $key => $value) {
301 | $this->session->flash("sweet_alert.{$key}", $value);
302 | }
303 |
304 | $this->session->flash('sweet_alert.alert', $this->buildJsonConfig());
305 | }
306 |
307 | /**
308 | * Build the configuration as Json.
309 | *
310 | * @return string
311 | */
312 | protected function buildJsonConfig()
313 | {
314 | return json_encode($this->config);
315 | }
316 |
317 | /**
318 | * Return the current alert configuration.
319 | *
320 | * @return array
321 | */
322 | public function getConfig($key = null)
323 | {
324 | if (is_null($key)) {
325 | return $this->config;
326 | }
327 |
328 | if (array_key_exists($key, $this->config)) {
329 | return $this->config[$key];
330 | }
331 | }
332 |
333 | /**
334 | * Customize alert configuration "by hand".
335 | *
336 | * @return array
337 | */
338 | public function setConfig($config = [])
339 | {
340 | $this->config = array_merge($this->config, $config);
341 |
342 | return $this;
343 | }
344 |
345 | /**
346 | * Return the current alert configuration as Json.
347 | *
348 | * @return string
349 | */
350 | public function getJsonConfig()
351 | {
352 | return $this->buildJsonConfig();
353 | }
354 |
355 | /**
356 | * Handle the object's destruction.
357 | *
358 | * @return void
359 | */
360 | public function __destruct()
361 | {
362 | $this->flashConfig();
363 | }
364 | }
365 |
--------------------------------------------------------------------------------
/src/SweetAlert/SweetAlertServiceProvider.php:
--------------------------------------------------------------------------------
1 | loadViewsFrom(__DIR__ . '/../views', 'sweet');
17 |
18 | $this->publishes([
19 | __DIR__ . '/../config/sweet-alert.php' => config_path('sweet-alert.php'),
20 | ], 'config');
21 |
22 | $this->publishes([
23 | __DIR__ . '/../views' => base_path('resources/views/vendor/sweet'),
24 | ], 'views');
25 | }
26 |
27 | /**
28 | * Register the service provider.
29 | *
30 | * @return void
31 | */
32 | public function register()
33 | {
34 | $this->mergeConfigFrom(
35 | __DIR__ . '/../config/sweet-alert.php',
36 | 'sweet-alert'
37 | );
38 |
39 | $this->app->bind(
40 | 'UxWeb\SweetAlert\SessionStore',
41 | 'UxWeb\SweetAlert\LaravelSessionStore'
42 | );
43 |
44 | $this->app->bind('uxweb.sweet-alert', function () {
45 | return $this->app->make('UxWeb\SweetAlert\SweetAlertNotifier');
46 | });
47 | }
48 |
49 | /**
50 | * Get the services provided by the provider.
51 | *
52 | * @return array
53 | */
54 | public function provides()
55 | {
56 | return [
57 | 'UxWeb\SweetAlert\SessionStore',
58 | 'uxweb.sweet-alert',
59 | ];
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/SweetAlert/functions.php:
--------------------------------------------------------------------------------
1 | message($message, $title);
18 | }
19 |
20 | return $notifier;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/config/sweet-alert.php:
--------------------------------------------------------------------------------
1 | autoclose(milliseconds) on the end.
7 | */
8 | 'autoclose' => 2500,
9 | ];
10 |
--------------------------------------------------------------------------------
/src/views/alert.blade.php:
--------------------------------------------------------------------------------
1 | @if (Session::has('sweet_alert.alert'))
2 |
13 | @endif
14 |
--------------------------------------------------------------------------------
/src/views/alert.twig:
--------------------------------------------------------------------------------
1 | {% if session_has('sweet_alert.alert') %}
2 |
13 | {% endif %}
14 |
--------------------------------------------------------------------------------
/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/uxweb/sweet-alert/d0bf6a800b0670d03189f1c2c35d3e5f7c1b3bb7/tests/.gitkeep
--------------------------------------------------------------------------------
/tests/SweetAlertNotifierTest.php:
--------------------------------------------------------------------------------
1 | message();
19 |
20 | $this->assertEquals('', $notifier->getConfig('text'));
21 | }
22 |
23 | /** @test */
24 | public function default_timer_is_2500_milliseconds()
25 | {
26 | $session = m::spy(SessionStore::class);
27 | $notifier = new SweetAlertNotifier($session);
28 |
29 | $notifier->message('Good News!');
30 |
31 | $this->assertEquals(2500, $notifier->getConfig('timer'));
32 | }
33 |
34 | /** @test */
35 | public function buttons_config_is_false_by_default()
36 | {
37 | $session = m::spy(SessionStore::class);
38 | $notifier = new SweetAlertNotifier($session);
39 |
40 | $notifier->message('Good News!');
41 |
42 | $buttonsConfig = [
43 | 'confirm' => false,
44 | 'cancel' => false,
45 | ];
46 | $this->assertEquals($buttonsConfig, $notifier->getConfig('buttons'));
47 | }
48 |
49 | /** @test */
50 | public function first_parameter_of_alert_message_is_the_config_text()
51 | {
52 | $session = m::spy(SessionStore::class);
53 | $notifier = new SweetAlertNotifier($session);
54 |
55 | $notifier->message('Hello World!');
56 |
57 | $this->assertEquals('Hello World!', $notifier->getConfig('text'));
58 | }
59 |
60 | /** @test */
61 | public function title_key_is_not_present_in_config_when_alert_title_is_not_set()
62 | {
63 | $session = m::spy(SessionStore::class);
64 | $notifier = new SweetAlertNotifier($session);
65 |
66 | $notifier->message('Hello World!');
67 |
68 | $this->assertArrayNotHasKey('title', $notifier->getConfig());
69 | }
70 |
71 | /** @test */
72 | public function second_parameter_of_alert_message_is_the_config_title()
73 | {
74 | $session = m::spy(SessionStore::class);
75 | $notifier = new SweetAlertNotifier($session);
76 |
77 | $notifier->message('Hello World!', 'This is the title');
78 |
79 | $this->assertEquals('This is the title', $notifier->getConfig('title'));
80 | }
81 |
82 | /** @test */
83 | public function third_parameter_of_alert_message_is_the_config_icon()
84 | {
85 | $session = m::spy(SessionStore::class);
86 | $notifier = new SweetAlertNotifier($session);
87 |
88 | $notifier->message('Hello World!', 'This is the title', 'info');
89 |
90 | $this->assertEquals('info', $notifier->getConfig('icon'));
91 | }
92 |
93 | /** @test */
94 | public function icon_key_is_not_present_in_config_when_alert_icon_is_not_set()
95 | {
96 | $session = m::spy(SessionStore::class);
97 | $notifier = new SweetAlertNotifier($session);
98 |
99 | $notifier->message('Hello World!', 'This is the title');
100 |
101 | $this->assertArrayNotHasKey('icon', $notifier->getConfig());
102 | }
103 |
104 | /** @test */
105 | public function it_flashes_config_for_a_basic_alert()
106 | {
107 | $session = m::spy(SessionStore::class);
108 | $notifier = new SweetAlertNotifier($session);
109 |
110 | $notifier->basic('Basic Alert!', 'Alert');
111 |
112 | $expectedConfig = [
113 | 'text' => 'Basic Alert!',
114 | 'title' => 'Alert',
115 | ];
116 | $this->assertArraySubset($expectedConfig, $notifier->getConfig());
117 | unset($notifier);
118 | $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once();
119 | $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text'])->once();
120 | $session->shouldHaveReceived('flash')->with('sweet_alert.alert', \Hamcrest\Text\IsEmptyString::isNonEmptyString())->once();
121 | }
122 |
123 | /** @test */
124 | public function it_flashes_config_for_a_info_alert()
125 | {
126 | $session = m::spy(SessionStore::class);
127 | $notifier = new SweetAlertNotifier($session);
128 |
129 | $notifier->info('Info Alert!', 'Alert');
130 |
131 | $expectedConfig = [
132 | 'text' => 'Info Alert!',
133 | 'title' => 'Alert',
134 | 'icon' => 'info',
135 | ];
136 | $this->assertArraySubset($expectedConfig, $notifier->getConfig());
137 | unset($notifier);
138 | $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title'])->once();
139 | $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text'])->once();
140 | $session->shouldHaveReceived('flash')->with('sweet_alert.icon', $expectedConfig['icon'])->once();
141 | $session->shouldHaveReceived('flash')->with('sweet_alert.alert', \Hamcrest\Text\IsEmptyString::isNonEmptyString())->once();
142 | }
143 |
144 | /** @test */
145 | public function it_flashes_config_for_a_success_alert()
146 | {
147 | $session = m::spy(SessionStore::class);
148 | $notifier = new SweetAlertNotifier($session);
149 |
150 | $notifier->success('Well Done!', 'Success!');
151 |
152 | $expectedConfig = [
153 | 'title' => 'Success!',
154 | 'text' => 'Well Done!',
155 | 'icon' => 'success',
156 | ];
157 | $this->assertArraySubset($expectedConfig, $notifier->getConfig());
158 | unset($notifier);
159 | $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']);
160 | $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']);
161 | $session->shouldReceive('flash')->with('sweet_alert.icon', $expectedConfig['icon']);
162 | $session->shouldHaveReceived('flash')->with('sweet_alert.alert', \Hamcrest\Matchers::isNonEmptyString())->once();
163 | }
164 |
165 | /** @test */
166 | public function it_flashes_config_for_a_warning_alert()
167 | {
168 | $session = m::spy(SessionStore::class);
169 | $notifier = new SweetAlertNotifier($session);
170 |
171 | $notifier->warning('Hey cowboy!', 'Watch Out!');
172 |
173 | $expectedConfig = [
174 | 'title' => 'Watch Out!',
175 | 'text' => 'Hey cowboy!',
176 | 'icon' => 'warning',
177 | ];
178 | $this->assertArraySubset($expectedConfig, $notifier->getConfig());
179 | unset($notifier);
180 | $session->shouldReceive('flash')->with('sweet_alert.title', $expectedConfig['title']);
181 | $session->shouldReceive('flash')->with('sweet_alert.text', $expectedConfig['text']);
182 | $session->shouldReceive('flash')->with('sweet_alert.icon', $expectedConfig['icon']);
183 | $session->shouldHaveReceived('flash')->with('sweet_alert.alert', \Hamcrest\Matchers::isNonEmptyString())->once();
184 | }
185 |
186 | /** @test */
187 | public function it_flashes_config_for_a_error_alert()
188 | {
189 | $session = m::spy(SessionStore::class);
190 | $notifier = new SweetAlertNotifier($session);
191 |
192 | $notifier->error('Something wrong happened!', 'Whoops!');
193 |
194 | $expectedConfig = [
195 | 'title' => 'Whoops!',
196 | 'text' => 'Something wrong happened!',
197 | 'icon' => 'error',
198 | ];
199 | $this->assertArraySubset($expectedConfig, $notifier->getConfig());
200 | unset($notifier);
201 | $session->shouldHaveReceived('flash')->with('sweet_alert.title', $expectedConfig['title']);
202 | $session->shouldHaveReceived('flash')->with('sweet_alert.text', $expectedConfig['text']);
203 | $session->shouldHaveReceived('flash')->with('sweet_alert.icon', $expectedConfig['icon']);
204 | $session->shouldHaveReceived('flash')->with('sweet_alert.alert', \Hamcrest\Matchers::isNonEmptyString())->once();
205 | }
206 |
207 | /** @test */
208 | public function autoclose_can_be_customized_for_an_alert_message()
209 | {
210 | $session = m::spy(SessionStore::class);
211 | $notifier = new SweetAlertNotifier($session);
212 |
213 | $notifier->message('Hello!', 'Alert')->autoclose(2000);
214 |
215 | $this->assertEquals(2000, $notifier->getConfig('timer'));
216 | unset($notifier);
217 | $session->shouldHaveReceived('flash')->with('sweet_alert.timer', 2000);
218 | }
219 |
220 | /** @test */
221 | public function timer_option_is_not_present_in_config_when_using_a_persistent_alert()
222 | {
223 | $session = m::mock(SessionStore::class);
224 | $session->shouldReceive('flash')->atLeast(1);
225 | $session->shouldReceive('remove')->atLeast(1);
226 | $notifier = new SweetAlertNotifier($session);
227 |
228 | $notifier->message('Please, read with care!', 'Alert')->persistent('Got it!');
229 |
230 | $this->assertArrayNotHasKey('timer', $notifier->getConfig());
231 | }
232 |
233 | /** @test */
234 | public function persistent_alert_has_only_a_confirm_button_by_default()
235 | {
236 | $session = m::mock(SessionStore::class);
237 | $session->shouldReceive('flash')->atLeast(1);
238 | $session->shouldReceive('remove')->atLeast(1);
239 | $notifier = new SweetAlertNotifier($session);
240 |
241 | $notifier->warning('Are you sure?', 'Delete all posts')->persistent('I\'m sure');
242 |
243 | $this->assertArraySubset(
244 | [
245 | 'confirm' => [
246 | 'text' => 'I\'m sure',
247 | 'visible' => true,
248 | ],
249 | ],
250 | $notifier->getConfig('buttons')
251 | );
252 | }
253 |
254 | /** @test */
255 | public function it_will_add_the_content_option_to_config_when_using_an_html_alert()
256 | {
257 | $session = m::mock(SessionStore::class);
258 | $session->shouldReceive('flash')->atLeast(1);
259 | $session->shouldReceive('remove')->atLeast(1);
260 | $notifier = new SweetAlertNotifier($session);
261 |
262 | $notifier->message('This should be bold!', 'Alert')->html();
263 |
264 | $this->assertEquals('This should be bold!', $notifier->getConfig('content'));
265 | }
266 |
267 | /** @test */
268 | public function allows_to_configure_a_confirm_button_for_an_alert()
269 | {
270 | $session = m::mock(SessionStore::class);
271 | $session->shouldReceive('flash')->atLeast(1);
272 | $session->shouldReceive('remove')->atLeast(1);
273 | $notifier = new SweetAlertNotifier($session);
274 |
275 | $notifier->basic('Basic Alert!', 'Alert')->confirmButton('help!');
276 |
277 | $this->assertArraySubset(
278 | [
279 | 'text' => 'help!',
280 | 'visible' => true,
281 | ],
282 | $notifier->getConfig('buttons')['confirm']
283 | );
284 | $this->assertFalse($notifier->getConfig('closeOnClickOutside'));
285 | }
286 |
287 | /** @test */
288 | public function allows_to_configure_a_cancel_button_for_an_alert()
289 | {
290 | $session = m::spy(SessionStore::class);
291 | $notifier = new SweetAlertNotifier($session);
292 |
293 | $notifier->basic('Basic Alert!', 'Alert')->cancelButton('Cancel!');
294 |
295 | $this->assertArraySubset(['text' => 'Cancel!', 'visible' => true], $notifier->getConfig('buttons')['cancel']);
296 | $this->assertFalse($notifier->getConfig('closeOnClickOutside'));
297 | }
298 |
299 | /** @test */
300 | public function close_on_click_outside_config_can_be_enabled()
301 | {
302 | $session = m::spy(SessionStore::class);
303 | $notifier = new SweetAlertNotifier($session);
304 |
305 | $notifier->basic('Basic Alert!', 'Alert')->closeOnClickOutside();
306 |
307 | $this->assertTrue($notifier->getConfig('closeOnClickOutside'));
308 | }
309 |
310 | /** @test */
311 | public function close_on_click_outside_config_can_be_disabled()
312 | {
313 | $session = m::spy(SessionStore::class);
314 | $notifier = new SweetAlertNotifier($session);
315 |
316 | $notifier->basic('Basic Alert!', 'Alert')->closeOnClickOutside(false);
317 |
318 | $this->assertFalse($notifier->getConfig('closeOnClickOutside'));
319 | }
320 |
321 | /** @test */
322 | public function additional_buttons_can_be_added()
323 | {
324 | $session = m::spy(SessionStore::class);
325 | $notifier = new SweetAlertNotifier($session);
326 |
327 | $notifier->basic('Pay with:', 'Payment')->addButton('credit_card', 'Credit Card');
328 | $notifier->basic('Pay with:', 'Payment')->addButton('paypal', 'Paypal');
329 |
330 | $this->assertArraySubset(
331 | [
332 | 'credit_card' => [
333 | 'text' => 'Credit Card',
334 | 'visible' => true,
335 | ],
336 | 'paypal' => [
337 | 'text' => 'Paypal',
338 | 'visible' => true,
339 | ],
340 | ],
341 | $notifier->getConfig('buttons')
342 | );
343 | $this->assertFalse($notifier->getConfig('closeOnClickOutside'));
344 | }
345 |
346 | /** @test */
347 | public function additional_config_can_be_added_to_configure_alert_message()
348 | {
349 | $session = m::spy(SessionStore::class);
350 | $notifier = new SweetAlertNotifier($session);
351 |
352 | $notifier->basic('Basic Alert!', 'Alert')->setConfig(['dangerMode' => true]);
353 |
354 | $this->assertTrue($notifier->getConfig('dangerMode'));
355 | unset($notifier);
356 | $session->shouldHaveReceived('flash')->with('sweet_alert.dangerMode', true);
357 | }
358 | }
359 |
360 | /**
361 | * Get / set the specified configuration value.
362 | *
363 | * If an array is passed as the key, we will assume you want to set an array of values.
364 | *
365 | * @param array|string $key
366 | * @param mixed $default
367 | *
368 | * @return mixed|\Illuminate\Config\Repository
369 | */
370 | function config($key = null, $default = null)
371 | {
372 | return 2500;
373 | }
374 |
--------------------------------------------------------------------------------
/travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | php:
4 | - 7.2
5 | - 7.3
6 | - 7.4
7 |
8 | before_script:
9 | - composer self-update
10 | - composer install --prefer-source --no-interaction --dev
11 |
--------------------------------------------------------------------------------