Skip to content
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ Adding message to default container.
\Krucas\Notification\Facades\Notification::warning('Warning message');
```

### Add errors from laravel validation
Adding errors from the Laravel validator to the default container.
```php
$validator = // Your validation in a controller or custom validation object

\Krucas\Notification\Facades\Notification::addLaravelValidationErrors($validator);
```

### Containers

Containers allows you to set up different containers for different placeholders.
Expand Down
52 changes: 50 additions & 2 deletions src/Krucas/Notification/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ class Notification
*/
protected $types = [];

/**
* Default Laravel Validation error type available for new containers.
*
* @var string
*/
protected $laravelValidationErrorType;

/**
* Laravel Validation error types for defined containers
*
* @var string
*/
protected $laravelValidationErrorTypes;

/**
* Default format for each container type.
*
Expand Down Expand Up @@ -74,6 +88,7 @@ class Notification
* @param string $defaultContainer
* @param array $defaultTypes
* @param array $types
* @param string $laravelValidationErrorType
* @param string $defaultFormat
* @param array $format
* @param array $defaultFormats
Expand All @@ -83,6 +98,7 @@ public function __construct(
$defaultContainer,
$defaultTypes,
$types,
$laravelValidationErrorType,
$defaultFormat,
$format,
$defaultFormats,
Expand All @@ -91,6 +107,7 @@ public function __construct(
$this->defaultContainer = $defaultContainer;
$this->defaultTypes = $defaultTypes;
$this->types = $types;
$this->laravelValidationErrorType = $laravelValidationErrorType;
$this->defaultFormat = $defaultFormat;
$this->format = $format;
$this->defaultFormats = $defaultFormats;
Expand Down Expand Up @@ -136,6 +153,35 @@ public function getContainerTypes($container)
return $this->defaultTypes;
}

/**
* Set Laravel Validation error type for a container.
*
* @param string $container
* @param array $types
* @return \Krucas\Notification\Notification
*/
public function setContainerLaravelValidationErrorType($container, $type)
{
$this->laravelValidationErrorTypes[$container] = $type;

return $this;
}

/**
* Return Laravel Validation error type for a container.
*
* @param $container
* @return string
*/
public function getContainerLaravelValidationErrorType($container)
{
if (isset($this->laravelValidationErrorTypes[$container])) {
return $this->laravelValidationErrorTypes[$container];
}

return $this->laravelValidationErrorType;
}

/**
* Set format for a container.
*
Expand Down Expand Up @@ -199,17 +245,18 @@ public function getContainerFormats($container)
*
* @param string $container
* @param array $types
* @param string $laravelValidationErrorType
* @param null $defaultFormat
* @param array $formats
* @return \Krucas\Notification\Notification
*/
public function addContainer($container, $types = [], $defaultFormat = null, $formats = [])
public function addContainer($container, $types = [], $laravelValidationErrorType = null, $defaultFormat = null, $formats = [])
{
if (isset($this->containers[$container])) {
return $this;
}

$this->containers[$container] = new NotificationsBag($container, $types, $defaultFormat, $formats);
$this->containers[$container] = new NotificationsBag($container, $types, $laravelValidationErrorType, $defaultFormat, $formats);
$this->containers[$container]->setNotification($this);

return $this;
Expand Down Expand Up @@ -240,6 +287,7 @@ public function container($container = null, Closure $callback = null)
$this->addContainer(
$container,
$this->getContainerTypes($container),
$this->getContainerLaravelValidationErrorType($container),
$this->getContainerFormat($container),
$this->getContainerFormats($container)
);
Expand Down
1 change: 1 addition & 0 deletions src/Krucas/Notification/NotificationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function register()
$config->get('notification.default_container'),
$config->get('notification.default_types'),
$config->get('notification.types'),
$config->get('notification.laravel_validation_error_type'),
$config->get('notification.default_format'),
$config->get('notification.format'),
$config->get('notification.default_formats'),
Expand Down
57 changes: 55 additions & 2 deletions src/Krucas/Notification/NotificationsBag.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php namespace Krucas\Notification;

use Closure;
use Countable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Closure;
use Illuminate\Contracts\Validation\Validator;

class NotificationsBag implements Arrayable, Jsonable, Countable
{
Expand All @@ -21,6 +22,13 @@ class NotificationsBag implements Arrayable, Jsonable, Countable
*/
protected $types = array();

/*
* The Laravel Validation error type
*
* @var string
*/
protected $laravelValidationErrorType;

/**
* Array of matcher for extracting types.
*
Expand Down Expand Up @@ -73,13 +81,15 @@ class NotificationsBag implements Arrayable, Jsonable, Countable
*
* @param $container
* @param array $types
* @param null $laravelValidationErrorType
* @param null $defaultFormat
* @param array $formats
*/
public function __construct($container, $types = array(), $defaultFormat = null, $formats = array())
public function __construct($container, $types = array(), $laravelValidationErrorType = null, $defaultFormat = null, $formats = array())
{
$this->container = $container;
$this->addType($types);
$this->setLaravelValidationErrorType($laravelValidationErrorType);
$this->setDefaultFormat($defaultFormat);
$this->setFormats($formats);
$this->notifications = new Collection();
Expand Down Expand Up @@ -122,6 +132,29 @@ public function addType($type)
return $this;
}

/**
* Get the Laravel Validation error type
*
* @return \Krucas\Notification\NotificationsBag
*/
public function getLaravelValidationErrorType()
{
return $this->laravelValidationErrorType;
}

/**
* Set the Laravel Validation error type
*
* @param string $type The message type of the Laravel Validation errors
* @return \Krucas\Notification\NotificationsBag
*/
public function setLaravelValidationErrorType($type)
{
$this->laravelValidationErrorType = $type;

return $this;
}

/**
* Return available types of messages in container.
*
Expand Down Expand Up @@ -353,6 +386,26 @@ protected function addInstance(Message $message, $type, $flash = true, $format =
}
}

/**
* Add all the Laravel Validation erors to this collection.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @param string $type Can be used to overwrite the Laravel
* Validation error type
* @param boolean $flash
* @param string $format
*/
public function addLaravelValidationErrors(Validator $validator, $type = null, $flash = true, $format = null)
{
if ($type === null) {
$type = $this->laravelValidationErrorType;
}

foreach ($validator->errors()->all() as $message) {
$this->add($type, $message, $flash, $format);
}
}

/**
* Returns all messages for given type.
*
Expand Down
11 changes: 11 additions & 0 deletions src/config/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@
*/
'types' => array(),

/*
| Default type for laravel validation errors
|--------------------------------------------------------------------------
|
| This type will be used for new containers.
| Take note that the type specified here has to be specified in the
| types that can be used by the container as well
|
*/
'laravel_validation_error_type' => 'error',

/*
|--------------------------------------------------------------------------
| Default message format
Expand Down
5 changes: 4 additions & 1 deletion tests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public function testCreateNewContainerWithDefaults()
'default',
['info', 'warning', 'success', 'error'],
[],
'info',
':type :message',
[],
[],
Expand All @@ -236,6 +237,7 @@ public function testCreateNewContainerWithDefaults()
$this->assertEquals('test', $container->getName());
$this->assertEquals(':type :message', $container->getDefaultFormat());
$this->assertEquals(['info', 'warning', 'success', 'error'], $container->getTypes());
$this->assertEquals('info', $container->getLaravelValidationErrorType());
}

public function testCreateNewContainerFromDefined()
Expand All @@ -246,6 +248,7 @@ public function testCreateNewContainerFromDefined()
[
'test' => ['info']
],
null,
':type :message',
[
'test' => ':message',
Expand All @@ -268,7 +271,7 @@ public function testCreateNewContainerFromDefined()

protected function getNotification()
{
return new \Krucas\Notification\Notification('default', [], [], null, [], [], []);
return new \Krucas\Notification\Notification('default', [], [], null,null, null, [], []);
}

protected function getMessage()
Expand Down