Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions config/alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| Here you may register your custom alert types
|
*/

'types' => [
'alert-normal' => [
'alert' => \Digitlimit\Alert\Types\Normal::class,
Expand All @@ -19,22 +19,22 @@

'alert-field' => [
'alert' => \Digitlimit\Alert\Types\Field::class,
'component' => \Digitlimit\Alert\View\Components\Field::class
'component' => \Digitlimit\Alert\View\Components\Field::class,
],

'alert-modal' => [
'alert' => \Digitlimit\Alert\Types\Modal::class,
'component' => \Digitlimit\Alert\View\Components\Modal::class
'component' => \Digitlimit\Alert\View\Components\Modal::class,
],

'alert-notify' => [
'alert' => \Digitlimit\Alert\Types\Notify::class,
'component' => \Digitlimit\Alert\View\Components\Notify::class
'component' => \Digitlimit\Alert\View\Components\Notify::class,
],

'alert-sticky' => [
'alert' => \Digitlimit\Alert\Types\Sticky::class,
'component' => \Digitlimit\Alert\View\Components\Sticky::class
]
]
'component' => \Digitlimit\Alert\View\Components\Sticky::class,
],
],
];
80 changes: 41 additions & 39 deletions src/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Digitlimit\Alert;

use Digitlimit\Alert\Message\MessageInterface;
use Digitlimit\Alert\Message\MessageFactory;
use Digitlimit\Alert\Helpers\SessionKey;
use Digitlimit\Alert\Helpers\Type;
use Illuminate\Validation\Validator;
use Digitlimit\Alert\Message\MessageFactory;
use Digitlimit\Alert\Message\MessageInterface;
use Exception;
use Illuminate\Validation\Validator;

class Alert
{
Expand All @@ -18,128 +18,130 @@ class Alert

/**
* Create a new alert instance.
*
* @return void
*/
public function __construct(
protected SessionInterface $session
){}
) {
}

/**
* Fetch an alert based on the default tag
* Fetch an alert based on the default tag.
*/
public function default(string $type) : MessageInterface|null
public function default(string $type): MessageInterface|null
{
return self::tagged($type, self::DEFAULT_TAG);
}

