diff --git a/.github/workflows/alert.yml b/.github/workflows/alert.yml index 6630f3c8..e806cf9e 100644 --- a/.github/workflows/alert.yml +++ b/.github/workflows/alert.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - php-version: ['8.1', '8.2'] + php-version: ['8.2'] steps: - uses: actions/checkout@v3 diff --git a/config/alert.php b/config/alert.php index 6e495f8d..5da7adb5 100644 --- a/config/alert.php +++ b/config/alert.php @@ -30,9 +30,9 @@ 'icons' => [ 'success' => \Digitlimit\Alert\Icons\Success::class, - 'error' => \Digitlimit\Alert\Icons\Error::class, + 'error' => \Digitlimit\Alert\Icons\Error::class, 'warning' => \Digitlimit\Alert\Icons\Warning::class, - 'info' => \Digitlimit\Alert\Icons\Info::class, + 'info' => \Digitlimit\Alert\Icons\Info::class, ], /* @@ -46,32 +46,32 @@ 'tailwind' => [ 'types' => [ 'message' => [ - 'view' => 'alert-message', - 'alert' => Types\Message::class, + 'view' => 'alert-message', + 'alert' => Types\Message::class, 'component' => Themes\Tailwind\Components\Message::class, ], 'field' => [ - 'view' => 'alert-field', - 'alert' => Types\Field::class, + 'view' => 'alert-field', + 'alert' => Types\Field::class, 'component' => Themes\Tailwind\Components\Field::class, ], 'modal' => [ - 'view' => 'alert-modal', - 'alert' => Types\Modal::class, + 'view' => 'alert-modal', + 'alert' => Types\Modal::class, 'component' => Themes\Tailwind\Components\Modal::class, ], 'toastr' => [ - 'view' => 'alert-toastr', - 'alert' => Types\Toastr::class, + 'view' => 'alert-toastr', + 'alert' => Types\Toastr::class, 'component' => Themes\Tailwind\Components\Toastr::class, ], 'notify' => [ - 'view' => 'alert-notify', - 'alert' => Types\Notify::class, + 'view' => 'alert-notify', + 'alert' => Types\Notify::class, 'component' => Themes\Tailwind\Components\Notify::class, ], ], @@ -79,21 +79,21 @@ 'attributes' => [ 'buttons' => [ 'action' => [ - 'type' => 'button', + 'type' => 'button', '@click' => 'show = false;', ], 'cancel' => [ - 'type' => 'button', + 'type' => 'button', '@click' => 'show = false;', ], ], 'links' => [ 'action' => [ - 'type' => 'button', + 'type' => 'button', '@click' => 'show = false;', ], 'cancel' => [ - 'type' => 'button', + 'type' => 'button', '@click' => 'show = false;', ], ], diff --git a/resources/css/themes/tailwind/alert.css b/resources/css/themes/tailwind/alert.css index 13f633eb..f71dbbd0 100644 --- a/resources/css/themes/tailwind/alert.css +++ b/resources/css/themes/tailwind/alert.css @@ -34,34 +34,42 @@ @apply inline-flex items-center justify-center h-10 px-4 py-2 text-sm font-medium text-blue-600 transition-colors rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 hover:underline; } +.alert-position, .alert-toastr-position { @apply fixed px-2 mt-3 overflow-x-hidden z-50; } +.alert-position.top-right, .alert-toastr-position.top-right { @apply top-0 right-0; } +.alert-position.top-left, .alert-toastr-position.top-left { @apply top-0 left-0; } +.alert-position.bottom-right, .alert-toastr-position.bottom-right { @apply bottom-0 right-0; } +.alert-position.bottom-left, .alert-toastr-position.bottom-left { @apply bottom-0 left-0; } +.alert-position.top-center, .alert-toastr-position.top-center { @apply top-0 left-1/2 transform -translate-x-1/2; } +.alert-position.bottom-center, .alert-toastr-position.bottom-center { @apply bottom-0 left-1/2 transform -translate-x-1/2; } +.alert-position.center, .alert-toastr-position.center { @apply top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2; } diff --git a/resources/css/themes/tailwind/components/position.css b/resources/css/themes/tailwind/components/position.css index 269b9d2f..2bab0385 100644 --- a/resources/css/themes/tailwind/components/position.css +++ b/resources/css/themes/tailwind/components/position.css @@ -1,24 +1,32 @@ +.alert-position, .alert-toastr-position { @apply fixed px-2 mt-3 overflow-x-hidden z-50; } +.alert-position.top-right, .alert-toastr-position.top-right { @apply top-0 right-0; } +.alert-position.top-left, .alert-toastr-position.top-left { @apply top-0 left-0; } +.alert-position.bottom-right, .alert-toastr-position.bottom-right { @apply bottom-0 right-0; } +.alert-position.bottom-left, .alert-toastr-position.bottom-left { @apply bottom-0 left-0; } +.alert-position.top-center, .alert-toastr-position.top-center { @apply top-0 left-1/2 transform -translate-x-1/2; } +.alert-position.bottom-center, .alert-toastr-position.bottom-center { @apply bottom-0 left-1/2 transform -translate-x-1/2; } +.alert-position.center, .alert-toastr-position.center { @apply top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2; } diff --git a/resources/scss/themes/tailwind/alert.css b/resources/scss/themes/tailwind/alert.css index 7e39120e..2d88747c 100644 --- a/resources/scss/themes/tailwind/alert.css +++ b/resources/scss/themes/tailwind/alert.css @@ -26,27 +26,35 @@ @apply inline-flex items-center justify-center h-10 px-4 py-2 text-sm font-medium text-blue-600 transition-colors rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 hover:underline; } +.alert-position, .alert-toastr-position { @apply fixed px-2 mt-3 overflow-x-hidden z-50; } +.alert-position.top-right, .alert-toastr-position.top-right { @apply top-0 right-0; } +.alert-position.top-left, .alert-toastr-position.top-left { @apply top-0 left-0; } +.alert-position.bottom-right, .alert-toastr-position.bottom-right { @apply bottom-0 right-0; } +.alert-position.bottom-left, .alert-toastr-position.bottom-left { @apply bottom-0 left-0; } +.alert-position.top-center, .alert-toastr-position.top-center { @apply top-0 left-1/2 transform -translate-x-1/2; } +.alert-position.bottom-center, .alert-toastr-position.bottom-center { @apply bottom-0 left-1/2 transform -translate-x-1/2; } +.alert-position.center, .alert-toastr-position.center { @apply top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2; } diff --git a/resources/scss/themes/tailwind/alert.css.map b/resources/scss/themes/tailwind/alert.css.map index 331fc271..85bb3126 100644 --- a/resources/scss/themes/tailwind/alert.css.map +++ b/resources/scss/themes/tailwind/alert.css.map @@ -1 +1 @@ -{"version":3,"sourceRoot":"","sources":["components/button.scss","components/position.scss","components/field.scss","components/message.scss","components/toastr.scss","components/notify.scss","components/modal.scss"],"names":[],"mappings":"AACI;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;;AClCR;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;;AC3BF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;;ACdJ;EAEE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAEA;EACI;;AAIN;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;;AC5DN;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;IAAO;;EACP;IAAK;;;AAGP;EACE;;;ACrDJ;EACE;;AAEA;EACE;;AAGF;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAIJ;EACE;;AAGF;EAEE;;AAGF;EAEE;;AAEA;EACE;;AAGF;EACE;;AAIJ;EACE;IAAO;;EACP;IAAK;;;AAGP;EACE;EACA;;;ACpEJ;EACE;;AAEA;EACE;;AAGF;EACE;;AAEA;EAAU;;AACV;EAAW;;AACX;EAAU;;AACV;EAAgB;;AAChB;EAAe;;AACf;EAAe;;AAGjB;EACE;;AAEA;EAAiB;;AACjB;EAAe;;AACf;EAAiB;;AACjB;EAAc;;AAEd;EACE;;AAGF;EACE;;AAGF;EACE;;AAIJ;EACE;;AAGF;EACE;;AAEA;EACE;;AAGF;EACE","file":"alert.css"} \ No newline at end of file +{"version":3,"sourceRoot":"","sources":["components/button.scss","components/position.scss","components/field.scss","components/message.scss","components/toastr.scss","components/notify.scss","components/modal.scss"],"names":[],"mappings":"AACI;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;AAGJ;EACI;;;AClCR;AAAA;EAEE;;AAEA;AAAA;EACE;;AAGF;AAAA;EACE;;AAGF;AAAA;EACE;;AAGF;AAAA;EACE;;AAGF;AAAA;EACE;;AAGF;AAAA;EACE;;AAGF;AAAA;EACE;;;AC5BF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;;ACdJ;EAEE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAEA;EACI;;AAIN;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;;AC5DN;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;IAAO;;EACP;IAAK;;;AAGP;EACE;;;ACrDJ;EACE;;AAEA;EACE;;AAGF;EACE;;AAEA;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAGF;EACE;;AAIJ;EACE;;AAGF;EAEE;;AAGF;EAEE;;AAEA;EACE;;AAGF;EACE;;AAIJ;EACE;IAAO;;EACP;IAAK;;;AAGP;EACE;EACA;;;ACpEJ;EACE;;AAEA;EACE;;AAGF;EACE;;AAEA;EAAU;;AACV;EAAW;;AACX;EAAU;;AACV;EAAgB;;AAChB;EAAe;;AACf;EAAe;;AAGjB;EACE;;AAEA;EAAiB;;AACjB;EAAe;;AACf;EAAiB;;AACjB;EAAc;;AAEd;EACE;;AAGF;EACE;;AAGF;EACE;;AAIJ;EACE;;AAGF;EACE;;AAEA;EACE;;AAGF;EACE","file":"alert.css"} \ No newline at end of file diff --git a/resources/scss/themes/tailwind/components/position.scss b/resources/scss/themes/tailwind/components/position.scss index 3ae5a982..68338054 100644 --- a/resources/scss/themes/tailwind/components/position.scss +++ b/resources/scss/themes/tailwind/components/position.scss @@ -1,3 +1,4 @@ +.alert-position, .alert-toastr-position { @apply fixed px-2 mt-3 overflow-x-hidden z-50; diff --git a/resources/views/themes/tailwind/components/toastr-classic.blade.php b/resources/views/themes/tailwind/components/toastr-classic.blade.php new file mode 100644 index 00000000..41a28ca3 --- /dev/null +++ b/resources/views/themes/tailwind/components/toastr-classic.blade.php @@ -0,0 +1,73 @@ +
+
+ +
+
diff --git a/src/Alert.php b/src/Alert.php index e61fa459..143b62ec 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -43,11 +43,11 @@ public static function typeKey(string $type): string */ public static function named(string $type, string $name, string $tag): ?HasName { - if (! Type::exists($type)) { + if (!Type::exists($type)) { throw new Exception("Invalid alert type '$type'. Check the alert config"); } - $tag = $tag . '.' . $name; + $tag = $tag.'.'.$name; return Session::get( SessionKey::key($type, $tag) @@ -61,7 +61,7 @@ public static function named(string $type, string $name, string $tag): ?HasName */ public static function tagged(string $type, string $tag): Collection { - if (! Type::exists($type)) { + if (!Type::exists($type)) { throw new Exception("Invalid alert type '$type'. Check the alert config"); } @@ -79,7 +79,7 @@ public static function tagged(string $type, string $tag): Collection */ public static function from(string $type, ...$args): AlertInterface { - if (! Type::exists($type)) { + if (!Type::exists($type)) { throw new Exception("Invalid alert type '$type'. Check the alert config"); } @@ -152,7 +152,7 @@ public static function fromArray(array $alert): ?AlertInterface $type = $alert['type'] ?? null; - if (! $type || ! Type::exists($type)) { + if (!$type || !Type::exists($type)) { throw new Exception("Invalid alert type '$type'. Check the alert config"); } @@ -176,7 +176,7 @@ public static function has(string $type): bool { $types = Session::get(SessionKey::typeKey($type)) ?? []; - return ! empty($types); + return !empty($types); } /** diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index de7b7082..7c750136 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -74,4 +74,4 @@ protected function registerMacros(): void ->forget($tag); }); } -} \ No newline at end of file +} diff --git a/src/Component/Button.php b/src/Component/Button.php index e09e8e16..b37f303c 100644 --- a/src/Component/Button.php +++ b/src/Component/Button.php @@ -116,7 +116,7 @@ public function getLabel(): ?string */ public function isLink(): bool { - return ! empty($this->link); + return !empty($this->link); } /** @@ -141,10 +141,10 @@ public function isCancel(): bool public function toArray(): array { return [ - 'id' => $this->getId(), - 'name' => $this->getName(), - 'label' => $this->getLabel(), - 'link' => $this->getLink(), + 'id' => $this->getId(), + 'name' => $this->getName(), + 'label' => $this->getLabel(), + 'link' => $this->getLink(), 'attributes' => $this->getAttributes(), ]; } diff --git a/src/Contracts/Closable.php b/src/Contracts/Closable.php index b854c06c..8af759f1 100644 --- a/src/Contracts/Closable.php +++ b/src/Contracts/Closable.php @@ -3,26 +3,26 @@ namespace Digitlimit\Alert\Contracts; /** - * Alert closable contract + * Alert closable contract. */ interface Closable { /** - * Set the alert to be closable + * Set the alert to be closable. * * @return $this */ public function closable(bool $closable): self; /** - * Set the alert to be not closable + * Set the alert to be not closable. * * @return $this */ public function notClosable(): self; /** - * Check if the alert is closable + * Check if the alert is closable. */ public function isClosable(): bool; } diff --git a/src/Contracts/HasButton.php b/src/Contracts/HasButton.php index 7a61132b..dbd0bc79 100644 --- a/src/Contracts/HasButton.php +++ b/src/Contracts/HasButton.php @@ -5,7 +5,7 @@ use Illuminate\Support\Collection; /** - * Alert button contract + * Alert button contract. */ interface HasButton { diff --git a/src/Contracts/HasMessage.php b/src/Contracts/HasMessage.php index 15f71e4e..363ad5e4 100644 --- a/src/Contracts/HasMessage.php +++ b/src/Contracts/HasMessage.php @@ -3,7 +3,7 @@ namespace Digitlimit\Alert\Contracts; /** - * Alert message contract + * Alert message contract. */ interface HasMessage { diff --git a/src/Contracts/HasName.php b/src/Contracts/HasName.php index f5daf94d..438320ba 100644 --- a/src/Contracts/HasName.php +++ b/src/Contracts/HasName.php @@ -3,7 +3,7 @@ namespace Digitlimit\Alert\Contracts; /** - * Has name contract + * Has name contract. */ interface HasName { diff --git a/src/Contracts/LivewireInterface.php b/src/Contracts/LivewireInterface.php index caffe3a1..d77c5df6 100644 --- a/src/Contracts/LivewireInterface.php +++ b/src/Contracts/LivewireInterface.php @@ -2,8 +2,6 @@ namespace Digitlimit\Alert\Contracts; -use Illuminate\Support\Collection; - interface LivewireInterface { public function refresh(string $tag, array $alerts): void; diff --git a/src/Contracts/Sizable.php b/src/Contracts/Sizable.php index 484b6bc0..23b37549 100644 --- a/src/Contracts/Sizable.php +++ b/src/Contracts/Sizable.php @@ -35,14 +35,14 @@ public function extraLarge(): self; public function fullscreen(): self; /** - * Set modal size + * Set modal size. * * @throws Exception */ public function size(string $size): self; /** - * Get the size of the alert + * Get the size of the alert. */ public function getSize(): string; } diff --git a/src/Contracts/ThemeInterface.php b/src/Contracts/ThemeInterface.php index 6b58dad2..2ce878bf 100644 --- a/src/Contracts/ThemeInterface.php +++ b/src/Contracts/ThemeInterface.php @@ -8,5 +8,4 @@ interface ThemeInterface * Alert types. */ public static function types(): array; - } diff --git a/src/Events/Field/Flashed.php b/src/Events/Field/Flashed.php index 8f6bb08c..89a9b9b7 100644 --- a/src/Events/Field/Flashed.php +++ b/src/Events/Field/Flashed.php @@ -11,14 +11,17 @@ class Flashed { - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable; + use InteractsWithSockets; + use SerializesModels; /** * Create a new event instance. */ public function __construct( public Field $alert - ) {} + ) { + } /** * Get the channels the event should broadcast on. diff --git a/src/Events/Message/Flashed.php b/src/Events/Message/Flashed.php index 57c53064..82a0270c 100644 --- a/src/Events/Message/Flashed.php +++ b/src/Events/Message/Flashed.php @@ -11,14 +11,17 @@ class Flashed { - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable; + use InteractsWithSockets; + use SerializesModels; /** * Create a new event instance. */ public function __construct( public Message $alert - ) {} + ) { + } /** * Get the channels the event should broadcast on. diff --git a/src/Events/Modal/Flashed.php b/src/Events/Modal/Flashed.php index 5fc7fa87..2329ec7d 100644 --- a/src/Events/Modal/Flashed.php +++ b/src/Events/Modal/Flashed.php @@ -11,14 +11,17 @@ class Flashed { - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable; + use InteractsWithSockets; + use SerializesModels; /** * Create a new event instance. */ public function __construct( public Modal $alert - ) {} + ) { + } /** * Get the channels the event should broadcast on. diff --git a/src/Events/Notify/Flashed.php b/src/Events/Notify/Flashed.php index f934e2c1..8f66c94a 100644 --- a/src/Events/Notify/Flashed.php +++ b/src/Events/Notify/Flashed.php @@ -11,14 +11,17 @@ class Flashed { - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable; + use InteractsWithSockets; + use SerializesModels; /** * Create a new event instance. */ public function __construct( public Notify $alert - ) {} + ) { + } /** * Get the channels the event should broadcast on. diff --git a/src/Events/Toastr/Flashed.php b/src/Events/Toastr/Flashed.php index faa3f782..0b3c5ab4 100644 --- a/src/Events/Toastr/Flashed.php +++ b/src/Events/Toastr/Flashed.php @@ -11,14 +11,17 @@ class Flashed { - use Dispatchable, InteractsWithSockets, SerializesModels; + use Dispatchable; + use InteractsWithSockets; + use SerializesModels; /** * Create a new event instance. */ public function __construct( public Toastr $alert - ) {} + ) { + } /** * Get the channels the event should broadcast on. diff --git a/src/Factory/AlertFactory.php b/src/Factory/AlertFactory.php index d546fd8f..46b5d2da 100644 --- a/src/Factory/AlertFactory.php +++ b/src/Factory/AlertFactory.php @@ -20,7 +20,7 @@ public static function make(string $type, ...$args): AlertInterface { $class = Type::classname($type); - if (! class_exists($class)) { + if (!class_exists($class)) { throw new Exception("Alert type '$class' class not found "); } @@ -37,7 +37,7 @@ public static function makeFromArray(array $alert): AlertInterface $type = $alert['type']; $class = Type::classname($type); - if (! class_exists($class)) { + if (!class_exists($class)) { throw new Exception("Alert type '$class' class not found "); } diff --git a/src/Foundation/AbstractAlert.php b/src/Foundation/AbstractAlert.php index 2143dcc5..df2bd5a4 100644 --- a/src/Foundation/AbstractAlert.php +++ b/src/Foundation/AbstractAlert.php @@ -5,14 +5,14 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Helpers\Helper; use Digitlimit\Alert\Helpers\SessionKey; -use Illuminate\Support\Facades\Session; -use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Support\Facades\Session; /** * @property string $message */ -abstract class AbstractAlert implements AlertInterface, Jsonable, Arrayable +abstract class AbstractAlert implements AlertInterface, Arrayable, Jsonable { /** * Alert unique ID. @@ -50,7 +50,7 @@ public function id(string|int $id): self */ public function autoSetId(): self { - if (! empty($this->id)) { + if (!empty($this->id)) { return $this; } @@ -72,7 +72,7 @@ public function getId(): string|int */ public function getIdTag(): string { - return $this->getTag() . '.' . $this->getId(); + return $this->getTag().'.'.$this->getId(); } /** @@ -103,7 +103,7 @@ public function flash(): void public function forget(?string $tag = null): self { $tag = $tag - ? $tag . '.' . $this->getId() + ? $tag.'.'.$this->getId() : $this->getIdTag(); $sessionKey = SessionKey::key($this->key(), $tag); diff --git a/src/Foundation/AbstractTheme.php b/src/Foundation/AbstractTheme.php index 846931eb..2a17e449 100644 --- a/src/Foundation/AbstractTheme.php +++ b/src/Foundation/AbstractTheme.php @@ -4,4 +4,6 @@ use Digitlimit\Alert\Contracts\ThemeInterface; -abstract class AbstractTheme implements ThemeInterface {} +abstract class AbstractTheme implements ThemeInterface +{ +} diff --git a/src/Helpers.php b/src/Helpers.php index 0e09601c..4c2ca604 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -1,14 +1,15 @@ message($message); @@ -20,14 +21,16 @@ function alert(string $message, ?string $title = null): Message { } } -if (! function_exists('field')) { - function field(string $name, string $message): Field { +if (!function_exists('field')) { + function field(string $name, string $message): Field + { return app('alert')->field($name, $message); } } -if (! function_exists('modal')) { - function modal(string $message, ?string $title = null,): Modal { +if (!function_exists('modal')) { + function modal(string $message, ?string $title = null): Modal + { $alert = app('alert')->modal($message); if ($title) { @@ -38,8 +41,9 @@ function modal(string $message, ?string $title = null,): Modal { } } -if (! function_exists('toastr')) { - function toastr(string $message, ?string $title = null,): Toastr { +if (!function_exists('toastr')) { + function toastr(string $message, ?string $title = null): Toastr + { $alert = app('alert')->toastr($message); if ($title) { @@ -50,8 +54,9 @@ function toastr(string $message, ?string $title = null,): Toastr { } } -if (! function_exists('notify')) { - function notify(string $message, ?string $title = null,): Notify { +if (!function_exists('notify')) { + function notify(string $message, ?string $title = null): Notify + { $alert = app('alert')->notify($message); if ($title) { @@ -62,8 +67,9 @@ function notify(string $message, ?string $title = null,): Notify { } } -if (! function_exists('forgetAlert')) { - function forgetAlert(string $type, string $tag = Alert::DEFAULT_TAG): void { +if (!function_exists('forgetAlert')) { + function forgetAlert(string $type, string $tag = Alert::DEFAULT_TAG): void + { app('alert')->forget($type, $tag); } } diff --git a/src/Helpers/Theme.php b/src/Helpers/Theme.php index 872d63a4..30971ca6 100644 --- a/src/Helpers/Theme.php +++ b/src/Helpers/Theme.php @@ -31,7 +31,7 @@ public static function theme(): ThemeInterface $theme = $themes[$name] ?? null; - if (! $theme) { + if (!$theme) { throw new Exception("Theme {$name} not found"); } diff --git a/src/Helpers/Type.php b/src/Helpers/Type.php index efeb57e8..3e94ebe4 100644 --- a/src/Helpers/Type.php +++ b/src/Helpers/Type.php @@ -57,10 +57,10 @@ public static function type(string $type): array */ public static function classname(string $type): string { - if (! self::exists($type)) { + if (!self::exists($type)) { throw new Exception("The alert type '$type' does not exist in config"); } return self::type($type)['alert']; } -} \ No newline at end of file +} diff --git a/src/Helpers/ValidationError.php b/src/Helpers/ValidationError.php index ed3dc040..04e06c32 100644 --- a/src/Helpers/ValidationError.php +++ b/src/Helpers/ValidationError.php @@ -15,7 +15,7 @@ public function errors(): MessageBag return $errors instanceof MessageBag ? $errors - : new MessageBag; + : new MessageBag(); } /** @@ -23,7 +23,7 @@ public function errors(): MessageBag */ public function taggedErrors(string $tag): MessageBag { - return $this->errors()->{$tag} ?? new MessageBag; + return $this->errors()->{$tag} ?? new MessageBag(); } /** diff --git a/src/Icons/Error.php b/src/Icons/Error.php index 628bcf78..bdd5f296 100644 --- a/src/Icons/Error.php +++ b/src/Icons/Error.php @@ -15,8 +15,6 @@ class Error extends Component /** * Check if the alert icon is circled. - * - * @return bool */ public function isCircled(): bool { diff --git a/src/Icons/Info.php b/src/Icons/Info.php index 60068ae9..1a94c868 100644 --- a/src/Icons/Info.php +++ b/src/Icons/Info.php @@ -15,8 +15,6 @@ class Info extends Component /** * Check if the alert icon is circled. - * - * @return bool */ public function isCircled(): bool { diff --git a/src/Icons/Success.php b/src/Icons/Success.php index 77b44a52..f813ea33 100644 --- a/src/Icons/Success.php +++ b/src/Icons/Success.php @@ -15,8 +15,6 @@ class Success extends Component /** * Check if the alert icon is circled. - * - * @return bool */ public function isCircled(): bool { diff --git a/src/Icons/Warning.php b/src/Icons/Warning.php index 3bad66f4..2bff5c28 100644 --- a/src/Icons/Warning.php +++ b/src/Icons/Warning.php @@ -15,8 +15,6 @@ class Warning extends Component /** * Check if the alert icon is circled. - * - * @return bool */ public function isCircled(): bool { diff --git a/src/Themes/Tailwind/AbstractComponent.php b/src/Themes/Tailwind/AbstractComponent.php index 42343259..4a3dc9c4 100644 --- a/src/Themes/Tailwind/AbstractComponent.php +++ b/src/Themes/Tailwind/AbstractComponent.php @@ -6,5 +6,4 @@ abstract class AbstractComponent extends Component { - } diff --git a/src/Themes/Tailwind/Components/Field.php b/src/Themes/Tailwind/Components/Field.php index 9fa7c04d..a3b1e496 100644 --- a/src/Themes/Tailwind/Components/Field.php +++ b/src/Themes/Tailwind/Components/Field.php @@ -4,13 +4,13 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; +use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; use Exception; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; -use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; /** - * Class Field + * Class Field. */ class Field extends AbstractComponent implements LivewireInterface { @@ -20,12 +20,12 @@ class Field extends AbstractComponent implements LivewireInterface public ?string $for = null; /** - * The alert name + * The alert name. */ public ?string $name = null; /** - * The alerts + * The alerts. */ public array $alert = []; @@ -41,7 +41,7 @@ class Field extends AbstractComponent implements LivewireInterface */ public function mount(): void { - if (! empty($this->for)) { + if (!empty($this->for)) { $this->name = $this->for; } diff --git a/src/Themes/Tailwind/Components/Message.php b/src/Themes/Tailwind/Components/Message.php index d92c6884..c923c1bd 100644 --- a/src/Themes/Tailwind/Components/Message.php +++ b/src/Themes/Tailwind/Components/Message.php @@ -4,13 +4,13 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; +use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; use Exception; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; -use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; /** - * Class Message + * Class Message. */ class Message extends AbstractComponent implements LivewireInterface { @@ -20,7 +20,7 @@ class Message extends AbstractComponent implements LivewireInterface public string $tag = Alert::DEFAULT_TAG; /** - * The alerts + * The alerts. */ public array $alerts = []; @@ -40,6 +40,7 @@ public function resolve(string $tag, array $alerts = []): void ->values() ->map(function ($alert) { $alert->forget(); + return $alert->toArray(); })->toArray(); } diff --git a/src/Themes/Tailwind/Components/Modal.php b/src/Themes/Tailwind/Components/Modal.php index 03d0df98..477eeb45 100644 --- a/src/Themes/Tailwind/Components/Modal.php +++ b/src/Themes/Tailwind/Components/Modal.php @@ -4,13 +4,13 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; +use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; use Exception; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; -use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; /** - * Class Modal + * Class Modal. */ class Modal extends AbstractComponent implements LivewireInterface { @@ -20,7 +20,7 @@ class Modal extends AbstractComponent implements LivewireInterface public string $tag = Alert::DEFAULT_TAG; /** - * The alerts + * The alerts. */ public array $alerts = []; diff --git a/src/Themes/Tailwind/Components/Notify.php b/src/Themes/Tailwind/Components/Notify.php index 65beaa18..6ba0b7d8 100644 --- a/src/Themes/Tailwind/Components/Notify.php +++ b/src/Themes/Tailwind/Components/Notify.php @@ -4,13 +4,13 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; +use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; use Exception; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; -use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; /** - * Class Notify + * Class Notify. */ class Notify extends AbstractComponent implements LivewireInterface { @@ -20,7 +20,7 @@ class Notify extends AbstractComponent implements LivewireInterface public string $tag = Alert::DEFAULT_TAG; /** - * The alerts + * The alerts. */ public array $alerts = []; diff --git a/src/Themes/Tailwind/Components/Toastr.php b/src/Themes/Tailwind/Components/Toastr.php index dbb262c5..960e1973 100644 --- a/src/Themes/Tailwind/Components/Toastr.php +++ b/src/Themes/Tailwind/Components/Toastr.php @@ -4,13 +4,13 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; +use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; use Exception; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; -use Digitlimit\Alert\Themes\Tailwind\AbstractComponent; /** - * Class Toastr + * Class Toastr. */ class Toastr extends AbstractComponent implements LivewireInterface { @@ -20,7 +20,7 @@ class Toastr extends AbstractComponent implements LivewireInterface public string $tag = Alert::DEFAULT_TAG; /** - * The alerts + * The alerts. */ public array $alerts = []; @@ -40,6 +40,7 @@ public function resolve(string $tag, array $alerts = []): void ->values() ->map(function ($alert) { $alert->forget(); + return $alert->toArray(); })->toArray(); } diff --git a/src/Themes/Tailwind/Components/ToastrClassic.php b/src/Themes/Tailwind/Components/ToastrClassic.php new file mode 100644 index 00000000..7546f714 --- /dev/null +++ b/src/Themes/Tailwind/Components/ToastrClassic.php @@ -0,0 +1,76 @@ +alerts = $alerts + ->filter(function ($alert) { + return $alert->getTag() === $this->tag; + }) + ->values() + ->map(function ($alert) { + $alert->forget(); + + return $alert->toArray(); + })->toArray(); + } + + /** + * Create a new component instance. + * + * @throws Exception + */ + public function mount(): void + { + $this->resolve($this->tag); + } + + #[On('refresh-alert-toastr')] + public function refresh(string $tag, array $alerts): void + { + if ($tag !== $this->tag) { + return; + } + + $this->resolve($tag, array_values($alerts)); + $this->dispatch('open-alert-toastr'); + } + + /** + * Get the view / contents that represent the component. + */ + public function render(): View + { + return view('alert::themes.tailwind.components.toastr'); + } +} diff --git a/src/Themes/Tailwind/Tailwind.php b/src/Themes/Tailwind/Tailwind.php index bd68953a..390e517b 100644 --- a/src/Themes/Tailwind/Tailwind.php +++ b/src/Themes/Tailwind/Tailwind.php @@ -4,12 +4,9 @@ use Digitlimit\Alert\Alert; use Digitlimit\Alert\Contracts\LivewireInterface; -use Digitlimit\Alert\Contracts\Taggable; use Digitlimit\Alert\Contracts\ThemeInterface; use Digitlimit\Alert\Foundation\AbstractTheme; -use Digitlimit\Alert\Foundation\AlertInterface; use Digitlimit\Alert\Helpers\SessionKey; -use Illuminate\Support\Collection; use Livewire\Component; use Livewire\Livewire; @@ -19,7 +16,7 @@ class Tailwind extends AbstractTheme implements ThemeInterface { /** - * Register the alert types + * Register the alert types. */ public static function types(): array { @@ -27,7 +24,7 @@ public static function types(): array } /** - * Register the alert components + * Register the alert components. */ public static function registerComponents(): void { @@ -39,12 +36,12 @@ public static function registerComponents(): void } /** - * Dehydrate the alert to the component + * Dehydrate the alert to the component. */ public static function dehydrate(): void { on('dehydrate', function (Component $component) { - if (! Livewire::isLivewireRequest()) { + if (!Livewire::isLivewireRequest()) { return false; } @@ -63,9 +60,9 @@ public static function dehydrate(): void $all = Alert::all(); foreach ($all as $type => $alerts) { - $event = 'refresh-alert-' . $type; + $event = 'refresh-alert-'.$type; - foreach($alerts as $tag => $tagAlerts) { + foreach ($alerts as $tag => $tagAlerts) { if (empty($tagAlerts)) { continue; } diff --git a/src/Themes/Tailwind/TailwindServiceProvider.php b/src/Themes/Tailwind/TailwindServiceProvider.php index 6c3bca14..53d8bfc3 100644 --- a/src/Themes/Tailwind/TailwindServiceProvider.php +++ b/src/Themes/Tailwind/TailwindServiceProvider.php @@ -53,22 +53,22 @@ protected function bootForConsole(): void { // Define publishable SCSS assets $this->publishes([ - __DIR__ . '/../../../resources/scss/themes/tailwind' => resource_path('scss/alert'), + __DIR__.'/../../../resources/scss/themes/tailwind' => resource_path('scss/alert'), ], 'alert-scss'); // Define publishable compiled CSS $this->publishes([ - __DIR__ . '/../../../resources/css/themes/tailwind/alert.css' => public_path('vendor/alert/alert.css'), + __DIR__.'/../../../resources/css/themes/tailwind/alert.css' => public_path('vendor/alert/alert.css'), ], 'alert-css'); } /** - * Register directives + * Register directives. */ public function registerDirectives(): void { Blade::directive('alertStyles', function () { - return ''; + return ''; }); } } diff --git a/src/Themes/Tailwind/Utils/Css.php b/src/Themes/Tailwind/Utils/Css.php index fef9efb6..f278240b 100644 --- a/src/Themes/Tailwind/Utils/Css.php +++ b/src/Themes/Tailwind/Utils/Css.php @@ -5,17 +5,17 @@ use Illuminate\Support\Facades\Session; /** - * Class Css + * Class Css. */ class Css { /** - * The classes + * The classes. */ protected array $classes; /** - * The cache key + * The cache key. */ protected string $cacheKey = 'digitlimit_alert_unique_tailwind_classes'; @@ -25,7 +25,7 @@ public function __construct(array $classes = []) } /** - * Set the classes + * Set the classes. */ public function setClasses(array $classes): self { @@ -35,7 +35,7 @@ public function setClasses(array $classes): self } /** - * Flush the cache + * Flush the cache. */ public function forgetCache(): void { @@ -43,7 +43,7 @@ public function forgetCache(): void } /** - * Retrieve unique Tailwind classes with caching + * Retrieve unique Tailwind classes with caching. */ public function uniqueClasses(): array { @@ -58,7 +58,7 @@ public function uniqueClasses(): array } /** - * Recursively extract unique Tailwind classes from the provided array + * Recursively extract unique Tailwind classes from the provided array. */ protected function extractUniqueClasses(array $array): array { @@ -74,7 +74,7 @@ protected function extractUniqueClasses(array $array): array } /** - * Convert the classes to a string + * Convert the classes to a string. */ public function toString(): string { @@ -82,7 +82,7 @@ public function toString(): string } /** - * Convert the classes to a string + * Convert the classes to a string. */ public function __toString(): string { diff --git a/src/Traits/Positionable.php b/src/Traits/Positionable.php index 4870df04..fec9eb9d 100644 --- a/src/Traits/Positionable.php +++ b/src/Traits/Positionable.php @@ -95,7 +95,7 @@ public function position(string $position): self $method = Str::camel($position); // check if function exists - if (! method_exists($this, $method)) { + if (!method_exists($this, $method)) { throw new Exception("Position method {$method} does not exist."); } diff --git a/src/Traits/Sizable.php b/src/Traits/Sizable.php index 7801d611..8eb21faf 100644 --- a/src/Traits/Sizable.php +++ b/src/Traits/Sizable.php @@ -11,7 +11,7 @@ trait Sizable { /** - * The size of the alert + * The size of the alert. */ protected string $size = 'medium'; @@ -66,7 +66,7 @@ public function fullscreen(): self } /** - * Set modal size + * Set modal size. * * @throws Exception */ @@ -75,7 +75,7 @@ public function size(string $size): self $method = Str::camel($size); // check if function exists - if (! method_exists($this, $method)) { + if (!method_exists($this, $method)) { throw new Exception("Size {$method} is not supported."); } @@ -83,7 +83,7 @@ public function size(string $size): self } /** - * Get the size of the alert + * Get the size of the alert. */ public function getSize(): string { diff --git a/src/Traits/WithButton.php b/src/Traits/WithButton.php index 281e2552..a337bd50 100644 --- a/src/Traits/WithButton.php +++ b/src/Traits/WithButton.php @@ -18,7 +18,7 @@ trait WithButton public function actionButton(): ?Button { return $this->getButtons() - ->filter(fn($button) => $button->getName() === 'action') + ->filter(fn ($button) => $button->getName() === 'action') ->first(); } @@ -28,7 +28,7 @@ public function actionButton(): ?Button public function cancelButton(): ?Button { return $this->getButtons() - ->filter(fn($button) => $button->getName() === 'cancel') + ->filter(fn ($button) => $button->getName() === 'cancel') ->first(); } @@ -38,7 +38,7 @@ public function cancelButton(): ?Button public function customButtons(): Collection { return $this->getButtons() - ->filter(fn($button) => !in_array($button->getName(), ['action', 'cancel'])); + ->filter(fn ($button) => !in_array($button->getName(), ['action', 'cancel'])); } /** @@ -77,7 +77,7 @@ public function button( $button = new Button($name, $label, $link, $attributes); if (empty($this->buttons)) { - $this->buttons = new Collection; + $this->buttons = new Collection(); } $this->buttons->push($button); @@ -108,7 +108,7 @@ public function buttons(array $buttons): self public function getButtons(): Collection { if (empty($this->buttons)) { - return new Collection; + return new Collection(); } return $this->buttons; diff --git a/src/Traits/WithTitle.php b/src/Traits/WithTitle.php index 028c16a9..138077d9 100644 --- a/src/Traits/WithTitle.php +++ b/src/Traits/WithTitle.php @@ -29,6 +29,6 @@ public function getTitle(): ?string public function hasTitle(): bool { - return ! is_null($this->title); + return !is_null($this->title); } } diff --git a/src/Traits/WithView.php b/src/Traits/WithView.php index 3e4e126c..c53622b6 100644 --- a/src/Traits/WithView.php +++ b/src/Traits/WithView.php @@ -62,6 +62,6 @@ public function getView(): ?string */ public function hasView(): bool { - return ! is_null($this->view); + return !is_null($this->view); } } diff --git a/src/Types/Field.php b/src/Types/Field.php index eacd1935..27d83e37 100644 --- a/src/Types/Field.php +++ b/src/Types/Field.php @@ -17,7 +17,7 @@ /** * Field alert class. */ -class Field extends AbstractAlert implements AlertInterface, HasMessage, HasName, Levelable, Taggable, HasTimeout +class Field extends AbstractAlert implements AlertInterface, HasMessage, HasName, HasTimeout, Levelable, Taggable { use Traits\Levelable; use Traits\Taggable; @@ -75,13 +75,13 @@ public function getTag(): string public function toArray(): array { return array_merge(parent::toArray(), [ - 'type' => $this->key(), - 'name' => $this->getName(), - 'tag' => $this->getTag(), + 'type' => $this->key(), + 'name' => $this->getName(), + 'tag' => $this->getTag(), 'named_tag' => $this->getNamedTag(), - 'level' => $this->getLevel(), - 'message' => $this->getMessage(), - 'timeout' => $this->getTimeout(), + 'level' => $this->getLevel(), + 'message' => $this->getMessage(), + 'timeout' => $this->getTimeout(), ]); } @@ -104,7 +104,7 @@ public static function fill(array $alert): AlertInterface */ public function getNamedTag(): string { - return $this->tag . '.' . $this->getName(); + return $this->tag.'.'.$this->getName(); } /** diff --git a/src/Types/Message.php b/src/Types/Message.php index 2f1e8647..943c207f 100644 --- a/src/Types/Message.php +++ b/src/Types/Message.php @@ -5,8 +5,8 @@ use Digitlimit\Alert\Contracts\Closable; use Digitlimit\Alert\Contracts\HasMessage; use Digitlimit\Alert\Contracts\HasSticky; -use Digitlimit\Alert\Contracts\HasTitle; use Digitlimit\Alert\Contracts\HasTimeout; +use Digitlimit\Alert\Contracts\HasTitle; use Digitlimit\Alert\Contracts\Levelable; use Digitlimit\Alert\Contracts\Taggable; use Digitlimit\Alert\Events\Message\Flashed; @@ -24,8 +24,8 @@ class Message extends AbstractAlert implements AlertInterface, Closable, HasMess use Traits\Taggable; use Traits\WithMessage; use Traits\WithSticky; - use Traits\WithTitle; use Traits\WithTimeout; + use Traits\WithTitle; /** * The default level of the alert. @@ -70,14 +70,14 @@ public function getLevel(): string public function toArray(): array { return array_merge(parent::toArray(), [ - 'type' => $this->key(), - 'tag' => $this->getTag(), - 'level' => $this->getLevel(), - 'message' => $this->getMessage(), - 'title' => $this->getTitle(), - 'timeout' => $this->getTimeout(), + 'type' => $this->key(), + 'tag' => $this->getTag(), + 'level' => $this->getLevel(), + 'message' => $this->getMessage(), + 'title' => $this->getTitle(), + 'timeout' => $this->getTimeout(), 'closable' => $this->isClosable(), - 'sticky' => $this->isSticky(), + 'sticky' => $this->isSticky(), ]); } diff --git a/src/Types/Modal.php b/src/Types/Modal.php index 5368b6a7..a3db9663 100644 --- a/src/Types/Modal.php +++ b/src/Types/Modal.php @@ -33,8 +33,8 @@ class Modal extends AbstractAlert implements AlertInterface, Closable, HasButton use Traits\WithButton; use Traits\WithCancelButton; use Traits\WithMessage; - use Traits\WithTitle; use Traits\WithTimeout; + use Traits\WithTitle; use Traits\WithView; /** @@ -62,17 +62,17 @@ public function key(): string public function toArray(): array { return array_merge(parent::toArray(), [ - 'type' => $this->key(), - 'level' => $this->getLevel(), - 'title' => $this->getTitle(), - 'message' => $this->getMessage(), - 'tag' => $this->getTag(), - 'size' => $this->getSize(), - 'timeout' => $this->getTimeout(), + 'type' => $this->key(), + 'level' => $this->getLevel(), + 'title' => $this->getTitle(), + 'message' => $this->getMessage(), + 'tag' => $this->getTag(), + 'size' => $this->getSize(), + 'timeout' => $this->getTimeout(), 'scrollable' => $this->isScrollable(), - 'closable' => $this->isClosable(), - 'view' => $this->getView(), - 'buttons' => $this->buttonsToArray(), + 'closable' => $this->isClosable(), + 'view' => $this->getView(), + 'buttons' => $this->buttonsToArray(), ]); } diff --git a/src/Types/Notify.php b/src/Types/Notify.php index 079e5725..8f7d494c 100644 --- a/src/Types/Notify.php +++ b/src/Types/Notify.php @@ -27,12 +27,12 @@ class Notify extends AbstractAlert implements AlertInterface, Closable, HasButto use Traits\Levelable; use Traits\Positionable; use Traits\Taggable; - use Traits\WithMessage; - use Traits\WithTimeout; - use Traits\WithTitle; use Traits\WithActionButton; use Traits\WithButton; use Traits\WithCancelButton; + use Traits\WithMessage; + use Traits\WithTimeout; + use Traits\WithTitle; protected string $defaultLevel = 'info'; @@ -73,16 +73,16 @@ public function key(): string public function toArray(): array { return array_merge(parent::toArray(), [ - 'type' => $this->key(), - 'title' => $this->getTitle(), - 'timeout' => $this->getTimeout(), - 'message' => $this->getMessage(), - 'tag' => $this->getTag(), - 'id_tag' => $this->getIdTag(), - 'level' => $this->getLevel(), + 'type' => $this->key(), + 'title' => $this->getTitle(), + 'timeout' => $this->getTimeout(), + 'message' => $this->getMessage(), + 'tag' => $this->getTag(), + 'id_tag' => $this->getIdTag(), + 'level' => $this->getLevel(), 'position' => $this->getPosition(), 'closable' => $this->isClosable(), - 'buttons' => $this->buttonsToArray(), + 'buttons' => $this->buttonsToArray(), ]); } @@ -116,8 +116,8 @@ public static function fill(array $alert): AlertInterface public function getIdTag(): string { return $this->tag - . '.' - . $this->getId(); + .'.' + .$this->getId(); } /** diff --git a/src/Types/Toastr.php b/src/Types/Toastr.php index 2b0de966..608c125a 100644 --- a/src/Types/Toastr.php +++ b/src/Types/Toastr.php @@ -67,12 +67,12 @@ public function key(): string public function toArray(): array { return array_merge(parent::toArray(), [ - 'type' => $this->key(), - 'title' => $this->getTitle(), - 'timeout' => $this->getTimeout(), - 'message' => $this->getMessage(), - 'tag' => $this->getTag(), - 'level' => $this->getLevel(), + 'type' => $this->key(), + 'title' => $this->getTitle(), + 'timeout' => $this->getTimeout(), + 'message' => $this->getMessage(), + 'tag' => $this->getTag(), + 'level' => $this->getLevel(), 'position' => $this->getPosition(), 'closable' => $this->isClosable(), ]); diff --git a/src/Types/ToastrClassic.php b/src/Types/ToastrClassic.php new file mode 100644 index 00000000..5874b8d2 --- /dev/null +++ b/src/Types/ToastrClassic.php @@ -0,0 +1,113 @@ +level)) { + return $this->defaultLevel; + } + + return $this->level; + } + + /** + * Message store key for the toastr alert. + */ + public function key(): string + { + return 'toastr'; + } + + /** + * Convert the toastr alert to an array. + */ + public function toArray(): array + { + return array_merge(parent::toArray(), [ + 'type' => $this->key(), + 'title' => $this->getTitle(), + 'timeout' => $this->getTimeout(), + 'message' => $this->getMessage(), + 'tag' => $this->getTag(), + 'level' => $this->getLevel(), + 'position' => $this->getPosition(), + 'closable' => $this->isClosable(), + ]); + } + + /** + * Fill the notification alert from an array. + * + * @throws Exception + */ + public static function fill(array $alert): AlertInterface + { + $toastr = new static($alert['message']); + $toastr->id($alert['id']); + + if ($alert['title']) { + $toastr->title($alert['title']); + } + + $toastr->tag($alert['tag']); + $toastr->level($alert['level']); + $toastr->position($alert['position']); + $toastr->timeout($alert['timeout']); + $toastr->closable($alert['closable']); + + return $toastr; + } + + /** + * Flash field instance to store. + */ + public function flash(): void + { + parent::flash(); + + Flashed::dispatch($this); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 4fb91501..a624d8a0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,8 +6,8 @@ use Illuminate\Foundation\Application; use Illuminate\Foundation\Testing\Concerns\InteractsWithViews; use Illuminate\Support\Facades\Facade; -use Orchestra\Testbench\TestCase as BaseTestCase; use Livewire; +use Orchestra\Testbench\TestCase as BaseTestCase; class TestCase extends BaseTestCase { @@ -24,7 +24,8 @@ protected function getPackageProviders($app): array /** * Override application aliases. * - * @param Application $app + * @param Application $app + * * @return array> */ protected function getPackageAliases($app): array diff --git a/tests/Unit/Component/ButtonTest.php b/tests/Unit/Component/ButtonTest.php index 56b0a960..c7f722d8 100644 --- a/tests/Unit/Component/ButtonTest.php +++ b/tests/Unit/Component/ButtonTest.php @@ -41,10 +41,10 @@ $button = new Button('submit', 'Submit Form', '/submit', ['class' => 'btn-submit']); $expected = [ - 'id' => $button->getId(), - 'name' => 'submit', - 'label' => 'Submit Form', - 'link' => '/submit', + 'id' => $button->getId(), + 'name' => 'submit', + 'label' => 'Submit Form', + 'link' => '/submit', 'attributes' => ['class' => 'btn-submit'], ]; @@ -53,9 +53,9 @@ it('can fill a button from an array', function () { $data = [ - 'name' => 'delete', - 'label' => 'Delete Item', - 'link' => '/delete', + 'name' => 'delete', + 'label' => 'Delete Item', + 'link' => '/delete', 'attributes' => ['id' => 'btn-123', 'class' => 'btn-danger'], ]; diff --git a/tests/Unit/Factory/AlertFactoryTest.php b/tests/Unit/Factory/AlertFactoryTest.php new file mode 100644 index 00000000..b5e13fc9 --- /dev/null +++ b/tests/Unit/Factory/AlertFactoryTest.php @@ -0,0 +1,27 @@ +toBeInstanceOf(AlertInterface::class) + ->and($alert->getMessage()) + ->toBe('Hello World'); +})->group('unit', 'factory', 'alert-factory'); + +it('creates an alert using makeFromArray()', function () { + $alert = AlertFactory::makeFromArray([ + 'id' => 1, + 'title' => 'Test Alert', + 'level' => 'info', + 'tag' => 'default', + 'type' => 'message', + 'message' => 'Hello World', + ]); + + expect($alert)->toBeInstanceOf(AlertInterface::class) + ->and($alert->getMessage()) + ->toBe('Hello World'); +})->group('unit', 'factory', 'alert-factory'); diff --git a/tests/Unit/Foundation/AbstractAlertTest.php b/tests/Unit/Foundation/AbstractAlertTest.php new file mode 100644 index 00000000..047794ad --- /dev/null +++ b/tests/Unit/Foundation/AbstractAlertTest.php @@ -0,0 +1,190 @@ +id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? 'Default message'; + + return $instance; + } + }; + + expect($alert->getId())->not()->toBeEmpty() + ->and($alert->getIdTag())->toContain('test_alert'); +})->group('alert', 'abstract-alert', 'auto-set-id'); + +it('can manually set ID', function () { + $alert = new class() extends AbstractAlert { + public string $message = 'Test Alert'; + + public function key(): string + { + return 'manual_alert'; + } + + public function getTag(): string + { + return 'alert'; + } + + public static function fill(array $alert): AlertInterface + { + $instance = new self(); + $instance->id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? 'Default message'; + + return $instance; + } + }; + + $alert->id('custom-id'); + + expect($alert->getId())->toBe('custom-id') + ->and($alert->getIdTag())->toBe('alert.custom-id'); +})->group('alert', 'abstract-alert', 'manual-set-id'); + +it('flashes alert to session if id and message are set', function () { + $alert = new class() extends AbstractAlert { + public string $message = 'Hello session'; + + public function key(): string + { + return 'session_alert'; + } + + public function getTag(): string + { + return 'alert'; + } + + public static function fill(array $alert): AlertInterface + { + $instance = new self(); + $instance->id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? 'Default message'; + + return $instance; + } + }; + + $alert->id('flash-id'); + $sessionKey = SessionKey::key('session_alert', 'alert.flash-id'); + + expect(Session::get($sessionKey))->toBe($alert); +})->group('alert', 'abstract-alert', 'flash-session'); + +it('does not flash if message is empty', function () { + $alert = new class() extends AbstractAlert { + public function key(): string + { + return 'session_alert'; + } + + public function getTag(): string + { + return 'alert'; + } + + public static function fill(array $alert): AlertInterface + { + $instance = new self(); + $instance->id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? ''; + + return $instance; + } + }; + + $alert->id('no-message-id'); + $sessionKey = SessionKey::key('session_alert', 'alert.no-message-id'); + + expect(Session::has($sessionKey))->toBeFalse(); +})->group('alert', 'abstract-alert', 'no-flash-empty-message'); + +it('can forget session key', function () { + $alert = new class() extends AbstractAlert { + public string $message = 'Forget me'; + + public function key(): string + { + return 'forget_alert'; + } + + public function getTag(): string + { + return 'alert'; + } + + public static function fill(array $alert): AlertInterface + { + $instance = new self(); + $instance->id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? 'Default message'; + + return $instance; + } + }; + + $alert->id('forget-id'); + $sessionKey = SessionKey::key('forget_alert', 'alert.forget-id'); + + expect(Session::get($sessionKey))->toBe($alert); + + $alert->forget(); + + expect(Session::has($sessionKey))->toBeFalse(); +})->group('alert', 'abstract-alert', 'forget-session'); + +it('can convert to array and json', function () { + $alert = new class() extends AbstractAlert { + public string $message = 'Format test'; + + public function key(): string + { + return 'json_alert'; + } + + public function getTag(): string + { + return 'alert'; + } + + public static function fill(array $alert): AlertInterface + { + $instance = new self(); + $instance->id($alert['id'] ?? 'default-id'); + $instance->message = $alert['message'] ?? 'Default message'; + + return $instance; + } + }; + + $id = $alert->getId(); + + expect($alert->toArray())->toBe(['id' => $id]) + ->and($alert->toJson())->toBe(json_encode(['id' => $id])); +})->group('alert', 'abstract-alert', 'to-array-json'); diff --git a/tests/Unit/Helpers/AttributeTest.php b/tests/Unit/Helpers/AttributeTest.php index da5fd972..925c2ed9 100644 --- a/tests/Unit/Helpers/AttributeTest.php +++ b/tests/Unit/Helpers/AttributeTest.php @@ -3,9 +3,8 @@ use Digitlimit\Alert\Helpers\Attribute; it('can generate html element attribute from an array', function () { - $attributesString = Attribute::toString([ - 'id' => 'alert-id', + 'id' => 'alert-id', 'class' => 'hover:underline to-pink-500', ]); diff --git a/tests/Unit/Helpers/ThemeTest.php b/tests/Unit/Helpers/ThemeTest.php index 5b68f209..a0e8c741 100644 --- a/tests/Unit/Helpers/ThemeTest.php +++ b/tests/Unit/Helpers/ThemeTest.php @@ -10,7 +10,6 @@ expect($theme) ->toEqual('tailwind') ->and(Theme::name())->toEqual('tailwind'); - })->group('theme', 'helpers', 'theme-name'); it('can get all themes', function () { @@ -21,7 +20,6 @@ ->not()->toBeEmpty() ->toBeArray() ->toEqual(Theme::all()); - })->group('theme', 'helpers', 'theme-all'); it('can throw exception if theme is not found', function () { @@ -43,5 +41,4 @@ ->toEqual('tailwind') ->and(Theme::theme()) ->toBeInstanceOf(ThemeInterface::class); - })->group('theme', 'helpers', 'theme-instance'); diff --git a/tests/Unit/Themes/Tailwind/Components/FieldTest.php b/tests/Unit/Themes/Tailwind/Components/FieldTest.php index 49ecce07..7651b63d 100644 --- a/tests/Unit/Themes/Tailwind/Components/FieldTest.php +++ b/tests/Unit/Themes/Tailwind/Components/FieldTest.php @@ -7,4 +7,4 @@ Livewire::test(Field::class, ['for' => 'test-field']) ->assertSet('name', 'test-field') ->assertSet('alert', []); -})->group('tailwind', 'components', 'field'); \ No newline at end of file +})->group('tailwind', 'components', 'field'); diff --git a/tests/Unit/Types/FieldTest.php b/tests/Unit/Types/FieldTest.php index c012544a..f7740ab1 100644 --- a/tests/Unit/Types/FieldTest.php +++ b/tests/Unit/Types/FieldTest.php @@ -1,10 +1,10 @@ toArray(); expect($array)->toMatchArray([ - 'id' => $alert->getId(), - 'type' => 'field', - 'name' => 'email', - 'tag' => 'user', + 'id' => $alert->getId(), + 'type' => 'field', + 'name' => 'email', + 'tag' => 'user', 'named_tag' => 'user.email', - 'level' => 'warning', - 'message' => 'Invalid email', - 'timeout' => 0, + 'level' => 'warning', + 'message' => 'Invalid email', + 'timeout' => 0, ]); })->group('field', 'toArray'); it('can fill a field alert from array', function () { $data = [ - 'id' => 'alert-id', - 'name' => 'password', + 'id' => 'alert-id', + 'name' => 'password', 'message' => 'Password must be at least 6 characters', - 'tag' => 'auth', - 'level' => 'info', + 'tag' => 'auth', + 'level' => 'info', ]; $alert = Field::fill($data); @@ -63,7 +63,7 @@ $alert = new Field('username', 'Username is required'); $alert->tag('register'); - $sessionKey = Alert::MAIN_KEY . '.field.register.username'; + $sessionKey = Alert::MAIN_KEY.'.field.register.username'; $alert->flash(); diff --git a/tests/Unit/Types/MessageTest.php b/tests/Unit/Types/MessageTest.php index c3ef2059..ff27b34d 100644 --- a/tests/Unit/Types/MessageTest.php +++ b/tests/Unit/Types/MessageTest.php @@ -1,10 +1,10 @@ toArray(); expect($array)->toMatchArray([ - 'id' => $alert->getId(), - 'type' => 'message', - 'tag' => 'updates', - 'level' => 'success', - 'message' => 'New update available', - 'title' => 'Update', - 'timeout' => 10, + 'id' => $alert->getId(), + 'type' => 'message', + 'tag' => 'updates', + 'level' => 'success', + 'message' => 'New update available', + 'title' => 'Update', + 'timeout' => 10, 'closable' => true, ]); })->group('message', 'toArray'); it('can fill a message alert from array', function () { $data = [ - 'id' => 'alert-msg-1', - 'tag' => 'dashboard', - 'level' => 'warning', - 'message' => 'Please verify your email', - 'title' => 'Verification Needed', + 'id' => 'alert-msg-1', + 'tag' => 'dashboard', + 'level' => 'warning', + 'message' => 'Please verify your email', + 'title' => 'Verification Needed', 'closable' => true, ]; @@ -63,7 +63,6 @@ ->and($alert->getMessage())->toBe('Please verify your email') ->and($alert->getTitle())->toBe('Verification Needed') ->and($alert->isClosable())->toBeTrue(); - })->group('message', 'fill'); it('can flash a normal message to session and dispatch event', function () { @@ -73,7 +72,7 @@ $alert->tag('intro'); $alert->flash(); - $sessionKey = Alert::MAIN_KEY . '.message.default.' . $alert->getId(); + $sessionKey = Alert::MAIN_KEY.'.message.default.'.$alert->getId(); expect(Session::get($sessionKey))->toBeInstanceOf(Message::class) ->and(Session::get($sessionKey)->getMessage())->toBe('Welcome to the app'); diff --git a/tests/Unit/Types/ModalTest.php b/tests/Unit/Types/ModalTest.php index 110c589b..98a54853 100644 --- a/tests/Unit/Types/ModalTest.php +++ b/tests/Unit/Types/ModalTest.php @@ -1,10 +1,10 @@ setView('modals.confirm') ->buttons([ ['name' => 'cancel', 'label' => 'Cancel'], - ['name' => 'confirm', 'label' => 'Yes, Confirm'] + ['name' => 'confirm', 'label' => 'Yes, Confirm'], ]); $array = $alert->toArray(); expect($array)->toMatchArray([ - "id" => $alert->getId(), - "type" => "modal", - "level" => "info", - "title" => "Confirm Action", - "message" => "Modal Message", - "tag" => "confirm", - "size" => "medium", - "timeout" => 30, - "scrollable" => true, - "closable" => true, - "view" => "modals.confirm", - "buttons" => [ + 'id' => $alert->getId(), + 'type' => 'modal', + 'level' => 'info', + 'title' => 'Confirm Action', + 'message' => 'Modal Message', + 'tag' => 'confirm', + 'size' => 'medium', + 'timeout' => 30, + 'scrollable' => true, + 'closable' => true, + 'view' => 'modals.confirm', + 'buttons' => [ [ - "id" => $alert->cancelButton()->getId(), - "name" => "cancel", - "label" => "Cancel", - "link" => null, - "attributes" => [], + 'id' => $alert->cancelButton()->getId(), + 'name' => 'cancel', + 'label' => 'Cancel', + 'link' => null, + 'attributes' => [], ], [ - "id" => $alert->customButtons()->first()->getId(), - "name" => "confirm", - "label" => "Yes, Confirm", - "link" => null, - "attributes" => [] - ] - ] + 'id' => $alert->customButtons()->first()->getId(), + 'name' => 'confirm', + 'label' => 'Yes, Confirm', + 'link' => null, + 'attributes' => [], + ], + ], ]); })->group('modal', 'toArray'); it('can fill a modal alert from an array', function () { $data = [ - 'id' => 'modal-123', - 'message' => 'Do you want to continue?', - 'tag' => 'dialog', - 'level' => 'warning', - 'timeout' => 15, - 'title' => 'Continue?', + 'id' => 'modal-123', + 'message' => 'Do you want to continue?', + 'tag' => 'dialog', + 'level' => 'warning', + 'timeout' => 15, + 'title' => 'Continue?', 'scrollable' => true, - 'closable' => true, - 'view' => 'modals.continue', - 'buttons' => [ + 'closable' => true, + 'view' => 'modals.continue', + 'buttons' => [ ['name' => 'no', 'label' => 'No'], - ['name' => 'yes', 'label' => 'Yes'] + ['name' => 'yes', 'label' => 'Yes'], ], ]; @@ -112,7 +112,7 @@ $alert->flash(); - $sessionKey = SessionKey::key('modal', 'example.' . $alert->getId()); + $sessionKey = SessionKey::key('modal', 'example.'.$alert->getId()); expect(Session::get($sessionKey))->toBeInstanceOf(Modal::class) ->and(Session::get($sessionKey)->getMessage())->toBe('Flashing modal'); diff --git a/tests/Unit/Types/NotifyTest.php b/tests/Unit/Types/NotifyTest.php index c35f724f..7e02a292 100644 --- a/tests/Unit/Types/NotifyTest.php +++ b/tests/Unit/Types/NotifyTest.php @@ -1,8 +1,6 @@ toArray(); expect($array)->toHaveKeys([ - 'type', 'title', 'timeout', 'message', 'tag', 'id_tag', 'level', 'position', 'closable', 'buttons' + 'type', 'title', 'timeout', 'message', 'tag', 'id_tag', 'level', 'position', 'closable', 'buttons', ])->and($array['message'])->toBe('Test message') ->and($array['title'])->toBe('Test Title') ->and($array['tag'])->toBe('test-tag'); @@ -31,15 +29,15 @@ it('fills from array correctly', function () { $alertData = [ - 'id' => '123', - 'message' => 'Filled message', - 'title' => 'Filled Title', - 'timeout' => 5000, - 'tag' => 'filled-tag', - 'level' => 'warning', + 'id' => '123', + 'message' => 'Filled message', + 'title' => 'Filled Title', + 'timeout' => 5000, + 'tag' => 'filled-tag', + 'level' => 'warning', 'position' => 'top-right', 'closable' => true, - 'buttons' => [], + 'buttons' => [], ]; $notify = Notify::fill($alertData); @@ -52,4 +50,3 @@ ->and($notify->getPosition())->toBe('top-right') ->and($notify->isClosable())->toBeTrue(); })->group('notify', 'fill'); - diff --git a/tests/Unit/Types/ToastrTest.php b/tests/Unit/Types/ToastrTest.php index f8e46469..725ba94d 100644 --- a/tests/Unit/Types/ToastrTest.php +++ b/tests/Unit/Types/ToastrTest.php @@ -1,7 +1,7 @@ toArray(); expect($array)->toMatchArray([ - 'type' => 'toastr', - 'title' => 'Test Title', - 'timeout' => 3000, - 'message' => 'Test message', - 'tag' => 'test-tag', - 'level' => 'success', + 'type' => 'toastr', + 'title' => 'Test Title', + 'timeout' => 3000, + 'message' => 'Test message', + 'tag' => 'test-tag', + 'level' => 'success', 'position' => 'top-right', 'closable' => true, ]); @@ -39,12 +39,12 @@ it('fills a toastr alert from an array', function () { $alertArray = [ - 'id' => '123', - 'message' => 'Filled message', - 'title' => 'Filled Title', - 'timeout' => 5000, - 'tag' => 'filled-tag', - 'level' => 'error', + 'id' => '123', + 'message' => 'Filled message', + 'title' => 'Filled Title', + 'timeout' => 5000, + 'tag' => 'filled-tag', + 'level' => 'error', 'position' => 'bottom-left', 'closable' => false, ]; diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php index 3d48fd79..af6cac87 100644 --- a/workbench/app/Models/User.php +++ b/workbench/app/Models/User.php @@ -6,11 +6,11 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; -use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { - use HasFactory, Notifiable; + use HasFactory; + use Notifiable; /** * The attributes that are mass assignable. @@ -40,6 +40,6 @@ class User extends Authenticatable */ protected $casts = [ 'email_verified_at' => 'datetime', - 'password' => 'hashed', + 'password' => 'hashed', ]; } diff --git a/workbench/database/factories/UserFactory.php b/workbench/database/factories/UserFactory.php index 636b6ea1..f812df7c 100644 --- a/workbench/database/factories/UserFactory.php +++ b/workbench/database/factories/UserFactory.php @@ -34,11 +34,11 @@ class UserFactory extends Factory public function definition(): array { return [ - 'name' => fake()->name(), - 'email' => fake()->unique()->safeEmail(), + 'name' => fake()->name(), + 'email' => fake()->unique()->safeEmail(), 'email_verified_at' => now(), - 'password' => static::$password ??= Hash::make('password'), - 'remember_token' => Str::random(10), + 'password' => static::$password ??= Hash::make('password'), + 'remember_token' => Str::random(10), ]; } diff --git a/workbench/database/seeders/DatabaseSeeder.php b/workbench/database/seeders/DatabaseSeeder.php index 57868a4e..5a18b689 100644 --- a/workbench/database/seeders/DatabaseSeeder.php +++ b/workbench/database/seeders/DatabaseSeeder.php @@ -2,9 +2,9 @@ namespace Database\Seeders; -use Illuminate\Database\Seeder; -// use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Database\Factories\UserFactory; +// use Illuminate\Database\Console\Seeds\WithoutModelEvents; +use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { @@ -16,7 +16,7 @@ public function run(): void // UserFactory::new()->times(10)->create(); UserFactory::new()->create([ - 'name' => 'Test User', + 'name' => 'Test User', 'email' => 'test@example.com', ]); }