From 5c58dfe1aec5b4c224b8c7f5793c2228f9efabd2 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 12 Feb 2018 20:58:23 -0600 Subject: [PATCH] generate a hex code for an aircraft on creation #33 --- app/Database/factories/AircraftFactory.php | 1 + app/Models/Aircraft.php | 7 +++++++ app/Support/ICAO.php | 11 +++++++++++ resources/views/admin/aircraft/fields.blade.php | 2 +- tests/UtilsTest.php | 6 ++++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/Database/factories/AircraftFactory.php b/app/Database/factories/AircraftFactory.php index 6ef50057..22cb3d9a 100644 --- a/app/Database/factories/AircraftFactory.php +++ b/app/Database/factories/AircraftFactory.php @@ -14,6 +14,7 @@ $factory->define(App\Models\Aircraft::class, function (Faker $faker) { 'icao' => $faker->unique()->text(5), 'name' => $faker->unique()->text(50), 'registration' => $faker->unique()->text(10), + 'hex_code' => \App\Support\ICAO::createHexCode(), 'active' => true, 'created_at' => $faker->dateTimeBetween('-1 week', 'now'), 'updated_at' => function (array $pirep) { diff --git a/app/Models/Aircraft.php b/app/Models/Aircraft.php index 3ab0eb3a..b75be6a8 100644 --- a/app/Models/Aircraft.php +++ b/app/Models/Aircraft.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Support\ICAO; + class Aircraft extends BaseModel { public $table = 'aircraft'; @@ -12,6 +14,7 @@ class Aircraft extends BaseModel 'name', 'icao', 'registration', + 'hex_code', 'zfw', 'active', ]; @@ -46,6 +49,10 @@ class Aircraft extends BaseModel if (!empty($model->icao)) { $model->icao = strtoupper(trim($model->icao)); } + + if(empty($model->hex_code)) { + $model->hex_code = ICAO::createHexCode(); + } }); } diff --git a/app/Support/ICAO.php b/app/Support/ICAO.php index e5349b5a..6cc92b68 100644 --- a/app/Support/ICAO.php +++ b/app/Support/ICAO.php @@ -9,4 +9,15 @@ namespace App\Support; class ICAO { + /** + * Create a random hex code. Eventually this may follow the format in: + * ICAO Aeronautical Telecommunications, Annex 10, Vol. III, chapter 9 + * @param null $country + * @return string + */ + public static function createHexCode($country=null) + { + $bytes = random_bytes(4); + return bin2hex($bytes); + } } diff --git a/resources/views/admin/aircraft/fields.blade.php b/resources/views/admin/aircraft/fields.blade.php index cafc24e9..ee820837 100644 --- a/resources/views/admin/aircraft/fields.blade.php +++ b/resources/views/admin/aircraft/fields.blade.php @@ -29,7 +29,7 @@
{!! Form::label('registration', 'Registration:') !!} -

Enter the registration without the country prefix

+ {{--

Enter the registration with the country prefix

--}} {!! Form::text('registration', null, ['class' => 'form-control']) !!}

{{ $errors->first('registration') }}

diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index c1ed7823..26c5efab 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -57,4 +57,10 @@ class UtilsTest extends TestCase $api_key = Utils::generateApiKey(); $this->assertNotNull($api_key); } + + public function testHexCode() + { + $hex_code = \App\Support\ICAO::createHexCode(); + $this->assertNotNull($hex_code); + } }