Skip to content

Commit f51d652

Browse files
committed
Add support for immutable carbon dates in models
1 parent 6d1226a commit f51d652

20 files changed

+79
-26
lines changed

app/Models/Allocation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @property \Pterodactyl\Models\Server|null $server
1919
* @property \Pterodactyl\Models\Node $node
2020
*/
21-
class Allocation extends Validable
21+
class Allocation extends Model
2222
{
2323
/**
2424
* The resource name for this model when it is transformed into an
@@ -75,7 +75,7 @@ public function getHashidAttribute()
7575
/**
7676
* Accessor to automatically provide the IP alias if defined.
7777
*
78-
* @param null|string $value
78+
* @param string|null $value
7979
* @return string
8080
*/
8181
public function getAliasAttribute($value)
@@ -86,7 +86,7 @@ public function getAliasAttribute($value)
8686
/**
8787
* Accessor to quickly determine if this allocation has an alias.
8888
*
89-
* @param null|string $value
89+
* @param string|null $value
9090
* @return bool
9191
*/
9292
public function getHasAliasAttribute($value)

app/Models/ApiKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @property \Carbon\Carbon $created_at
1717
* @property \Carbon\Carbon $updated_at
1818
*/
19-
class ApiKey extends Validable
19+
class ApiKey extends Model
2020
{
2121
const RESOURCE_NAME = 'api_key';
2222

app/Models/DaemonKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Znck\Eloquent\Traits\BelongsToThrough;
66

7-
class DaemonKey extends Validable
7+
class DaemonKey extends Model
88
{
99
use BelongsToThrough;
1010

app/Models/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Models;
44

5-
class Database extends Validable
5+
class Database extends Model
66
{
77
/**
88
* The resource name for this model when it is transformed into an

app/Models/DatabaseHost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Models;
44

5-
class DatabaseHost extends Validable
5+
class DatabaseHost extends Model
66
{
77
/**
88
* The resource name for this model when it is transformed into an

app/Models/Egg.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @property \Pterodactyl\Models\Egg|null $scriptFrom
4040
* @property \Pterodactyl\Models\Egg|null $configFrom
4141
*/
42-
class Egg extends Validable
42+
class Egg extends Model
4343
{
4444
/**
4545
* The resource name for this model when it is transformed into an

app/Models/EggVariable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Models;
44

5-
class EggVariable extends Validable
5+
class EggVariable extends Model
66
{
77
/**
88
* The resource name for this model when it is transformed into an

app/Models/Location.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Pterodactyl\Models;
44

5-
class Location extends Validable
5+
class Location extends Model
66
{
77
/**
88
* The resource name for this model when it is transformed into an
Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
use Illuminate\Support\Str;
66
use Illuminate\Validation\Rule;
77
use Illuminate\Container\Container;
8-
use Illuminate\Database\Eloquent\Model;
98
use Illuminate\Contracts\Validation\Factory;
9+
use Pterodactyl\Models\Traits\WithImmutableDates;
10+
use Illuminate\Database\Eloquent\Model as IlluminateModel;
1011

11-
abstract class Validable extends Model
12+
abstract class Model extends IlluminateModel
1213
{
14+
use WithImmutableDates;
15+
16+
/**
17+
* Set to true to return immutable Carbon date instances from the model.
18+
*
19+
* @var bool
20+
*/
21+
protected $immutableDates = false;
22+
1323
/**
1424
* Determines if the model should undergo data validation before it is saved
1525
* to the database.
@@ -47,7 +57,7 @@ protected static function boot()
4757

4858
static::$validatorFactory = Container::getInstance()->make(Factory::class);
4959

50-
static::saving(function (Validable $model) {
60+
static::saving(function (Model $model) {
5161
return $model->validate();
5262
});
5363
}
@@ -148,4 +158,19 @@ public function validate()
148158
)
149159
)->passes();
150160
}
161+
162+
/**
163+
* Return a timestamp as DateTime object.
164+
*
165+
* @param mixed $value
166+
* @return \Illuminate\Support\Carbon|\Carbon\CarbonImmutable
167+
*/
168+
protected function asDateTime($value)
169+
{
170+
if (! $this->immutableDates) {
171+
return parent::asDateTime($value);
172+
}
173+
174+
return $this->asImmutableDateTime($value);
175+
}
151176
}

app/Models/Nest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Egg[] $eggs
1616
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs
1717
*/
18-
class Nest extends Validable
18+
class Nest extends Model
1919
{
2020
/**
2121
* The resource name for this model when it is transformed into an

0 commit comments

Comments
 (0)