2018-03-17 20:09:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Traits\Services;
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2018-03-17 20:09:09 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException;
|
|
|
|
|
|
|
|
trait ValidatesValidationRules
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
abstract protected function getValidator(): ValidationFactory;
|
2018-03-17 20:09:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that the rules being provided are valid for Laravel and can
|
|
|
|
* be resolved.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function validateRules(array|string $rules): void
|
2018-03-17 20:09:09 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->getValidator()->make(['__TEST' => 'test'], ['__TEST' => $rules])->fails();
|
2023-02-23 19:30:16 +00:00
|
|
|
} catch (\BadMethodCallException $exception) {
|
2018-03-17 20:09:09 +00:00
|
|
|
$matches = [];
|
|
|
|
if (preg_match('/Method \[(.+)\] does not exist\./', $exception->getMessage(), $matches)) {
|
2021-01-23 20:33:34 +00:00
|
|
|
throw new BadValidationRuleException(trans('exceptions.nest.variables.bad_validation_rule', ['rule' => Str::snake(str_replace('validate', '', array_get($matches, 1, 'unknownRule')))]), $exception);
|
2018-03-17 20:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|