Skip to content

Commit

Permalink
feat(mapping): VRPs and Mapping Elements (#874)
Browse files Browse the repository at this point in the history
* Create VRP Tables

* Start VRP Data Patch

* VRP Data

* VRP Dependency

* Interface for mapping elements

* Mapping things

* Add mapping dependency

* Mapping dependency tweak

* Style
  • Loading branch information
AndyTWF authored Mar 17, 2022
1 parent 22d4f4d commit 452a336
Show file tree
Hide file tree
Showing 18 changed files with 2,588 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/Helpers/Airfield/MappingElementProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Helpers\Airfield;

use App\Models\Mapping\MappingElement;
use Illuminate\Support\Collection;

interface MappingElementProvider
{
/**
* Returns an array of mapping elements.
*
* @return MappingElement[] | Collection
*/
public function mappingElements(): Collection;
}
43 changes: 43 additions & 0 deletions app/Models/Airfield/VisualReferencePoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Models\Airfield;

use App\Models\Mapping\MappingElement;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Location\Coordinate;

class VisualReferencePoint extends Model implements MappingElement
{
protected $fillable = [
'name',
'short_name',
'latitude',
'longitude',
];

public function airfields(): BelongsToMany
{
return $this->belongsToMany(Airfield::class);
}

public function elementId(): int
{
return $this->id;
}

public function elementType(): string
{
return 'visual_reference_point';
}

public function elementName(): string
{
return $this->name;
}

public function elementCoordinate(): Coordinate
{
return new Coordinate($this->latitude, $this->longitude);
}
}
29 changes: 29 additions & 0 deletions app/Models/Mapping/MappingElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models\Mapping;

use Location\Coordinate;

interface MappingElement
{
/**
* The id that uniquely identifies the element within its
* type.
*/
public function elementId(): int;

/**
* The type of the element. e.g. visual_reference_point
*/
public function elementType(): string;

/**
* The name of the element, for labels etc
*/
public function elementName(): string;

/**
* The coordinate of the element.
*/
public function elementCoordinate(): Coordinate;
}
22 changes: 22 additions & 0 deletions app/Providers/MappingProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Providers;

use App\Services\MappingService;
use App\Services\VrpService;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class MappingProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton(MappingService::class, function (Application $application) {
return new MappingService(
[
$application->make(VrpService::class)
]
);
});
}
}
42 changes: 42 additions & 0 deletions app/Services/MappingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Services;

use App\Helpers\Airfield\MappingElementProvider;
use App\Models\Mapping\MappingElement;

class MappingService
{
/**
* @var MappingElementProvider[]
*/
private array $elementProviders;

public function __construct(array $elementProviders)
{
$this->elementProviders = $elementProviders;
}

public function providers(): array
{
return $this->elementProviders;
}

public function getMappingElementsDependency(): array
{
return collect(
array_map(
fn (MappingElementProvider $provider) => $provider->mappingElements()->map(
fn (MappingElement $element) => [
'id' => $element->elementId(),
'name' => $element->elementName(),
'type' => $element->elementType(),
'latitude' => $element->elementCoordinate()->getLat(),
'longitude' => $element->elementCoordinate()->getLng(),
]
),
$this->elementProviders,
)
)->flatten(1)->toArray();
}
}
25 changes: 25 additions & 0 deletions app/Services/VrpService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Services;

use App\Helpers\Airfield\MappingElementProvider;
use App\Models\Airfield\VisualReferencePoint;
use Illuminate\Support\Collection;

class VrpService implements MappingElementProvider
{
public function getVrpDependency(): array
{
return VisualReferencePoint::all('id', 'name', 'short_name', 'latitude', 'longitude')->map(
fn (VisualReferencePoint $visualReferencePoint) => array_merge(
$visualReferencePoint->toArray(),
['airfields' => $visualReferencePoint->airfields->pluck('id')->toArray()]
)
)->toArray();
}

public function mappingElements(): Collection
{
return VisualReferencePoint::all();
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\HorizonServiceProvider::class,
App\Providers\MappingProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateVisualReferencePointsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('visual_reference_points', function (Blueprint $table) {
$table->id();
$table->string('name')->comment('The name of the VRP');
$table->string('short_name')->comment('The short name of the VRP');
$table->double('latitude', 10, 8)
->comment('The latitude of the VRP in decimal degrees');
$table->double('longitude', 11, 8)
->comment('The longitude of the VRP in decimal degrees');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('visual_reference_points');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use App\Models\Airfield\Airfield;
use App\Models\Airfield\VisualReferencePoint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAirfieldVisualReferencePointTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('airfield_visual_reference_point', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('airfield_id');
$table->foreignIdFor(VisualReferencePoint::class);

$table->foreign('airfield_id')->references('id')->on('airfield');
$table->foreign('visual_reference_point_id', 'airfield_visual_reference_vrp')->references('id')->on('visual_reference_points');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('airfield_visual_reference_point');
}
}
Loading

0 comments on commit 452a336

Please sign in to comment.