ldc-boutique-cnc/app/Models/XmlInco.php
2022-12-02 10:37:01 +01:00

316 lines
10 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Product\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();
}
/**
* Fetch nutritional Values
*
* @return object|null
*/
public function fetchIncoNutritionalValues(): ?object
{
$nutri_values = $this->nutritional_values;
if (empty($nutri_values)) {
return null;
}
$nutri_values = \collect($nutri_values);
return (object)[
// Pour la ref.
'nv_ref_weight_g' => $nutri_values->get('taille_ref'),
// Energie Kj.
'nv_kj' => $nutri_values->get('val_energ_kj_r'),
// Energie Kcal.
'nv_kcal' => $nutri_values->get('val_energ_kcal_r'),
// Graisses.
'nv_fat_g' => $nutri_values->get('mat_grass_r'),
// Dont acides gras saturés.
'nv_sat_fat_g' => $nutri_values->get('ags_r'),
// Glucides.
'nv_carb_g' => $nutri_values->get('glucides_r'),
// Dont sucres.
'nv_sugar_g' => $nutri_values->get('sucre_r'),
// Fibres alimentaires.
'nv_fiber_g' => $nutri_values->get('fib_alim_r'),
// Protéines.
'nv_protein_g' => $nutri_values->get('proteine_r'),
// Sel.
'nv_salt_g' => $nutri_values->get('sel_r'),
/** Autres valeurs dispos
* $nutri_values["sodium_r"];
* $nutri_values["ener_e14_r"];
* $nutri_values["fruits_r"];
* $nutri_values["fruits_coque_sech_r"];
* $nutri_values["ener_kjo_r"];
* $nutri_values["fatsat_r"];
* $nutri_values["choval_r"];
* $nutri_values["sugar_r"];
* $nutri_values["fibtg_r"];
* $nutri_values["pro_r"];
* $nutri_values["salteq_r"];
*/
// * Valeurs nutritionnelles par portion.
// Taille portion en grammes.
'nv_serving_size_g' => $nutri_values->get('taille_port'),
// Energie Kj.
'nv_serving_kj' => $nutri_values->get('val_energ_kj_p'),
// Energie Kcal.
'nv_serving_kcal' => $nutri_values->get('val_energ_kcal_p'),
// Graisses.
'nv_serving_fat_g' => $nutri_values->get('mat_grass_p'),
// Dont acides gras saturés.
'nv_serving_sat_fat_g' => $nutri_values->get('ags_p'),
// Glucides.
'nv_serving_carb_g' => $nutri_values->get('glucides_p'),
// Dont sucres.
'nv_serving_sugar_g' => $nutri_values->get('sucre_p'),
// Fibres alimentaires.
'nv_serving_fiber_g' => $nutri_values->get('fib_alim_p'),
// Protéines.
'nv_serving_protein_g' => $nutri_values->get('proteine_p'),
// Sel.
'nv_serving_salt_g' => $nutri_values->get('sel_p'),
/**
* autres valeurs disponibles
* $nutri_values["sodium_p"];
* $nutri_values["fruits_p"];
*/
// * Apports journaliers.
// Energie % recommandé.
'nv_daily_energy' => $nutri_values->get('pourcent_apport_ref_kjo_r'),
// Graisses.
'nv_daily_fat' => $nutri_values->get('pourcent_apport_ref_fat_r'),
// Dont acides gras saturés.
'nv_daily_sat_fat' => $nutri_values->get('pourcent_apport_ref_fatsat_r'),
// Glucides.
'nv_daily_carb' => $nutri_values->get('pourcent_apport_ref_choval_r'),
// Dont sucres.
'nv_daily_sugar' => $nutri_values->get('pourcent_apport_ref_sugar_r'),
// Fibres alimentaires.
'nv_daily_fiber' => $nutri_values->get('pourcent_apport_ref_fibtg_r'),
// Protéines.
'nv_daily_protein' => $nutri_values->get('pourcent_apport_ref_pro_r'),
// Sel.
'nv_daily_salt' => $nutri_values->get('pourcent_apport_ref_salteq_r'),
/**
* Autre valeur dispo
* $nutri_values["pourcent_apport_ref_e14_r"]
*/
// * Nutriscore.
'nutriscore' => $nutri_values->get('classement'),
'nutriscore_args' => '',
/**
* Infos n'ayant pas de mapping
*
* $nutri_values["val_energ_kj_nut"];
* $nutri_values["val_energ_kcal_nut"];
* $nutri_values["mat_grass_nut"];
* $nutri_values["ags_nut"];
* $nutri_values["glucides_nut"];
* $nutri_values["sucre_nut"];
* $nutri_values["sucre_r"];
* $nutri_values["fib_alim_nut"];
* $nutri_values["proteine_nut"];
* $nutri_values["sel_nut"];
* $nutri_values["apport_ref"];
* $nutri_values["fruits_coque_sech_p"];
* $nutri_values["ener_e14_p"];
* $nutri_values["pourcent_apport_ref_e14_p"];
* $nutri_values["ener_kjo_p"];
* $nutri_values["pourcent_apport_ref_kjo_p"];
* $nutri_values["fat_p"];
* $nutri_values["pourcent_apport_ref_fat_p"];
* $nutri_values["fatsat_p"];
* $nutri_values["pourcent_apport_ref_fatsat_p"];
* $nutri_values["choval_p"];
* $nutri_values["pourcent_apport_ref_choval_p"];
* $nutri_values["sugar_p"];
* $nutri_values["pourcent_apport_ref_sugar_p"];
* $nutri_values["fibtg_p"];
* $nutri_values["pourcent_apport_ref_fibtg_p"];
* $nutri_values["pro_p"];
* $nutri_values["pourcent_apport_ref_pro_p"];
* $nutri_values["salteq_p"];
* $nutri_values["pourcent_apport_ref_salteq_p"];
* $nutri_values["apport_port"];
* $nutri_values["part_p"];
* $nutri_values["part_g"];
* $nutri_values["part_l"];
* $nutri_values["p_sur_l"];
* $nutri_values["pourcent_ags"];
* $nutri_values["pourcent_agi"];
* $nutri_values["pourcent_ss"];
* $nutri_values["pourcent_amidon"];
* $nutri_values["composante_n"];
* $nutri_values["composante_p"];
* $nutri_values["pourcent_fruits"];
* $nutri_values["mat_grass_r_prec"];
* $nutri_values["ags_r_prec"];
* $nutri_values["glucides_r_prec"];
* $nutri_values["sucre_r_prec"];
* $nutri_values["fib_alim_r_prec"];
* $nutri_values["proteine_r_prec"];
* $nutri_values["sel_r_prec"];
* $nutri_values["sodium_r_prec"];
* $nutri_values["fruits_r_prec"];
* $nutri_values["fruits_coque_sech_r_prec"];
* $nutri_values["score_nutri"];
*/
];
}
}