generate a hex code for an aircraft on creation #33

This commit is contained in:
Nabeel Shahzad 2018-02-12 20:58:23 -06:00
parent 2678514077
commit 5c58dfe1ae
5 changed files with 26 additions and 1 deletions

View File

@ -14,6 +14,7 @@ $factory->define(App\Models\Aircraft::class, function (Faker $faker) {
'icao' => $faker->unique()->text(5), 'icao' => $faker->unique()->text(5),
'name' => $faker->unique()->text(50), 'name' => $faker->unique()->text(50),
'registration' => $faker->unique()->text(10), 'registration' => $faker->unique()->text(10),
'hex_code' => \App\Support\ICAO::createHexCode(),
'active' => true, 'active' => true,
'created_at' => $faker->dateTimeBetween('-1 week', 'now'), 'created_at' => $faker->dateTimeBetween('-1 week', 'now'),
'updated_at' => function (array $pirep) { 'updated_at' => function (array $pirep) {

View File

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use App\Support\ICAO;
class Aircraft extends BaseModel class Aircraft extends BaseModel
{ {
public $table = 'aircraft'; public $table = 'aircraft';
@ -12,6 +14,7 @@ class Aircraft extends BaseModel
'name', 'name',
'icao', 'icao',
'registration', 'registration',
'hex_code',
'zfw', 'zfw',
'active', 'active',
]; ];
@ -46,6 +49,10 @@ class Aircraft extends BaseModel
if (!empty($model->icao)) { if (!empty($model->icao)) {
$model->icao = strtoupper(trim($model->icao)); $model->icao = strtoupper(trim($model->icao));
} }
if(empty($model->hex_code)) {
$model->hex_code = ICAO::createHexCode();
}
}); });
} }

View File

@ -9,4 +9,15 @@ namespace App\Support;
class ICAO 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);
}
} }

View File

@ -29,7 +29,7 @@
<!-- Registration Field --> <!-- Registration Field -->
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
{!! Form::label('registration', 'Registration:') !!} {!! Form::label('registration', 'Registration:') !!}
<p class="text-success small">Enter the registration without the country prefix</p> {{--<p class="text-success small">Enter the registration with the country prefix</p>--}}
{!! Form::text('registration', null, ['class' => 'form-control']) !!} {!! Form::text('registration', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('registration') }}</p> <p class="text-danger">{{ $errors->first('registration') }}</p>
</div> </div>

View File

@ -57,4 +57,10 @@ class UtilsTest extends TestCase
$api_key = Utils::generateApiKey(); $api_key = Utils::generateApiKey();
$this->assertNotNull($api_key); $this->assertNotNull($api_key);
} }
public function testHexCode()
{
$hex_code = \App\Support\ICAO::createHexCode();
$this->assertNotNull($hex_code);
}
} }