Add support for immutable carbon dates in models

This commit is contained in:
Dane Everitt 2020-04-03 23:22:35 -07:00
parent 6d1226a0c1
commit f51d65229b
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
20 changed files with 79 additions and 26 deletions

View File

@ -18,7 +18,7 @@ namespace Pterodactyl\Models;
* @property \Pterodactyl\Models\Server|null $server * @property \Pterodactyl\Models\Server|null $server
* @property \Pterodactyl\Models\Node $node * @property \Pterodactyl\Models\Node $node
*/ */
class Allocation extends Validable class Allocation extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an
@ -75,7 +75,7 @@ class Allocation extends Validable
/** /**
* Accessor to automatically provide the IP alias if defined. * Accessor to automatically provide the IP alias if defined.
* *
* @param null|string $value * @param string|null $value
* @return string * @return string
*/ */
public function getAliasAttribute($value) public function getAliasAttribute($value)
@ -86,7 +86,7 @@ class Allocation extends Validable
/** /**
* Accessor to quickly determine if this allocation has an alias. * Accessor to quickly determine if this allocation has an alias.
* *
* @param null|string $value * @param string|null $value
* @return bool * @return bool
*/ */
public function getHasAliasAttribute($value) public function getHasAliasAttribute($value)

View File

@ -16,7 +16,7 @@ use Pterodactyl\Services\Acl\Api\AdminAcl;
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
*/ */
class ApiKey extends Validable class ApiKey extends Model
{ {
const RESOURCE_NAME = 'api_key'; const RESOURCE_NAME = 'api_key';

View File

@ -4,7 +4,7 @@ namespace Pterodactyl\Models;
use Znck\Eloquent\Traits\BelongsToThrough; use Znck\Eloquent\Traits\BelongsToThrough;
class DaemonKey extends Validable class DaemonKey extends Model
{ {
use BelongsToThrough; use BelongsToThrough;

View File

@ -2,7 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
class Database extends Validable class Database extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -2,7 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
class DatabaseHost extends Validable class DatabaseHost extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -39,7 +39,7 @@ namespace Pterodactyl\Models;
* @property \Pterodactyl\Models\Egg|null $scriptFrom * @property \Pterodactyl\Models\Egg|null $scriptFrom
* @property \Pterodactyl\Models\Egg|null $configFrom * @property \Pterodactyl\Models\Egg|null $configFrom
*/ */
class Egg extends Validable class Egg extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -2,7 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
class EggVariable extends Validable class EggVariable extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -2,7 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
class Location extends Validable class Location extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -5,11 +5,21 @@ namespace Pterodactyl\Models;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Illuminate\Container\Container; use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Validation\Factory; use Illuminate\Contracts\Validation\Factory;
use Pterodactyl\Models\Traits\WithImmutableDates;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
abstract class Validable extends Model abstract class Model extends IlluminateModel
{ {
use WithImmutableDates;
/**
* Set to true to return immutable Carbon date instances from the model.
*
* @var bool
*/
protected $immutableDates = false;
/** /**
* Determines if the model should undergo data validation before it is saved * Determines if the model should undergo data validation before it is saved
* to the database. * to the database.
@ -47,7 +57,7 @@ abstract class Validable extends Model
static::$validatorFactory = Container::getInstance()->make(Factory::class); static::$validatorFactory = Container::getInstance()->make(Factory::class);
static::saving(function (Validable $model) { static::saving(function (Model $model) {
return $model->validate(); return $model->validate();
}); });
} }
@ -148,4 +158,19 @@ abstract class Validable extends Model
) )
)->passes(); )->passes();
} }
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return \Illuminate\Support\Carbon|\Carbon\CarbonImmutable
*/
protected function asDateTime($value)
{
if (! $this->immutableDates) {
return parent::asDateTime($value);
}
return $this->asImmutableDateTime($value);
}
} }

View File

@ -15,7 +15,7 @@ namespace Pterodactyl\Models;
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Egg[] $eggs * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Egg[] $eggs
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs
*/ */
class Nest extends Validable class Nest extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -32,9 +32,10 @@ use Pterodactyl\Models\Traits\Searchable;
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
*/ */
class Node extends Validable class Node extends Model
{ {
use Notifiable, Searchable; use Notifiable;
use Searchable;
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -20,7 +20,7 @@ use Pterodactyl\Models\Traits\Searchable;
* @property \Pterodactyl\Models\Egg|null $egg * @property \Pterodactyl\Models\Egg|null $egg
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers
*/ */
class Pack extends Validable class Pack extends Model
{ {
use Searchable; use Searchable;

View File

@ -4,7 +4,7 @@ namespace Pterodactyl\Models;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class Permission extends Validable class Permission extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -25,7 +25,7 @@ use Pterodactyl\Contracts\Extensions\HashidsInterface;
* @property \Pterodactyl\Models\Server $server * @property \Pterodactyl\Models\Server $server
* @property \Pterodactyl\Models\Task[]|\Illuminate\Support\Collection $tasks * @property \Pterodactyl\Models\Task[]|\Illuminate\Support\Collection $tasks
*/ */
class Schedule extends Validable class Schedule extends Model
{ {
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -52,9 +52,11 @@ use Znck\Eloquent\Traits\BelongsToThrough;
* @property \Pterodactyl\Models\DaemonKey $key * @property \Pterodactyl\Models\DaemonKey $key
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
*/ */
class Server extends Validable class Server extends Model
{ {
use BelongsToThrough, Notifiable, Searchable; use BelongsToThrough;
use Notifiable;
use Searchable;
/** /**
* The resource name for this model when it is transformed into an * The resource name for this model when it is transformed into an

View File

@ -2,7 +2,7 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
class Setting extends Validable class Setting extends Model
{ {
/** /**
* The table associated with the model. * The table associated with the model.

View File

@ -15,7 +15,7 @@ use Illuminate\Notifications\Notifiable;
* @property \Pterodactyl\Models\User $user * @property \Pterodactyl\Models\User $user
* @property \Pterodactyl\Models\Server $server * @property \Pterodactyl\Models\Server $server
*/ */
class Subuser extends Validable class Subuser extends Model
{ {
use Notifiable; use Notifiable;

View File

@ -22,7 +22,7 @@ use Pterodactyl\Contracts\Extensions\HashidsInterface;
* @property \Pterodactyl\Models\Schedule $schedule * @property \Pterodactyl\Models\Schedule $schedule
* @property \Pterodactyl\Models\Server $server * @property \Pterodactyl\Models\Server $server
*/ */
class Task extends Validable class Task extends Model
{ {
use BelongsToThrough; use BelongsToThrough;

View File

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Models\Traits;
/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait WithImmutableDates
{
/**
* Converts the mutable Carbon instance into an immutable Carbon instance.
*
* @param mixed $value
* @return \Carbon\CarbonImmutable
*/
protected function asImmutableDateTime($value)
{
return $this->asDateTime($value)->toImmutable();
}
}

View File

@ -40,12 +40,17 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
*/ */
class User extends Validable implements class User extends Model implements
AuthenticatableContract, AuthenticatableContract,
AuthorizableContract, AuthorizableContract,
CanResetPasswordContract CanResetPasswordContract
{ {
use Authenticatable, Authorizable, AvailableLanguages, CanResetPassword, Notifiable, Searchable; use Authenticatable;
use Authorizable;
use AvailableLanguages;
use CanResetPassword;
use Notifiable;
use Searchable;
const USER_LEVEL_USER = 0; const USER_LEVEL_USER = 0;
const USER_LEVEL_ADMIN = 1; const USER_LEVEL_ADMIN = 1;