Skip to content

Commit 138d952

Browse files
committed
create models
1 parent 40e4dba commit 138d952

4 files changed

Lines changed: 123 additions & 108 deletions

File tree

app/Models/Colour.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php
22

3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
39
namespace App\Models;
410

511
use Illuminate\Database\Eloquent\Model;
@@ -16,20 +22,20 @@ class Colour extends Model
1622
{
1723
public $timestamps = false;
1824

19-
protected $fillable = [
20-
'id',
21-
'R',
22-
'G',
23-
'B',
24-
];
25+
protected $fillable = [
26+
'id',
27+
'R',
28+
'G',
29+
'B',
30+
];
2531

26-
/**
27-
* Convert the colour to a hexadecimal string.
28-
*
29-
* @return string
30-
*/
31-
public function toHex(): string
32-
{
33-
return sprintf('#%02x%02x%02x', $this->R, $this->G, $this->B);
34-
}
32+
/**
33+
* Convert the colour to a hexadecimal string.
34+
*
35+
* @return string
36+
*/
37+
public function toHex(): string
38+
{
39+
return sprintf('#%02x%02x%02x', $this->R, $this->G, $this->B);
40+
}
3541
}

app/Models/Palette.php

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,50 @@
1010

1111
use Illuminate\Database\Eloquent\Model;
1212

13-
1413
/**
1514
* App\Models\Palette.
1615
*
17-
* @property int $id
18-
* @property string $photo_id
19-
* @property int $colour_1
20-
* @property int $colour_2
21-
* @property int $colour_3
22-
* @property int $colour_4
23-
* @property int $colour_5
16+
* @property int $id
17+
* @property string $photo_id
18+
* @property int $colour_1
19+
* @property int $colour_2
20+
* @property int $colour_3
21+
* @property int $colour_4
22+
* @property int $colour_5
2423
*/
2524
class Palette extends Model
2625
{
2726
public $timestamps = false;
2827

29-
protected $fillable = [
30-
'photo_id',
31-
'colour_1',
32-
'colour_2',
33-
'colour_3',
34-
'colour_4',
35-
'colour_5',
36-
];
37-
38-
/**
39-
* @return array{colour_1:string,colour_2:string,colour_3:string,colour_4:string,colour_5:string}
40-
*/
41-
public function toHexColours(): array
42-
{
43-
return [
44-
'colour_1' => self::toHex($this->colour_1),
45-
'colour_2' => self::toHex($this->colour_2),
46-
'colour_3' => self::toHex($this->colour_3),
47-
'colour_4' => self::toHex($this->colour_4),
48-
'colour_5' => self::toHex($this->colour_5),
49-
];
50-
}
51-
52-
static function toHex(int $colour)
53-
{
54-
$b = $colour & 0xFF; // Extract the blue component
55-
$g = ($colour >> 8) & 0xFF; // Extract the green component
56-
$r = ($colour >> 16) & 0xFF; // Extract the red component
57-
58-
return sprintf('#%02x%02x%02x', $r, $g, $b);
59-
}
28+
protected $fillable = [
29+
'photo_id',
30+
'colour_1',
31+
'colour_2',
32+
'colour_3',
33+
'colour_4',
34+
'colour_5',
35+
];
36+
37+
/**
38+
* @return array{colour_1:string,colour_2:string,colour_3:string,colour_4:string,colour_5:string}
39+
*/
40+
public function toHexColours(): array
41+
{
42+
return [
43+
'colour_1' => self::toHex($this->colour_1),
44+
'colour_2' => self::toHex($this->colour_2),
45+
'colour_3' => self::toHex($this->colour_3),
46+
'colour_4' => self::toHex($this->colour_4),
47+
'colour_5' => self::toHex($this->colour_5),
48+
];
49+
}
50+
51+
public static function toHex(int $colour)
52+
{
53+
$b = $colour & 0xFF; // Extract the blue component
54+
$g = ($colour >> 8) & 0xFF; // Extract the green component
55+
$r = ($colour >> 16) & 0xFF; // Extract the red component
56+
57+
return sprintf('#%02x%02x%02x', $r, $g, $b);
58+
}
6059
}
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
<?php
22

3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
39
use Illuminate\Database\Migrations\Migration;
410
use Illuminate\Database\Schema\Blueprint;
511
use Illuminate\Support\Facades\Schema;
612