/**
* Fetch an alert based on the type and named
* Fetch an alert based on the type and named.
*/
public function named(
string $type,
string $name,
string $tag = null
) : MessageInterface|null {

if(!Type::exists($type)) {
): MessageInterface|null {
if (!Type::exists($type)) {
throw new Exception("Invalid alert type '$type'. Check the alert config");
}

$tag = ($tag ?? self::DEFAULT_TAG) . '.' . $name;
$tag = ($tag ?? self::DEFAULT_TAG).'.'.$name;

return $this->session->get(
SessionKey::key($type, $tag)
);
}

/**
* Fetch an alert based on the tag name
* Fetch an alert based on the tag name.
*/
public function tagged(
string $type,
string $type,
string $tag
) : MessageInterface|null {

if(!Type::exists($type)) {
): MessageInterface|null {
if (!Type::exists($type)) {
throw new Exception("Invalid alert type '$type'. Check the alert config");
}

$tagged = $this->session->get(
SessionKey::key($type, $tag)
);

if(is_array($tagged)) {
if (is_array($tagged)) {
return null;
}

return $tagged;
}

/**
* Fetch the normal alert
* Fetch the normal alert.
*/
public function normal(string $message=null) : MessageInterface
public function normal(string $message = null): MessageInterface
{
return MessageFactory::make($this->session, 'normal', $message);
}

/**
* Fetch the field alert
* Fetch the field alert.
*/
public function field(
string $message=null,
?Validator $validator=null
) : MessageInterface {
string $message = null,
?Validator $validator = null
): MessageInterface {
return MessageFactory::make(
$this->session, 'field', $message, $validator
$this->session,
'field',
$message,
$validator
);
}

/**
* Fetch the modal alert
* Fetch the modal alert.
*/
public function modal(string $message=null) : MessageInterface
public function modal(string $message = null): MessageInterface
{
return MessageFactory::make($this->session, 'modal', $message);
}

/**
* Fetch the notify alert
* Fetch the notify alert.
*/
public function notify(string $message=null) : MessageInterface
public function notify(string $message = null): MessageInterface
{
return MessageFactory::make($this->session, 'notify', $message);
}

/**
* Fetch the sticky alert
* Fetch the sticky alert.
*/
public function sticky(string $message=null) : MessageInterface
public function sticky(string $message = null): MessageInterface
{
return MessageFactory::make($this->session, 'sticky', $message);
}

/**
* Fetch the default alert type, which is the normal alert
* Fetch the default alert type, which is the normal alert.
*/
public function message(string $message) : MessageInterface
public function message(string $message): MessageInterface
{
return $this->normal($message);
}

/**
* Fetch an alert from the given alert type
* Fetch an alert from the given alert type.
*/
public function from(
string $type,
string $message=null,
string $type,
string $message = null,
...$args
) : MessageInterface {

if(!Type::exists($type)) {
): MessageInterface {
if (!Type::exists($type)) {
throw new Exception("Invalid alert type '$type'. Check the alert config");
}

return MessageFactory::make($this->session, $type, $message, ...$args);
}
}
}
23 changes: 11 additions & 12 deletions src/AlertServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace Digitlimit\Alert;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Foundation\Application;
use Digitlimit\Alert\Helpers\Type;
use Illuminate\Support\ServiceProvider;

class AlertServiceProvider extends ServiceProvider
{
Expand All @@ -26,21 +25,22 @@ public function boot(): void
public function register(): void
{
$this->app->bind(SessionInterface::class, Session::class);
$this->app->bind(ConfigInterface::class, Config::class);
$this->app->bind(ConfigInterface::class, Config::class);

$this->app->singleton('alert', function ($app) {
return $app->make(Alert::class);
});

$this->mergeConfigFrom(
__DIR__.'/../config/alert.php', 'alert'
__DIR__.'/../config/alert.php',
'alert'
);
}

/**
* Get the services provided by the provider.
*/
public function provides() : array
public function provides(): array
{
return ['alert'];
}
Expand All @@ -54,23 +54,22 @@ protected function bootForConsole(): void
__DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit/alert'),
], 'alert.views');


$this->publishes([
__DIR__.'/../config/alert.php' => config_path('alert.php'),
], 'alert.config');
}

/**
* Register alert components
* Register alert components.
*/
protected function registerComponents() : void
protected function registerComponents(): void
{
Blade::componentNamespace('Digitlimit\\Alert\View\\Components', 'alert');

$types = config('alert.types');

foreach($types as $name => $type) {
Blade::component($name, $type['component']);
foreach ($types as $name => $type) {
Blade::component($name, $type['component']);
}
}
}
}
64 changes: 33 additions & 31 deletions src/Component/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@

class Button
{
/**
* Create a new button instance.
* @return void
*/
public function __construct(
public ?string $label = null,
public ?string $link = null,
public array $attributes = []
){}
/**
* Create a new button instance.
*
* @return void
*/
public function __construct(
public ?string $label = null,
public ?string $link = null,
public array $attributes = []
) {
}

/**
* Set the button label
*/
public function label(string $label) : void
{
$this->label = $label;
}
/**
* Set the button label.
*/
public function label(string $label): void
{
$this->label = $label;
}

/**
* Set the button link
*/
public function link(string $link) : void
{
$this->link = $link;
}
/**
* Set the button link.
*/
public function link(string $link): void
{
$this->link = $link;
}

/**
* Set the button attributes
*/
public function attributes(array $attributes) : void
{
$this->attributes = $attributes;
}
}
/**
* Set the button attributes.
*/
public function attributes(array $attributes): void
{
$this->attributes = $attributes;
}
}
6 changes: 3 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Config implements ConfigInterface
{
/**
* The config
* The config.
*/
private Repository $config;

Expand All @@ -17,9 +17,9 @@ public function __construct(Repository $config)
}

/**
* Fetch value from the config based on the given key
* Fetch value from the config based on the given key.
*/
public function get(string $key, mixed $default=null) : mixed
public function get(string $key, mixed $default = null): mixed
{
return $this->config->get($key, $default);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface ConfigInterface
{
/**
* Fetch value from the config based on the given key
* Fetch value from the config based on the given key.
*/
public function get(string $key, string $default=null) : mixed;
}
public function get(string $key, string $default = null): mixed;
}
Loading