Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Traits/Services/ValidatesValidationRules.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Traits\Services;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
7
use Pterodactyl\Exceptions\Service\Egg\Variable\BadValidationRuleException;
8
9
trait ValidatesValidationRules
10
{
11
abstract protected function getValidator(): ValidationFactory;
12
13
/**
14
* Validate that the rules being provided are valid for Laravel and can
15
* be resolved.
16
*
17
* @throws BadValidationRuleException
18
*/
19
public function validateRules(array|string $rules): void
20
{
21
try {
22
$this->getValidator()->make(['__TEST' => 'test'], ['__TEST' => $rules])->fails();
23
} catch (\BadMethodCallException $exception) {
24
$matches = [];
25
if (preg_match('/Method \[(.+)\] does not exist\./', $exception->getMessage(), $matches)) {
26
throw new BadValidationRuleException(trans('exceptions.nest.variables.bad_validation_rule', ['rule' => Str::snake(str_replace('validate', '', array_get($matches, 1, 'unknownRule')))]), $exception);
27
}
28
29
throw $exception;
30
}
31
}
32
}
33
34