ldc-boutique-cnc/app/Models/XmlInco.php
2022-11-08 15:41:10 +01:00

151 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\DefaultProductType;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Collection;
class XmlInco extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'no_art',
'nom',
'prod_saison',
'allegations',
'conseil_prepa',
'astuces',
'descriptif',
'arg_vente',
'nb_portion',
'typ_portion',
'pds_moy_theo',
'lib_conso',
'lst_ing',
'lib_allergene',
'nutritional_values',
'pictures',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'pictures' => 'array',
'nutritional_values' => 'array',
];
/**
* The Laravel disk name
*
* @var string
*/
protected static $disk = 'inco';
/**
* Storage path
*
* @var string
*/
protected static $imagesPath = 'images';
// * METHODS
/**
* Get xml inco picture path
*
* @param string $pictureName
* @return null|string
*/
public static function getXmlIncoPicturePath(string $pictureName): ?string
{
$disk = static::getDisk();
$picturePath = static::$imagesPath . "/{$pictureName}";
return $disk->exists($picturePath) ? $picturePath : null;
}
/**
* Getter media list retrieve a collection of medias
*
* @return \Illuminate\Support\Collection<array<string,string> Indexes are filename and imgdata.
*/
public function getMediaListAttribute(): Collection
{
$disk = static::getDisk();
/** @var \Illuminate\Support\Collection<array<string,string> */
$collFiles = new Collection();
foreach ($this->pictures as $pictureName) {
$collFiles->add([
'filename' => $pictureName,
'imgdata' => base64_encode($disk->get(
static::$imagesPath . "/{$pictureName}"
))
]);
}
return $collFiles;
}
/**
* Get the concerned disk
*
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public static function getDisk(): Filesystem
{
return Storage::disk(static::$disk);
}
/**
* Getter nice_string attribute
*
* @return string
*/
public function getNiceStringAttribute()
{
return sprintf('%s - %s', $this->id, $this->descriptif);
}
/**
* Translate Data to compatible minicrud inputs for product
*
* @return array<string,string>
* @throws \RuntimeException If a product minicrud input is not handled for translation.
*/
public function translateToProductIncoInputs(): array
{
return \collect(DefaultProductType::incoInputs())->keys()
->mapWithKeys(function (string $key) {
switch ($key) {
case 'ingredients':
return [$key => \strval($this->lst_ing)];
case 'preparation_advices':
return [$key => \strval($this->conseil_prepa)];
case 'allergens':
return [$key => \strval($this->lib_allergene)];
case 'selling_arguments':
return [$key => \strval($this->arg_vente)];
case 'servings':
return [$key => \strval($this->nb_portion)];
case 'servings_type':
return [$key => \strval($this->typ_portion)];
default:
throw new \RuntimeException('Unhandled product minicrud input');
}
})->all();
}
}