mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-11-13 09:38:48 +01:00
83 lines
2.0 KiB
PHP
83 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Models\Wiki;
|
|
|
|
use App\Contracts\Actions\Models\Wiki\BackfillAlternativeTitles;
|
|
use App\Contracts\Actions\Models\Wiki\BackfillImages;
|
|
use App\Contracts\Actions\Models\Wiki\BackfillResources;
|
|
use App\Enums\Models\Wiki\ResourceSite;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Support\Arr;
|
|
|
|
abstract class ExternalApiAction
|
|
{
|
|
public ?array $response = null;
|
|
|
|
abstract public function getSite(): ResourceSite;
|
|
|
|
/**
|
|
* Set the response after the request.
|
|
*/
|
|
abstract public function handle(BelongsToMany $resources): static;
|
|
|
|
/**
|
|
* Get the mapped resources.
|
|
*
|
|
* @return string[]
|
|
*/
|
|
public function getResources(): array
|
|
{
|
|
$resources = [];
|
|
|
|
if ($this instanceof BackfillResources && $this->response) {
|
|
foreach ($this->getResourcesMapping() as $site => $key) {
|
|
if (Arr::get($this->response, $key) !== null) {
|
|
$resources[$site] = Arr::get($this->response, $key);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $resources;
|
|
}
|
|
|
|
/**
|
|
* Get the mapped images.
|
|
*
|
|
* @return string[]
|
|
*/
|
|
public function getImages(): array
|
|
{
|
|
$images = [];
|
|
|
|
if ($this instanceof BackfillImages && $this->response) {
|
|
foreach ($this->getImagesMapping() as $facet => $key) {
|
|
if (Arr::get($this->response, $key) !== null) {
|
|
$images[$facet] = Arr::get($this->response, $key);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $images;
|
|
}
|
|
|
|
/**
|
|
* Get the mapped titles.
|
|
*
|
|
* @return array<string, string|null>
|
|
*/
|
|
public function getAlternativeTitles(): array
|
|
{
|
|
$titles = [];
|
|
|
|
if ($this instanceof BackfillAlternativeTitles && $this->response) {
|
|
foreach ($this->getAlternativeTitlesMapping() as $field => $key) {
|
|
$titles[$field] = Arr::get($this->response, $key);
|
|
}
|
|
}
|
|
|
|
return $titles;
|
|
}
|
|
}
|