Skip to content

Commit

Permalink
map providers are now specified in an Enum (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria authored Oct 1, 2023
1 parent 01c2445 commit 6b2b04c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
38 changes: 38 additions & 0 deletions app/Enum/MapProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Enum;

/**
* Define the possible Map providers and their layer & attributions.
* (will be used later in Livewire).
*/
enum MapProviders: string
{
case Wikimedia = 'Wikimedia';
case OpenStreetMapOrg = 'OpenStreetMap.org';
case OpenStreetMapDe = 'OpenStreetMap.de';
case OpenStreetMapFr = 'OpenStreetMap.fr';
case RRZE = 'RRZE';

public function getLayer(): string
{
return match ($this) {
self::Wikimedia => 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}{r}.png',
self::OpenStreetMapOrg => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
self::OpenStreetMapDe => 'https://tile.openstreetmap.de/{z}/{x}/{y}.png ',
self::OpenStreetMapFr => 'https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png ',
self::RRZE => 'https://{s}.osm.rrze.fau.de/osmhd/{z}/{x}/{y}.png',
};
}

public function getAtributionHtml(): string
{
return match ($this) {
self::Wikimedia => '<a href="https://wikimediafoundation.org/wiki/Maps_Terms_of_Use">Wikimedia</a>',
self::OpenStreetMapOrg => '&copy; <a href="https://openstreetmap.org/copyright">' . __('lychee.OSM_CONTRIBUTORS') . '</a>',
self::OpenStreetMapDe => '&copy; <a href="https://openstreetmap.org/copyright">' . __('lychee.OSM_CONTRIBUTORS') . '</a>',
self::OpenStreetMapFr => '&copy; <a href="https://openstreetmap.org/copyright">' . __('lychee.OSM_CONTRIBUTORS') . '</a>',
self::RRZE => '&copy; <a href="https://openstreetmap.org/copyright">' . __('lychee.OSM_CONTRIBUTORS') . '</a>',
};
}
}
13 changes: 4 additions & 9 deletions app/Http/Requests/Settings/SetMapProviderSettingRequest.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\MapProviders;
use Illuminate\Validation\Rules\Enum;

class SetMapProviderSettingRequest extends AbstractSettingRequest
{
Expand All @@ -11,19 +12,13 @@ class SetMapProviderSettingRequest extends AbstractSettingRequest
public function rules(): array
{
return [
self::ATTRIBUTE => ['required', 'string', Rule::in([
'Wikimedia',
'OpenStreetMap.org',
'OpenStreetMap.de',
'OpenStreetMap.fr',
'RRZE',
])],
self::ATTRIBUTE => ['required', new Enum(MapProviders::class)],
];
}

protected function processValidatedValues(array $values, array $files): void
{
$this->name = self::ATTRIBUTE;
$this->value = $values[self::ATTRIBUTE];
$this->value = MapProviders::from($values[self::ATTRIBUTE]);
}
}
3 changes: 2 additions & 1 deletion app/Http/Resources/ConfigurationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Enum\DefaultAlbumProtectionType;
use App\Enum\ImageOverlayType;
use App\Enum\LicenseType;
use App\Enum\MapProviders;
use App\Enum\ThumbAlbumSubtitleType;
use App\Exceptions\Handler;
use App\Metadata\Versions\InstalledVersion;
Expand Down Expand Up @@ -147,7 +148,7 @@ public function toArray($request): array
'map_display_direction' => Configs::getValueAsString('map_display_direction'),
'map_display_public' => Configs::getValueAsBool('map_display_public'),
'map_include_subalbums' => Configs::getValueAsBool('map_include_subalbums'),
'map_provider' => Configs::getValueAsString('map_provider'),
'map_provider' => Configs::getValueAsEnum('map_provider', MapProviders::class),
'mod_frame_enabled' => Configs::getValueAsBool('mod_frame_enabled'),
'mod_frame_refresh' => Configs::getValueAsInt('mod_frame_refresh'),
'new_photos_notification' => Configs::getValueAsBool('new_photos_notification'),
Expand Down
7 changes: 7 additions & 0 deletions app/Models/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Enum\LicenseType;
use App\Enum\MapProviders;
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\InvalidConfigOption;
use App\Exceptions\Internal\LycheeAssertionError;
Expand Down Expand Up @@ -61,6 +62,7 @@ class Configs extends Model
protected const TERNARY = '0|1|2';
protected const DISABLED = '';
protected const LICENSE = 'license';
protected const MAP_PROVIDER = 'map_provider';

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -134,6 +136,11 @@ public function sanity(?string $candidateValue, ?string $message_template = null
$message = sprintf($message_template, 'a valid license');
}
break;
case self::MAP_PROVIDER:
if (MapProviders::tryFrom($candidateValue) === null) {
$message = sprintf($message_template, 'a valid map provider');
}
break;
default:
$values = explode('|', $this->type_range);
if (!in_array($candidateValue, $values, true)) {
Expand Down
22 changes: 22 additions & 0 deletions database/migrations/2023_10_01_143159_config_map_provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
DB::table('configs')->where('key', '=', 'map_provider')->update(['type_range' => 'map_provider']);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
DB::table('configs')->where('key', '=', 'map_provider')->update(['type_range' => 'Wikimedia|OpenStreetMap.org|OpenStreetMap.de|OpenStreetMap.fr|RRZE']);
}
};

0 comments on commit 6b2b04c

Please sign in to comment.