mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-10-18 16:50:49 +02:00
115 lines
3.1 KiB
PHP
115 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Api\Field\Admin\FeaturedTheme;
|
|
|
|
use App\Contracts\Http\Api\Field\CreatableField;
|
|
use App\Contracts\Http\Api\Field\SelectableField;
|
|
use App\Contracts\Http\Api\Field\UpdatableField;
|
|
use App\Http\Api\Field\Field;
|
|
use App\Http\Api\Query\Query;
|
|
use App\Http\Api\Schema\Schema;
|
|
use App\Models\Admin\FeaturedTheme;
|
|
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
|
|
use App\Pivots\Wiki\AnimeThemeEntryVideo;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* Class FeaturedThemeEntryIdField.
|
|
*/
|
|
class FeaturedThemeEntryIdField extends Field implements CreatableField, SelectableField, UpdatableField
|
|
{
|
|
/**
|
|
* Create a new field instance.
|
|
*
|
|
* @param Schema $schema
|
|
*/
|
|
public function __construct(Schema $schema)
|
|
{
|
|
parent::__construct($schema, FeaturedTheme::ATTRIBUTE_ENTRY);
|
|
}
|
|
|
|
/**
|
|
* Set the creation validation rules for the field.
|
|
*
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function getCreationRules(Request $request): array
|
|
{
|
|
$videoId = $this->resolveVideoId($request);
|
|
|
|
return [
|
|
'sometimes',
|
|
'required',
|
|
'integer',
|
|
Rule::exists(AnimeThemeEntry::class, AnimeThemeEntry::ATTRIBUTE_ID),
|
|
Rule::when(
|
|
! empty($videoId),
|
|
[
|
|
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_ENTRY)
|
|
->where(AnimeThemeEntryVideo::ATTRIBUTE_VIDEO, $this->resolveVideoId($request)),
|
|
]
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Determine if the field should be included in the select clause of our query.
|
|
*
|
|
* @param Query $query
|
|
* @param Schema $schema
|
|
* @return bool
|
|
*/
|
|
public function shouldSelect(Query $query, Schema $schema): bool
|
|
{
|
|
// Needed to match entry relation.
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Set the update validation rules for the field.
|
|
*
|
|
* @param Request $request
|
|
* @return array
|
|
*/
|
|
public function getUpdateRules(Request $request): array
|
|
{
|
|
$videoId = $this->resolveVideoId($request);
|
|
|
|
return [
|
|
'sometimes',
|
|
'required',
|
|
'integer',
|
|
Rule::exists(AnimeThemeEntry::class, AnimeThemeEntry::ATTRIBUTE_ID),
|
|
Rule::when(
|
|
! empty($videoId),
|
|
[
|
|
Rule::exists(AnimeThemeEntryVideo::class, AnimeThemeEntryVideo::ATTRIBUTE_ENTRY)
|
|
->where(AnimeThemeEntryVideo::ATTRIBUTE_VIDEO, $this->resolveVideoId($request)),
|
|
]
|
|
),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get dependent video_id field.
|
|
*
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
private function resolveVideoId(Request $request): mixed
|
|
{
|
|
if ($request->has(FeaturedTheme::ATTRIBUTE_VIDEO)) {
|
|
return $request->get(FeaturedTheme::ATTRIBUTE_VIDEO);
|
|
}
|
|
|
|
/** @var FeaturedTheme|null $featuredTheme */
|
|
$featuredTheme = $request->route('featuredtheme');
|
|
|
|
return $featuredTheme?->video_id;
|
|
}
|
|
}
|