2017-06-25 00:49:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Exceptions\Model;
|
|
|
|
|
2022-06-26 20:31:48 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-06-25 00:49:09 +00:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2017-12-17 20:57:05 +00:00
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
2017-06-25 00:49:09 +00:00
|
|
|
use Illuminate\Contracts\Support\MessageProvider;
|
2017-12-17 20:57:05 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
2017-06-25 00:49:09 +00:00
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
class DataValidationException extends PterodactylException implements HttpExceptionInterface, MessageProvider
|
2017-06-25 00:49:09 +00:00
|
|
|
{
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* The validator instance.
|
|
|
|
*/
|
2022-06-26 20:31:48 +00:00
|
|
|
protected Validator $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The underlying model instance that triggered this exception.
|
|
|
|
*/
|
|
|
|
protected Model $model;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2017-06-25 00:49:09 +00:00
|
|
|
/**
|
|
|
|
* DataValidationException constructor.
|
|
|
|
*/
|
2022-06-26 20:31:48 +00:00
|
|
|
public function __construct(Validator $validator, Model $model)
|
2017-06-25 00:49:09 +00:00
|
|
|
{
|
2022-06-26 20:31:48 +00:00
|
|
|
$message = sprintf(
|
|
|
|
'Could not save %s[%s]: failed to validate data: %s',
|
|
|
|
get_class($model),
|
|
|
|
$model->getKey(),
|
|
|
|
$validator->errors()->toJson()
|
2017-12-17 20:57:05 +00:00
|
|
|
);
|
|
|
|
|
2022-06-26 20:31:48 +00:00
|
|
|
parent::__construct($message);
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
$this->validator = $validator;
|
2022-06-26 20:31:48 +00:00
|
|
|
$this->model = $model;
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-17 20:57:05 +00:00
|
|
|
* Return the validator message bag.
|
|
|
|
*
|
2017-06-25 00:49:09 +00:00
|
|
|
* @return \Illuminate\Support\MessageBag
|
|
|
|
*/
|
|
|
|
public function getMessageBag()
|
|
|
|
{
|
|
|
|
return $this->validator->errors();
|
|
|
|
}
|
2017-12-17 20:57:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the status code for this request.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStatusCode()
|
|
|
|
{
|
|
|
|
return 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHeaders()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2022-06-26 20:31:48 +00:00
|
|
|
|
|
|
|
public function getValidator(): Validator
|
|
|
|
{
|
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getModel(): Model
|
|
|
|
{
|
|
|
|
return $this->model;
|
|
|
|
}
|
2017-06-25 00:49:09 +00:00
|
|
|
}
|