7-
return new class extends Migration
8-
{
9-
/**
10-
* Run the migrations.
11-
*/
12-
public function up(): void
13-
{
14-
Schema::create('colours', function (Blueprint $table) {
15-
$table->unsignedMediumInteger('id')->primary();
16-
$table->tinyInteger('R')->default(0);
17-
$table->tinyInteger('G')->default(0);
18-
$table->tinyInteger('B')->default(0);
19-
$table->unique(['R', 'G', 'B']); // Ensure that the combination of R, G, and B is unique
20-
});
21-
}
13+
return new class() extends Migration {
14+
/**
15+
* Run the migrations.
16+
*/
17+
public function up(): void
18+
{
19+
Schema::create('colours', function (Blueprint $table) {
20+
$table->unsignedMediumInteger('id')->primary();
21+
$table->tinyInteger('R')->default(0);
22+
$table->tinyInteger('G')->default(0);
23+
$table->tinyInteger('B')->default(0);
24+
$table->unique(['R', 'G', 'B']); // Ensure that the combination of R, G, and B is unique
25+
});
26+
}
2227

23-
/**
24-
* Reverse the migrations.
25-
*/
26-
public function down(): void
27-
{
28-
Schema::dropIfExists('colours');
29-
}
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
Schema::dropIfExists('colours');
34+
}
3035
};
Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
<?php
22

3+
/**
4+
* SPDX-License-Identifier: MIT
5+
* Copyright (c) 2017-2018 Tobias Reich
6+
* Copyright (c) 2018-2025 LycheeOrg.
7+
*/
8+
39
use Illuminate\Database\Migrations\Migration;
410
use Illuminate\Database\Schema\Blueprint;
511
use Illuminate\Support\Facades\Schema;
612

7-
return new class extends Migration
8-
{
9-
const PHOTO_ID = 'photo_id';
10-
const RANDOM_ID_LENGTH = 24; // Length of the random ID
13+
return new class() extends Migration {
14+
public const PHOTO_ID = 'photo_id';
15+
public const RANDOM_ID_LENGTH = 24; // Length of the random ID
1116

12-
/**
13-
* Run the migrations.
14-
*/
15-
public function up(): void
16-
{
17-
Schema::create('palettes', function (Blueprint $table) {
18-
$table->id();
17+
/**
18+
* Run the migrations.
19+
*/
20+
public function up(): void
21+
{
22+
Schema::create('palettes', function (Blueprint $table) {
23+
$table->id();
1924
$table->char(self::PHOTO_ID, self::RANDOM_ID_LENGTH)->nullable(false);
20-
$table->unsignedMediumInteger('colour_1')->nullable(false);
21-
$table->unsignedMediumInteger('colour_2')->nullable(false);
22-
$table->unsignedMediumInteger('colour_3')->nullable(false);
23-
$table->unsignedMediumInteger('colour_4')->nullable(false);
24-
$table->unsignedMediumInteger('colour_5')->nullable(false);
25-
26-
$table->index('id');
25+
$table->unsignedMediumInteger('colour_1')->nullable(false);
26+
$table->unsignedMediumInteger('colour_2')->nullable(false);
27+
$table->unsignedMediumInteger('colour_3')->nullable(false);
28+
$table->unsignedMediumInteger('colour_4')->nullable(false);
29+
$table->unsignedMediumInteger('colour_5')->nullable(false);
30+
31+
$table->index('id');
2732
$table->index([self::PHOTO_ID]);
28-
$table->foreign('colour_1')->references('id')->on('colours');
29-
$table->foreign('colour_2')->references('id')->on('colours');
30-
$table->foreign('colour_3')->references('id')->on('colours');
31-
$table->foreign('colour_4')->references('id')->on('colours');
32-
$table->foreign('colour_5')->references('id')->on('colours');
33-
});
34-
}
33+
$table->foreign('colour_1')->references('id')->on('colours');
34+
$table->foreign('colour_2')->references('id')->on('colours');
35+
$table->foreign('colour_3')->references('id')->on('colours');
36+
$table->foreign('colour_4')->references('id')->on('colours');
37+
$table->foreign('colour_5')->references('id')->on('colours');
38+
});
39+
}
3540

36-
/**
37-
* Reverse the migrations.
38-
*/
39-
public function down(): void
40-
{
41-
Schema::dropIfExists('palettes');
42-
}
41+
/**
42+
* Reverse the migrations.
43+
*/
44+
public function down(): void
45+
{
46+
Schema::dropIfExists('palettes');
47+
}
4348
};

0 commit comments

Comments
 (0)