misc_pterodactyl-panel/app/Models/Model.php

181 lines
4.8 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Models;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Container\Container;
use Illuminate\Validation\Validator;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
abstract class Model extends IlluminateModel
{
use HasFactory;
/**
* Set to true to return immutable Carbon date instances from the model.
*/
2021-07-17 21:18:05 +00:00
protected bool $immutableDates = false;
/**
* Determines if the model should undergo data validation before it is saved
* to the database.
*
* @var bool
*/
protected $skipValidation = false;
/**
* The validator instance used by this model.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;
/**
* @var \Illuminate\Contracts\Validation\Factory
*/
protected static $validatorFactory;
2021-07-17 21:18:05 +00:00
public static array $validationRules = [];
/**
* Listen for the model saving event and fire off the validation
* function before it is saved.
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
protected static function boot()
{
parent::boot();
static::$validatorFactory = Container::getInstance()->make(Factory::class);
static::saving(function (Model $model) {
2021-01-23 20:33:34 +00:00
if (!$model->validate()) {
throw new DataValidationException($model->getValidator());
}
return true;
});
}
/**
* Set the model to skip validation when saving.
*
* @return $this
*/
public function skipValidation()
{
$this->skipValidation = true;
return $this;
}
/**
* Returns the validator instance used by this model.
*/
public function getValidator(): Validator
{
$rules = $this->getKey() ? static::getRulesForUpdate($this) : static::getRules();
return $this->validator ?: $this->validator = static::$validatorFactory->make(
2021-01-23 20:33:34 +00:00
[],
$rules,
[],
[]
);
}
/**
* Returns the rules associated with this model.
*
* @return array
*/
public static function getRules()
{
$rules = static::$validationRules;
foreach ($rules as $key => &$rule) {
$rule = is_array($rule) ? $rule : explode('|', $rule);
}
return $rules;
}
/**
* Returns the rules associated with the model, specifically for updating the given model
* rather than just creating it.
*
* @param \Illuminate\Database\Eloquent\Model|int|string $id
2021-01-23 20:33:34 +00:00
*
* @return array
*/
public static function getRulesForUpdate($id, string $primaryKey = 'id')
{
if ($id instanceof Model) {
2019-09-23 19:30:51 +00:00
[$primaryKey, $id] = [$id->getKeyName(), $id->getKey()];
}
$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) {
2021-01-23 20:33:34 +00:00
if (!is_string($datum) || !Str::startsWith($datum, 'unique')) {
continue;
}
2019-09-23 19:30:51 +00:00
[, $args] = explode(':', $datum);
$args = explode(',', $args);
$datum = Rule::unique($args[0], $args[1] ?? $key)->ignore($id, $primaryKey)->__toString();
}
}
return $rules;
}
/**
* Determines if the model is in a valid state or not.
*
* @return bool
*/
public function validate()
{
if ($this->skipValidation) {
return true;
}
return $this->getValidator()->setData(
// Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist
// for that model. Doing this will return all of the attributes in a format that can
// properly be validated.
2020-03-28 23:06:36 +00:00
$this->addCastAttributesToArray(
2021-01-23 20:33:34 +00:00
$this->getAttributes(),
$this->getMutatedAttributes()
2020-03-28 23:06:36 +00:00
)
)->passes();
}
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
2021-01-23 20:33:34 +00:00
*
* @return \Illuminate\Support\Carbon|\Carbon\CarbonImmutable
*/
protected function asDateTime($value)
{
2021-01-23 20:33:34 +00:00
if (!$this->immutableDates) {
return parent::asDateTime($value);
}
2020-04-04 18:37:10 +00:00
return parent::asDateTime($value)->toImmutable();
}
}