Files

160 lines
4.8 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Models;
use Database\Factories\TagFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\RouteKey;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Pterodactyl\Enum\EggSpecificTags;
use Pterodactyl\Rules\TagSlug;
#[Fillable([
'name',
'slug',
'color',
'legacy_nest_id',
])]
#[RouteKey('id')]
class Tag extends Model
{
/** @use HasFactory<TagFactory> */
use HasFactory;
public const string RESOURCE_NAME = 'tag';
/**
* @var list<string>
*/
public static array $searchableColumns = [
'name',
'slug',
];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'legacy_nest_id' => 'integer',
];
}
/**
2026-10-12 02:55:15 -04:00
* @param array<string> $identifiers
2026-10-05 18:10:18 -04:00
* @return Collection<int, self>
*/
public static function matchingKeysOrSlugs(array $identifiers): Collection
{
$query = self::query();
self::filterKeysOrSlugs($query, $identifiers);
return $query->get(['id', 'slug']);
}
2026-10-12 01:51:18 -04:00
/**
2026-10-12 02:55:15 -04:00
* The tags with these slugs, creating the missing ones; a built-in slug gets its friendly
* name. Numbers are never slugs (see TagSlug), so they are skipped.
2026-10-12 01:51:18 -04:00
*
2026-10-12 02:55:15 -04:00
* @param array<string> $slugs
2026-10-12 01:51:18 -04:00
* @return Collection<int, self>
*/
public static function findOrCreateFromSlugs(array $slugs): Collection
{
2026-10-12 02:55:15 -04:00
$slugs = collect($slugs)->reject(TagSlug::isReserved(...))->unique();
if ($slugs->isEmpty()) {
2026-10-12 01:51:18 -04:00
return new Collection;
}
2026-10-12 02:55:15 -04:00
$existing = self::query()->whereIn('slug', $slugs)->get();
2026-10-12 01:51:18 -04:00
2026-10-12 02:55:15 -04:00
return $existing->merge($slugs->diff($existing->map(fn (self $tag): string => $tag->slug))->map(
fn (string $slug): self => self::query()->firstOrCreate(['slug' => $slug], ['name' => EggSpecificTags::tryFrom($slug)?->friendlyName() ?? $slug]),
));
2026-10-12 01:51:18 -04:00
}
2026-10-05 18:10:18 -04:00
/**
* The nodes carrying this tag, under either pivot kind. The deploy gate reads
* the kind-scoped Node::eggTags()/deploymentTags() instead.
*
* @return MorphToMany<Node, $this>
*/
public function nodes(): MorphToMany
{
return $this->morphedByMany(Node::class, 'taggable', 'taggables', 'tag_id', 'taggable_id');
}
/**
* @return MorphToMany<Egg, $this>
*/
public function eggs(): MorphToMany
{
return $this->morphedByMany(Egg::class, 'taggable', 'taggables', 'tag_id', 'taggable_id');
}
/**
* Whether this is a built-in "special" game tag whose presentation is owned by
* the EggSpecificTags enum, and which therefore cannot be edited.
*/
public function isPredefined(): bool
{
return EggSpecificTags::isSpecial($this->slug);
}
/**
* The chip colour to render: the enum's brand colour for built-in tags,
* otherwise the operator-chosen colour.
*/
public function effectiveColor(): ?string
{
return $this->isPredefined() ? EggSpecificTags::from($this->slug)->color() : $this->color;
}
/**
* The display name to render: the enum's friendly name for built-in tags,
* otherwise the operator-chosen name.
*/
public function effectiveName(): string
{
return $this->isPredefined() ? EggSpecificTags::from($this->slug)->friendlyName() : $this->name;
}
/**
* Match a tag by either its primary key or its slug - pass a single identifier
* or a list of them. Callers can therefore mix ids and slugs freely: a slug can
* never be purely numeric ({@see TagSlug} forbids it), so the
* two identifier spaces are disjoint and the match is unambiguous.
*
* @param Builder<self> $query
* @param string|array<array-key, string> $identifier
*/
#[Scope]
protected function whereKeyOrSlug(Builder $query, string|array $identifier): void
{
$identifiers = array_values((array) $identifier);
self::filterKeysOrSlugs($query, $identifiers);
}
/**
* @param Builder<self> $query
2026-10-12 02:55:15 -04:00
* @param array<string> $identifiers
2026-10-05 18:10:18 -04:00
*/
private static function filterKeysOrSlugs(Builder $query, array $identifiers): void
{
2026-10-12 02:55:15 -04:00
$keys = array_values(array_filter($identifiers, TagSlug::isReserved(...)));
2026-10-05 18:10:18 -04:00
$slugColumn = $query->getModel()->qualifyColumn('slug');
2026-10-12 02:55:15 -04:00
$query->where(fn (Builder $inner) => $inner
->when($keys !== [], fn (Builder $byKey) => $byKey->whereKey($keys))
->orWhereIn($slugColumn, $identifiers));
2026-10-05 18:10:18 -04:00
}
}