Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace layout and overlay to proper Enum types #2015

Merged
merged 5 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/Enum/AlbumLayoutType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Enum;

/**
* Enum AlbumLayoutType.
*
* All the allowed layout possibilities on Album
*/
enum AlbumLayoutType: string
{
case SQUARE = 'square';
case JUSTIFIED = 'justified';
case UNJUSTIFIED = 'unjustified';
}
16 changes: 16 additions & 0 deletions app/Enum/ImageOverlayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enum;

/**
* Enum ImageOverlayType.
*
* All the allowed overlay info on Photos
*/
enum ImageOverlayType: string
{
case NONE = 'none';
case DESC = 'desc';
case DATE = 'date';
case EXIF = 'exif';
}
4 changes: 2 additions & 2 deletions app/Http/Requests/Settings/AbstractSettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
abstract class AbstractSettingRequest extends BaseApiRequest
{
protected string|int|bool $value;
protected string|int|bool|\BackedEnum $value;

protected string $name;

Expand All @@ -30,7 +30,7 @@ public function getSettingName(): string
return $this->name;
}

public function getSettingValue(): string|int|bool
public function getSettingValue(): string|int|bool|\BackedEnum
{
return $this->value;
}
Expand Down
12 changes: 5 additions & 7 deletions app/Http/Requests/Settings/SetImageOverlaySettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

namespace App\Http\Requests\Settings;

use Illuminate\Validation\Rule;
use App\Enum\ImageOverlayType;
use Illuminate\Validation\Rules\Enum;

class SetImageOverlaySettingRequest extends AbstractSettingRequest
{
public const ATTRIBUTE = 'image_overlay_type';

public function rules(): array
{
return [self::ATTRIBUTE => [
'required',
'string',
Rule::in(['none', 'desc', 'date', 'exif']),
],
return [
self::ATTRIBUTE => ['required', new Enum(ImageOverlayType::class)],
];
}

protected function processValidatedValues(array $values, array $files): void
{
$this->name = self::ATTRIBUTE;
$this->value = $values[self::ATTRIBUTE];
$this->value = ImageOverlayType::from($values[self::ATTRIBUTE]);
}
}
7 changes: 4 additions & 3 deletions app/Http/Requests/Settings/SetLayoutSettingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Http\Requests\Settings;

use Illuminate\Validation\Rule;
use App\Enum\AlbumLayoutType;
use Illuminate\Validation\Rules\Enum;

class SetLayoutSettingRequest extends AbstractSettingRequest
{
Expand All @@ -11,13 +12,13 @@ class SetLayoutSettingRequest extends AbstractSettingRequest
public function rules(): array
{
return [
self::ATTRIBUTE => ['required', Rule::in([0, 1, 2])],
self::ATTRIBUTE => ['required', new Enum(AlbumLayoutType::class)],
];
}

protected function processValidatedValues(array $values, array $files): void
{
$this->name = self::ATTRIBUTE;
$this->value = (int) $values[self::ATTRIBUTE];
$this->value = AlbumLayoutType::from($values[self::ATTRIBUTE]);
}
}
6 changes: 4 additions & 2 deletions app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use App\DTO\PhotoSortingCriterion;
use App\Enum\AlbumDecorationOrientation;
use App\Enum\AlbumDecorationType;
use App\Enum\AlbumLayoutType;
use App\Enum\DefaultAlbumProtectionType;
use App\Enum\ImageOverlayType;
use App\Enum\ThumbAlbumSubtitleType;
use App\Exceptions\Handler;
use App\Metadata\Versions\InstalledVersion;
Expand Down Expand Up @@ -131,10 +133,10 @@ public function toArray($request): array
'footer_show_social_media' => Configs::getValueAsBool('footer_show_social_media'),
'grants_download' => Configs::getValueAsBool('grants_download'),
'grants_full_photo_access' => Configs::getValueAsBool('grants_full_photo_access'),
'image_overlay_type' => Configs::getValueAsString('image_overlay_type'),
'image_overlay_type' => Configs::getValueAsEnum('image_overlay_type', ImageOverlayType::class),
'landing_page_enable' => Configs::getValueAsBool('landing_page_enable'),
'lang' => Configs::getValueAsString('lang'),
'layout' => Configs::getValueAsString('layout'),
'layout' => Configs::getValueAsEnum('layout', AlbumLayoutType::class),
'legacy_id_redirection' => Configs::getValueAsBool('legacy_id_redirection'),
'location_decoding' => Configs::getValueAsBool('location_decoding'),
'location_decoding_timeout' => Configs::getValueAsInt('location_decoding_timeout'),
Expand Down
70 changes: 70 additions & 0 deletions database/migrations/2023_09_16_070405_refactor_type_layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class() extends Migration {
public const SQUARE = 'square';
public const JUSTIFIED = 'justified';
public const UNJUSTIFIED = 'unjustified';

/**
* Run the migrations.
*/
public function up(): void
{
/** @var int $layout */
$layout = DB::table('configs')->select('value')->where('key', '=', 'layout')->first()->value;
DB::table('configs')->where('key', '=', 'layout')->delete();
DB::table('configs')->insert([
[
'key' => 'layout',
'value' => $this->toEnum($layout),
'confidentiality' => 0,
'cat' => 'Gallery',
'type_range' => self::SQUARE . '|' . self::JUSTIFIED . '|' . self::UNJUSTIFIED,
'description' => 'Layout for pictures',
],
]);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
/** @var string $layout */
$layout = DB::table('configs')->select('value')->where('key', '=', 'layout')->first()->value;
DB::table('configs')->where('key', '=', 'layout')->delete();
DB::table('configs')->insert([
[
'key' => 'layout',
'value' => $this->fromEnum($layout),
'confidentiality' => 0,
'cat' => 'Gallery',
'type_range' => '0|1|2',
'description' => 'Layout for pictures',
],
]);
}

private function toEnum(int $layout): string
{
return match ($layout) {
0 => self::SQUARE,
1 => self::JUSTIFIED,
2 => self::UNJUSTIFIED,
default => self::JUSTIFIED,
};
}

private function fromEnum(string $layout): int
{
return match ($layout) {
self::SQUARE => 0,
self::JUSTIFIED => 1,
self::UNJUSTIFIED => 2,
default => 1,
};
}
};
3 changes: 2 additions & 1 deletion tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function testSetLang(): void
public function testSetLayout(): void
{
$this->sendKV('/Settings::setLayout', 'layout', 3, 422);
$this->sendKV('/Settings::setLayout', 'layout', 1);
$this->sendKV('/Settings::setLayout', 'layout', 'something', 422);
$this->sendKV('/Settings::setLayout', 'layout', 'justified');
}

// Route::post('/Settings::setDefaultLicense', [Administration\SettingsController::class, 'setDefaultLicense']);
Expand Down