ldc-ovoteam/app/Models/Product.php

196 lines
7.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Models;
use App\Casts\NutritionalValuesCast;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\Products\BreedingMethod;
use Domain\Models\ValueObjects\Products\Business;
use Domain\Models\ValueObjects\Products\ProductCategory;
use Domain\Models\ValueObjects\Products\ProductFormat;
use Domain\Models\ValueObjects\Products\Technology;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
use Ramsey\Uuid\Uuid;
use Spatie\Translatable\HasTranslations;
use Uwil\LaravelBackend\Interfaces\TranslatableModel;
use Uwil\LaravelBackend\Traits\Models\HasActivityLog;
use Uwil\LaravelBackend\Traits\Models\HasCache;
use Uwil\LaravelBackend\Traits\Models\HasModelNaming;
use Uwil\LaravelBackend\Traits\Models\HasUwilImages;
2026-09-25 13:25:27 +02:00
// phpcs:disable Generic.Files.LineLength.TooLong
/**
2026-09-25 13:25:27 +02:00
* @property string $id
* @property string $seo_title
* @property string $seo_description
* @property string $picture
* @property string $picture_alt
* @property string $picture_title
* @property string $name
* @property string $slug
* @property ProductCategory $category Domain\Models\ValueObjects\Products\ProductRange
* @property Technology $technology Domain\Models\ValueObjects\Products\Technology
* @property bool $is_egalim
* @property \Illuminate\Support\Collection<int, BreedingMethod> $breeding_methods Array of Domain\Models\ValueObjects\Products\BreedingMethod
* @property \Illuminate\Support\Collection<int, Business> $businesses Array of Domain\Models\ValueObjects\Products\Business
* @property \Illuminate\Support\Collection<int, ProductFormat> $formats Array of Domain\Models\ValueObjects\Products\ProductFormat
* @property string $description
2026-09-25 13:25:27 +02:00
* @property string $details_and_advantages
* @property string $handling_advices
* @property string|null $ingredients
* @property string $conservation_before_opening
* @property string|null $conservation_after_opening
* @property \Domain\Models\ValueObjects\Products\NutritionalValues|null $nutritional_values Domain\Models\ValueObjects\NutritionalValues
* @property string|null $pdf
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Uwil\LaravelBackend\Models\ActivityLog> $activityLogs
* @property-read array $translatable_columns_from
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Recipe> $recipes
* @property-read mixed $translations
*
* @method static \Database\Factories\ProductFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product onlyTrashed()
2026-09-21 13:03:16 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product whereJsonContainsLocale(string $column, string $locale, ?mixed $value, string $operand = '=')
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product whereJsonContainsLocales(string $column, array $locales, ?mixed $value, string $operand = '=')
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product whereLocale(string $column, string $locale)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product whereLocales(string $column, array $locales)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product withTrashed(bool $withTrashed = true)
* @method static \Illuminate\Database\Eloquent\Builder<static>|Product withoutTrashed()
*
* @mixin \Eloquent
*/
2026-09-25 13:25:27 +02:00
// phpcs:enable Generic.Files.LineLength.TooLong
class Product extends Model implements TranslatableModel
{
use HasActivityLog;
use HasCache;
/** @use HasFactory<\Database\Factories\ProductFactory> */
use HasFactory;
use HasModelNaming;
use HasTranslations;
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
use HasUuids;
use HasUwilImages;
use Searchable;
use SoftDeletes;
protected $fillable = [
'seo_title',
'seo_description',
'picture',
'picture_alt',
'picture_title',
'name',
'category',
'technology',
'is_egalim',
'breeding_methods',
'businesses',
'formats',
'description',
'details_and_advantages',
'handling_advices',
'ingredients',
'conservation_before_opening',
'conservation_after_opening',
'nutritional_values',
'pdf',
];
protected $casts = [
'is_egalim' => 'boolean',
'category' => ProductCategory::class,
'technology' => Technology::class,
'breeding_methods' => AsEnumCollection::class . ':' . BreedingMethod::class,
'businesses' => AsEnumCollection::class . ':' . Business::class,
'formats' => AsEnumCollection::class . ':' . ProductFormat::class,
'nutritional_values' => NutritionalValuesCast::class,
];
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
public function newUniqueId(): string
{
return Uuid::uuid7()->toString();
}
public function getId(): ProductId
{
2026-09-25 13:25:27 +02:00
return new ProductId(Uuid::fromString(strval($this->id)));
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
}
#[\Override]
protected static function booted(): void
{
static::saving(function (self $model): void {
self::manageFields($model);
});
static::deleting(function (self $model): void {
FileStorageHelper::removeFile($model, 'picture');
FileStorageHelper::removeFile($model, 'pdf');
});
}
/** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\App\Models\Recipe,$this> */
public function recipes(): BelongsToMany
{
return $this->belongsToMany(Recipe::class);
}
/**
* Relations should be share to views.
*
* @return array<int,string>
*/
public static function getPropsRelations(): array
{
return self::getCacheRelations();
}
private static function manageFields(self $model): void
{
self::assertNameIsUnique($model->name, $model->id);
$model->slug = Str::slug($model->name);
$model->picture = FileStorageHelper::storeFile($model, $model->picture, true);
if ($model->pdf) {
$model->pdf = FileStorageHelper::storeFile($model, $model->pdf, true);
}
}
feat(directory): update ProductDirectory to use ProductId - Changed the find method to accept ProductId instead of int. - Added findBySlug method to retrieve products by slug. - Updated getCategoryProducts method to return CategoryProducts type. feat(eloquent): implement findBySlug in EloquentProductDirectory - Implemented findBySlug method to retrieve products by slug. - Updated find method to handle ProductId. feat(dto): enhance CategoryProducts to serialize product IDs - Updated CategoryProducts to serialize product IDs as strings. feat(controller): modify ProductController to handle search limits - Set limitPerCategory to -1 for search functionality. refactor(model): change Product model ID to UUID - Changed the id field in Product model to UUID. - Updated related methods to handle UUIDs. feat(migration): alter products and recipes tables for UUIDs - Updated migrations to use UUIDs for primary keys in products and recipes tables. - Adjusted foreign key constraints in product_recipe table. feat(route): add route model binding for products - Implemented route model binding for products using ProductId and slug. chore(seed): remove CSV import calls from MandatorySeeder - Removed calls to ImportProductsCsvSeeder and ImportRecipesCsvSeeder. feat(entity): create ProductId and RecipeId value objects - Added ProductId and RecipeId classes to encapsulate UUIDs. feat(frontend): update TypeScript interfaces for products - Updated TypeScript interfaces to reflect changes in product structure.
2026-07-08 16:32:12 +02:00
private static function assertNameIsUnique(?string $name, string | int | null $id = null): void
{
$table = new self()->getTable();
ToolboxHelper::assertFieldIsUnique($table, 'name', $name, $id);
ToolboxHelper::assertFieldIsUnique($table, 'slug', Str::slug($name ?? ''), $id);
}
/**
* Get the indexable data array for the model.
*
* @return array<string, mixed>
*/
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'category' => $this->category?->value,
];
}
}