2019-09-05 04:00:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Models;
|
|
|
|
|
2019-09-05 05:19:57 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Illuminate\Container\Container;
|
2019-09-05 04:00:34 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-09-05 05:19:57 +00:00
|
|
|
use Illuminate\Contracts\Validation\Factory;
|
2019-09-05 04:00:34 +00:00
|
|
|
|
|
|
|
abstract class Validable extends Model
|
|
|
|
{
|
|
|
|
/**
|
2019-09-05 05:19:57 +00:00
|
|
|
* Determines if the model should undergo data validation before it is saved
|
|
|
|
* to the database.
|
|
|
|
*
|
|
|
|
* @var bool
|
2019-09-05 04:00:34 +00:00
|
|
|
*/
|
2019-09-05 05:19:57 +00:00
|
|
|
protected $skipValidation = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The validator instance used by this model.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Validation\Validator
|
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Validation\Factory
|
|
|
|
*/
|
|
|
|
protected static $validatorFactory;
|
2019-09-05 04:00:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-05 05:19:57 +00:00
|
|
|
public static $validationRules = [];
|
2019-09-05 04:00:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for the model saving event and fire off the validation
|
|
|
|
* function before it is saved.
|
2019-09-05 05:19:57 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2019-09-05 04:00:34 +00:00
|
|
|
*/
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
2019-09-05 05:19:57 +00:00
|
|
|
static::$validatorFactory = Container::getInstance()->make(Factory::class);
|
|
|
|
|
2019-09-05 04:00:34 +00:00
|
|
|
static::saving(function (Validable $model) {
|
|
|
|
return $model->validate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-05 05:19:57 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Validation\Validator|\Illuminate\Contracts\Validation\Validator
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
|
|
|
$rules = $this->getKey() ? static::getRulesForUpdate($this) : static::getRules();
|
|
|
|
|
|
|
|
return $this->validator ?: $this->validator = static::$validatorFactory->make(
|
|
|
|
[], $rules, [], []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-05 05:26:28 +00:00
|
|
|
* Returns the rules associated with this model.
|
|
|
|
*
|
2019-09-05 05:19:57 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getRules()
|
|
|
|
{
|
2019-09-05 05:26:28 +00:00
|
|
|
$rules = static::$validationRules;
|
|
|
|
foreach ($rules as $key => &$rule) {
|
|
|
|
$rule = is_array($rule) ? $rule : explode('|', $rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
2019-09-05 05:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-05 05:26:28 +00:00
|
|
|
* Returns the rules associated with the model, specifically for updating the given model
|
|
|
|
* rather than just creating it.
|
|
|
|
*
|
2019-09-05 05:19:57 +00:00
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int|string $id
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param string $primaryKey
|
2019-09-05 05:19:57 +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()];
|
2019-09-05 05:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2019-09-05 05:26:28 +00:00
|
|
|
if (! is_string($datum) || ! Str::startsWith($datum, 'unique')) {
|
2019-09-05 05:19:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:30:51 +00:00
|
|
|
[, $args] = explode(':', $datum);
|
2019-09-05 05:19:57 +00:00
|
|
|
$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.
|
|
|
|
*
|
2019-09-05 04:00:34 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
2019-09-05 05:19:57 +00:00
|
|
|
if ($this->skipValidation) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getValidator()->setData(
|
|
|
|
$this->getAttributes()
|
|
|
|
)->passes();
|
2019-09-05 04:00:34 +00:00
|
|
|
}
|
|
|
|
}
|