-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Models, migrations, factories and seeders
- Loading branch information
Showing
40 changed files
with
1,226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Models\Builders; | ||
|
||
use App\Models\Season; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class CompetitionBuilder extends Builder | ||
{ | ||
public function inSeason(Season|string $season): self | ||
{ | ||
if ($season instanceof Season) { | ||
$season = $season->getKey(); | ||
} | ||
|
||
$this->query->where('season_id', $season); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Models\Builders; | ||
|
||
use App\Models\Competition; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class DivisionBuilder extends Builder | ||
{ | ||
public function inCompetition(Competition|string $competition): self | ||
{ | ||
if ($competition instanceof Competition) { | ||
$competition = $competition->getKey(); | ||
} | ||
|
||
$this->query->where('competition_id', $competition); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Models\Builders; | ||
|
||
use App\Models\Division; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class FixtureBuilder extends Builder | ||
{ | ||
public function inDivision(Division|string $division): self | ||
{ | ||
if ($division instanceof Division) { | ||
$division = $division->getKey(); | ||
} | ||
|
||
$this->query->where('division_id', $division); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @property string $name | ||
* @property-read Collection $teams | ||
* @property-read ?Venue $venue | ||
*/ | ||
class Club extends Model | ||
{ | ||
use HasFactory, | ||
HasUuids; | ||
|
||
/** @var array<int, string> */ | ||
protected $fillable = [ | ||
'name', | ||
'venue_id', | ||
]; | ||
|
||
public function teams(): HasMany | ||
{ | ||
return $this->hasMany(Team::class); | ||
} | ||
|
||
public function venue(): BelongsTo | ||
{ | ||
return $this->belongsTo(Venue::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Builders\CompetitionBuilder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @property string $season_id | ||
* @property string $name | ||
* @property-read Season $season | ||
* @property-read Collection $divisions | ||
* | ||
* @method CompetitionBuilder query() | ||
*/ | ||
class Competition extends Model | ||
{ | ||
use HasFactory, | ||
HasUuids; | ||
|
||
/** @var array<int, string> */ | ||
protected $fillable = [ | ||
'season_id', | ||
'name', | ||
]; | ||
|
||
public function newEloquentBuilder($query): CompetitionBuilder | ||
{ | ||
return new CompetitionBuilder($query); | ||
} | ||
|
||
public function season(): BelongsTo | ||
{ | ||
return $this->belongsTo(Season::class); | ||
} | ||
|
||
public function divisions(): HasMany | ||
{ | ||
return $this->hasMany(Division::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Builders\DivisionBuilder; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
/** | ||
* @property string $competition_id | ||
* @property string $name | ||
* @property int $display_order | ||
* @property-read Competition $competition | ||
* @property-read Collection $teams | ||
* @property-read Collection $fixtures | ||
* | ||
* @method DivisionBuilder query() | ||
*/ | ||
class Division extends Model | ||
{ | ||
use HasFactory, | ||
HasUuids, | ||
SoftDeletes; | ||
|
||
/** @var array<int, string> */ | ||
protected $fillable = [ | ||
'competition_id', | ||
'name', | ||
'display_order', | ||
]; | ||
|
||
public function newEloquentBuilder($query): DivisionBuilder | ||
{ | ||
return new DivisionBuilder($query); | ||
} | ||
|
||
public function competition(): BelongsTo | ||
{ | ||
return $this->belongsTo(Competition::class); | ||
} | ||
|
||
public function teams(): BelongsToMany | ||
{ | ||
return $this->belongsToMany(Team::class); | ||
} | ||
|
||
public function fixtures(): HasMany | ||
{ | ||
return $this->hasMany(Fixture::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Builders\FixtureBuilder; | ||
use Carbon\CarbonImmutable; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasOne; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $match_number | ||
* @property string $division_id | ||
* @property string home_team_id | ||
* @property string away_team_id | ||
* @property CarbonImmutable $match_date | ||
* @property-read CarbonImmutable $match_datetime | ||
* @property string venue_id | ||
* @property-read Division $division | ||
* @property-read Team homeTeam | ||
* @property-read Team awayTeam | ||
* @property-read Venue $venue | ||
*/ | ||
class Fixture extends Model | ||
{ | ||
use HasFactory, | ||
HasUuids, | ||
SoftDeletes; | ||
|
||
protected $guarded = [ | ||
'id', | ||
self::CREATED_AT, | ||
self::UPDATED_AT, | ||
'deleted_at', | ||
]; | ||
|
||
protected $casts = [ | ||
'match_date' => 'immutable_date', | ||
]; | ||
|
||
public function newEloquentBuilder($query): Builder | ||
{ | ||
return new FixtureBuilder($query); | ||
} | ||
|
||
protected function matchDatetime(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: function (mixed $value, array $attributes): CarbonImmutable { | ||
/** @var Carbon $matchDatetime */ | ||
$matchDatetime = $attributes['match_date']->copy()->toMutable(); | ||
$matchDatetime->setTimeFrom($attributes['start_time']); | ||
|
||
return $matchDatetime->toImmutable(); | ||
}, | ||
); | ||
} | ||
|
||
protected function startTime(): Attribute | ||
{ | ||
return Attribute::make( | ||
set: function (string|Carbon $value): string { | ||
if ($value instanceof Carbon) { | ||
return $value->format('H:i'); | ||
} | ||
|
||
return $value; | ||
}, | ||
); | ||
} | ||
|
||
public function division(): BelongsTo | ||
{ | ||
return $this->belongsTo(Division::class); | ||
} | ||
|
||
public function homeTeam(): HasOne | ||
{ | ||
return $this->hasOne(related: Team::class, foreignKey: 'id', localKey: 'home_team_id'); | ||
} | ||
|
||
public function awayTeam(): HasOne | ||
{ | ||
return $this->hasOne(related: Team::class, foreignKey: 'id', localKey: 'away_team_id'); | ||
} | ||
|
||
public function venue(): BelongsTo | ||
{ | ||
return $this->belongsTo(Venue::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @property int $year | ||
* @property-read string $name | ||
* @property-read Collection $competitions | ||
*/ | ||
class Season extends Model | ||
{ | ||
use HasFactory, | ||
HasUuids; | ||
|
||
/** @var array<int, string> */ | ||
protected $fillable = [ | ||
'year', | ||
]; | ||
|
||
protected static function booted(): void | ||
{ | ||
static::saving(function (Season $season) { | ||
$season->name = sprintf('%4u/%02u', $season->year, ($season->year + 1) % 100); | ||
}); | ||
} | ||
|
||
public function competitions(): HasMany | ||
{ | ||
return $this->hasMany(Competition::class); | ||
} | ||
} |
Oops, something went wrong.