Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/better seeding #7

Merged
merged 11 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .bladeformatterrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"endOfLine": "LF",
"endWithNewLine": true,
"extraLiners": [],
"indentInnerHtml": true,
"indentSize": 4,
"noMultipleEmptyLines": true,
"noSingleQuote": false,
"noTrailingCommaPhp": false,
"progress": true,
"sortHtmlAttributes": "code-guide",
"sortTailwindcssClasses": true,
"useTabs": false,
"wrapAttributes": "force-aligned",
"wrapAttributesMinAttrs": 1,
"wrapLineLength": 120
}
8 changes: 7 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PER-CS2.0' => true,
'@PhpCsFixer' => true
'@PhpCsFixer' => true,
'@PHP84Migration' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true
],
])
->setFinder($finder)
;
18 changes: 9 additions & 9 deletions _docker/plugins/default-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function loginFormField(string $name, string $envValue = '', string $valu
$nameAttr = "auth[{$name}]";

return <<<HTML
<tr>
<td>
{$name}
</td>
<td>
<input name="{$nameAttr}" value="{$inputValue}" />
</td>
</tr>
HTML;
<tr>
<td>
{$name}
</td>
<td>
<input name="{$nameAttr}" value="{$inputValue}" />
</td>
</tr>
HTML;
}
}

Expand Down
77 changes: 77 additions & 0 deletions app/Casts/ColourArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;

/**
* @implements CastsAttributes<array<int, string>, array<int, string|array<int|string>>>
*/
class ColourArray implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return json_decode($value, true);
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
if (is_null($value)) {
return json_encode([]);
}

if (!is_array($value)) {
throw new InvalidArgumentException('Expected an array');
}

return json_encode(array_map(fn ($item) => $this->transformColour($item), $value));
}

/**
* @param array<int|string, int|string>|string $colour
*/
private function transformColour(array|string $colour): string
{
if (is_array($colour)) {
if (3 !== count($colour)) {
throw new InvalidArgumentException('Invalid colour array');
}

$returnVal = '#';

$colours = [
'red' => intval($colour['red'] ?? $colour['r'] ?? $colour[0]),
'green' => intval($colour['green'] ?? $colour['g'] ?? $colour[1]),
'blue' => intval($colour['blue'] ?? $colour['b'] ?? $colour[2]),
];

foreach ($colours as $colourKey => $colourValue) {
if ($colourValue < 0 || $colourValue > 255) {
throw new InvalidArgumentException("Invalid colour value - {$colourKey} not in range (0, 255)");
}

$returnVal .= str_pad(dechex($colourValue), 2, '0', STR_PAD_LEFT);
}
} else {
if (preg_match('/^\d{6}$/', $colour)) {
$returnVal = "#{$colour}";
} else {
$returnVal = $colour;
}
}

return strtoupper($returnVal);
}
}
44 changes: 39 additions & 5 deletions app/Models/Competition.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace App\Models;

use Database\Factories\CompetitionFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Competition extends Model
{
/** @use HasFactory<CompetitionFactory> */
Expand All @@ -20,14 +24,44 @@ class Competition extends Model
*
* @var array<int, string>
*/
protected $fillable = [];
protected $fillable = [
'name',
'description',
'parent_id',
'sport_id',
];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
* @return BelongsTo<self, self>
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}

/**
* @return HasMany<self>
*/
protected $with = [];
public function children(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}

/**
* @return BelongsTo<Sport, self>
*/
public function sport(): BelongsTo
{
return $this->belongsTo(Sport::class);
}

/**
* @return HasMany<Game>
*/
public function games(): HasMany
{
return $this->hasMany(Game::class);
}

/**
* Get the attributes that should be cast.
Expand Down
13 changes: 2 additions & 11 deletions app/Models/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace App\Models;

use Database\Factories\GameFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Game extends Model
{
/** @use HasFactory<GameFactory> */
Expand All @@ -23,17 +25,6 @@ class Game extends Model
*/
protected $fillable = [];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
*/
protected $with = [
'team1',
'team2',
'competition',
];

/**
* @return BelongsTo<Team, self>
*/
Expand Down
11 changes: 2 additions & 9 deletions app/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace App\Models;

use Database\Factories\PersonFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Person extends Model
{
/** @use HasFactory<PersonFactory> */
Expand All @@ -28,15 +30,6 @@ class Person extends Model
'user_id',
];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
*/
protected $with = [
'user',
];

/**
* @return BelongsTo<User, self>
*/
Expand Down
11 changes: 2 additions & 9 deletions app/Models/Position.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace App\Models;

use Database\Factories\PositionFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Position extends Model
{
/** @use HasFactory<PositionFactory> */
Expand All @@ -23,15 +25,6 @@ class Position extends Model
*/
protected $fillable = [];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
*/
protected $with = [
'sport',
];

/**
* @return BelongsTo<Sport, self>
*/
Expand Down
12 changes: 8 additions & 4 deletions app/Models/Sport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace App\Models;

use Database\Factories\SportFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Sport extends Model
{
/** @use HasFactory<SportFactory> */
Expand All @@ -23,11 +26,12 @@ class Sport extends Model
protected $fillable = [];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
* @return HasMany<Competition>
*/
protected $with = [];
public function competitions(): HasMany
{
return $this->hasMany(Competition::class);
}

/**
* Get the attributes that should be cast.
Expand Down
12 changes: 4 additions & 8 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace App\Models;

use App\Casts\ColourArray;
use Database\Factories\TeamFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/** @mixin Builder */
class Team extends Model
{
/** @use HasFactory<TeamFactory> */
Expand All @@ -22,13 +25,6 @@ class Team extends Model
*/
protected $fillable = [];

/**
* The relations to eager load on every query.
*
* @var array<int, string>
*/
protected $with = [];

/**
* Get the attributes that should be cast.
*
Expand All @@ -37,7 +33,7 @@ class Team extends Model
protected function casts(): array
{
return [
'colours' => 'array',
'colours' => ColourArray::class,
];
}
}
11 changes: 11 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace App\Models;

use Database\Factories\UserFactory;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/** @mixin Builder */
class User extends Authenticatable
{
/** @use HasFactory<UserFactory> */
Expand Down Expand Up @@ -38,6 +41,14 @@ class User extends Authenticatable
'remember_token',
];

/**
* @return HasOne<Person>
*/
public function person(): HasOne
{
return $this->hasOne(Person::class);
}

/**
* Get the attributes that should be cast.
*
Expand Down
Loading
Loading