pelican-panel-mirror/app/Traits/HasValidation.php
Lance Pioch 635cc6a029
Add PHP 8.4 Support (#858)
* Add php 8.4

* Update ide helper

* Add php 8.4

* Update laravel sanctum

* Update laravel framework

* Hash rounds were increased

* This is always false

* Extend model now

* This does nothing

* Move model validation methods to trait

* Remove base model

* Backup routes were previously referenced by uuids

* Remove commented code

* Upgrade laravel/framework

* Fix migration

* Update ide helper

* Update sanctum

* Add version to composer

* Add this back in, fixed

* Make this protected to be safer
2025-01-30 16:39:00 -05:00

109 lines
3.3 KiB
PHP

<?php
namespace App\Traits;
use App\Observers\ValidationObserver;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
#[ObservedBy([ValidationObserver::class])]
trait HasValidation
{
/**
* Returns the validator instance used by this model.
*/
public function getValidator(): Validator
{
$rules = $this->exists ? static::getRulesForUpdate($this) : static::getRules();
$validatorFactory = Container::getInstance()->make(ValidationFactory::class);
return $validatorFactory->make([], $rules);
}
/**
* Returns the rules associated with this model.
*/
public static function getRules(): array
{
$rules = static::$validationRules;
foreach ($rules as &$rule) {
$rule = is_array($rule) ? $rule : explode('|', $rule);
}
return $rules;
}
/**
* Returns the rules for a specific field. If the field is not found an empty
* array is returned.
*/
public static function getRulesForField(string $field): array
{
return Arr::get(static::getRules(), $field) ?? [];
}
/**
* Returns the rules associated with the model, specifically for updating the given model
* rather than just creating it.
*/
public static function getRulesForUpdate(self $model): array
{
[$id, $column] = [$model->getKey(), $model->getKeyName()];
$rules = static::getRules();
foreach ($rules as $key => &$data) {
// For each rule in a given field, iterate over it and confirm if the rule
// is one for a unique field. If that is the case, append the ID of the current
// working model, so we don't run into errors due to the way that field validation
// works.
foreach ($data as &$datum) {
if (!is_string($datum) || !Str::startsWith($datum, 'unique')) {
continue;
}
[, $args] = explode(':', $datum);
$args = explode(',', $args);
$datum = Rule::unique($args[0], $args[1] ?? $key)->ignore($id ?? $model, $column);
}
}
return $rules;
}
/**
* Determines if the model is in a valid state or not.
*
* @throws \Illuminate\Validation\ValidationException
*/
public function validate(): void
{
if (isset($this->skipValidation)) {
return;
}
$validator = $this->getValidator();
$validator->setData(
// Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist
// for that model. Doing this will return all the attributes in a format that can
// properly be validated.
$this->addCastAttributesToArray(
$this->getAttributes(),
$this->getMutatedAttributes()
)
);
if (!$validator->passes()) {
throw new ValidationException($validator);
}
}
}