Skip to content

Commit

Permalink
feat: add language filter to api
Browse files Browse the repository at this point in the history
  • Loading branch information
daurensky committed Apr 9, 2024
1 parent ee6ed6b commit 33eba73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/UI/API/Controllers/LocalizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace AdminKit\Localizations\UI\API\Controllers;

use AdminKit\Localizations\Models\Localization;
use AdminKit\Localizations\UI\API\Data\LocalizationData;

class LocalizationController extends Controller
{
public function index()
{
return Localization::all();
return LocalizationData::collection(Localization::all());
}

public function show(int $id)
Expand Down
29 changes: 29 additions & 0 deletions src/UI/API/Data/LocalizationData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace AdminKit\Localizations\UI\API\Data;

use AdminKit\Localizations\Models\Localization;
use Spatie\LaravelData\Data;

class LocalizationData extends Data
{
public function __construct(
public string $key,
public array|string $content,
) {
}

public static function fromModel(Localization $localization): LocalizationData
{
$content = $localization->getTranslations('content');

if ($locale = request('locale')) {
$content = $content[$locale] ?? $content;
}

return new self(
key: $localization->key,

Check failure on line 25 in src/UI/API/Data/LocalizationData.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.

Check failure on line 25 in src/UI/API/Data/LocalizationData.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property AdminKit\Localizations\Models\Localization::$key.
content: $content,
);
}
}

0 comments on commit 33eba73

Please sign in to comment.