Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Allocation.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Illuminate\Database\Eloquent\Factories\HasFactory;
7
8
/**
9
* Pterodactyl\Models\Allocation.
10
*
11
* @property int $id
12
* @property int $node_id
13
* @property string $ip
14
* @property string|null $ip_alias
15
* @property int $port
16
* @property int|null $server_id
17
* @property string|null $notes
18
* @property \Carbon\Carbon|null $created_at
19
* @property \Carbon\Carbon|null $updated_at
20
* @property string $alias
21
* @property bool $has_alias
22
* @property Server|null $server
23
* @property Node $node
24
* @property string $hashid
25
*
26
* @method static \Database\Factories\AllocationFactory factory(...$parameters)
27
* @method static \Illuminate\Database\Eloquent\Builder|Allocation newModelQuery()
28
* @method static \Illuminate\Database\Eloquent\Builder|Allocation newQuery()
29
* @method static \Illuminate\Database\Eloquent\Builder|Allocation query()
30
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereCreatedAt($value)
31
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereId($value)
32
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereIp($value)
33
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereIpAlias($value)
34
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereNodeId($value)
35
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereNotes($value)
36
* @method static \Illuminate\Database\Eloquent\Builder|Allocation wherePort($value)
37
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereServerId($value)
38
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereUpdatedAt($value)
39
*
40
* @mixin \Eloquent
41
*/
42
class Allocation extends Model
43
{
44
/** @use HasFactory<\Database\Factories\AllocationFactory> */
45
use HasFactory;
46
47
/**
48
* The resource name for this model when it is transformed into an
49
* API representation using fractal.
50
*/
51
public const RESOURCE_NAME = 'allocation';
52
53
/**
54
* The table associated with the model.
55
*/
56
protected $table = 'allocations';
57
58
/**
59
* Fields that are not mass assignable.
60
*/
61
protected $guarded = ['id', 'created_at', 'updated_at'];
62
63
/**
64
* Cast values to correct type.
65
*/
66
protected $casts = [
67
'node_id' => 'integer',
68
'port' => 'integer',
69
'server_id' => 'integer',
70
];
71
72
public static array $validationRules = [
73
'node_id' => 'required|exists:nodes,id',
74
'ip' => 'required|ip',
75
'port' => 'required|numeric|between:1024,65535',
76
'ip_alias' => 'nullable|string',
77
'server_id' => 'nullable|exists:servers,id',
78
'notes' => 'nullable|string|max:256',
79
];
80
81
public function getRouteKeyName(): string
82
{
83
return $this->getKeyName();
84
}
85
86
/**
87
* Return a hashid encoded string to represent the ID of the allocation.
88
*/
89
public function getHashidAttribute(): string
90
{
91
return app()->make('hashids')->encode($this->id);
92
}
93
94
/**
95
* Accessor to automatically provide the IP alias if defined.
96
*/
97
public function getAliasAttribute(?string $value): string
98
{
99
return (is_null($this->ip_alias)) ? $this->ip : $this->ip_alias;
100
}
101
102
/**
103
* Accessor to quickly determine if this allocation has an alias.
104
*/
105
public function getHasAliasAttribute(?string $value): bool
106
{
107
return !is_null($this->ip_alias);
108
}
109
110
public function toString(): string
111
{
112
return sprintf('%s:%s', $this->ip, $this->port);
113
}
114
115
/**
116
* Gets information for the server associated with this allocation.
117
*/
118
public function server(): BelongsTo
119
{
120
return $this->belongsTo(Server::class);
121
}
122
123
/**
124
* Return the Node model associated with this allocation.
125
*/
126
public function node(): BelongsTo
127
{
128
return $this->belongsTo(Node::class);
129
}
130
}
131
132