-
-
Save tperrelli/6cfc6d5837bee23af3b1eae7e9764ddf to your computer and use it in GitHub Desktop.
PHP ValidatorException
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Units\Core\Validation; | |
use Illuminate\Contracts\Support\Arrayable; | |
use Illuminate\Contracts\Support\Jsonable; | |
use Illuminate\Support\MessageBag; | |
/** | |
* Class ValidatorException | |
* @package App\Core\Validation | |
*/ | |
class ValidatorException extends \Exception implements Jsonable, Arrayable { | |
/** | |
* @var MessageBag | |
*/ | |
protected $messageBag; | |
/** | |
* @param MessageBag $messageBag | |
*/ | |
public function __construct(MessageBag $messageBag){ | |
$this->messageBag = $messageBag; | |
} | |
/** | |
* @return MessageBag | |
*/ | |
public function getMessageBag(){ | |
return $this->messageBag; | |
} | |
/** | |
* Get the instance as an array. | |
* | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
return [ | |
'error' => 'validation_exception', | |
'error_description' => $this->getMessageBag() | |
]; | |
} | |
/** | |
* Convert the object to its JSON representation. | |
* | |
* @param int $options | |
* @return string | |
*/ | |
public function toJson($options = 0) | |
{ | |
return json_encode($this->toArray(), $options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment