ldc-flucollector/app/Lib/XmlInco/NutritionalValues.php
2023-09-27 12:12:57 +02:00

281 lines
7.8 KiB
PHP

<?php
namespace App\Lib\XmlInco;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
/**
* Les valeurs nutritionelles
*
* @property-read array<string,float|string|null> $values
*
* @package App\Lib
*/
class NutritionalValues implements \Stringable, \JsonSerializable, Jsonable, Arrayable
{
/**
* Liste des valeurs nutritionelles
*
* @var array<string,float|integer|null|string>
*/
private array $values;
/**
* Nutritional values properties
*
* @var string[]
*/
private static $properties = [
'contribution_for',
'nutritional_score',
'ranking',
'energy',
'energy_kj',
'protein',
'carbohydrates',
'sugar',
'fat',
'saturated_fatty_acids',
'dietary_fiber',
'salt',
'energy_percent',
'energy_kj_percent',
'protein_percent',
'carbohydrates_percent',
'sugar_percent',
'fat_percent',
'saturated_fatty_acids_percent',
'dietary_fiber_percent',
'salt_percent'
];
/**
* Nutritional values properties units mesure
*
* @var array<string,string|null>
*/
private static $propertiesUnits = [
'contribution_for' => null,
'nutritional_score' => null,
'ranking' => null,
'energy' => 'Kcal',
'energy_kj' => 'Kj',
'protein' => 'g',
'carbohydrates' => 'g',
'sugar' => 'g',
'fat' => 'g',
'saturated_fatty_acids' => 'g',
'dietary_fiber' => 'g',
'salt' => 'g',
'energy_percent' => '%',
'energy_kj_percent' => '%',
'protein_percent' => '%',
'carbohydrates_percent' => '%',
'sugar_percent' => '%',
'fat_percent' => '%',
'saturated_fatty_acids_percent' => '%',
'dietary_fiber_percent' => '%',
'salt_percent' => '%'
];
/**
* Translation values of inco unreadable keys
*
* @var string[]
*/
private static $cleanValues = [
'apport_port' => 'contribution_for',
'score_nutri' => 'nutritional_score',
'classement' => 'ranking',
'val_energ_kcal_r' => 'energy',
'val_energ_kcal_p' => 'energy_percent',
'val_energ_kj_r' => 'energy_kj',
'pourcent_apport_ref_kjo_r' => 'energy_kj_percent',
'proteine_r' => 'protein',
'pourcent_apport_ref_pro_r' => 'protein_percent',
'sucre_r' => 'sugar',
'pourcent_apport_ref_sugar_r' => 'sugar_percent',
'mat_grass_r' => 'fat',
'pourcent_apport_ref_fat_r' => 'fat_percent',
'fib_alim_r' => 'dietary_fiber',
'pourcent_apport_ref_fibtg_r' => 'dietary_fiber_percent',
'glucides_r' => 'carbohydrates',
'glucides_p' => 'carbohydrates_percent',
'ags_r' => 'saturated_fatty_acids',
'pourcent_ags' => 'saturated_fatty_acids_percent',
'sel_r' => 'salt',
'pourcent_apport_ref_salteq_r' => 'salt_percent'
];
/**
* Human readable translations for inco translation
*
* @var string[]
*/
private static $humanReadable = [
'Pour' => 'contribution_for',
'Classement' => 'ranking',
'Nutriscore' => 'nutritional_score',
'Énergie' => 'energy',
'Énergie kJ' => 'energy_kj',
'Protéines' => 'protein',
'Glucides' => 'carbohydrates',
'Sucres' => 'sugar',
'Matière grasse' => 'fat',
'Acides Gras Saturés' => 'saturated_fatty_acids',
'Fibres alimentaires' => 'dietary_fiber',
'Sel' => 'salt'
];
/**
* Allegation
*
* ! All array are filtered keeping only strings.
*
* @param string[] $values Nutritional values.
* @return void
*/
public function __construct(array $values = [])
{
$emptySkel = \array_map(fn () => null, \array_flip(self::$properties));
$values = \array_map(fn ($value) => \is_null($value) ?
$value : (\is_numeric($value) ? \floatval($value) : \strval($value)), $values);
$this->values = \array_merge($emptySkel, $values);
}
// * Methods
/**
* Getter
*
* @param string $name
* @return array|float|string|null
* @throws \Error If property access is unhandled.
*/
public function __get(string $name): array|float|string|null
{
if (\in_array($name, self::$properties)) {
return $this->values[$name];
}
if ($name === 'values') {
return $this->values;
}
$class = __CLASS__;
throw new \Error("Undefined property: {$class}::{$name}");
}
/**
* Get human readable values
*
* @return array<string,float|string|null>
*/
public function humanReadableValues(): array
{
$o = [];
foreach (self::$humanReadable as $k => $t) {
$o[$k] = $this->values[$t];
}
return $o;
}
/**
* Get units
*
* @param boolean $forHumanReadable
* @return array
*/
public static function getUnits(bool $forHumanReadable = false): array
{
if ($forHumanReadable) {
$out = [];
$humanReadable = array_flip(self::$humanReadable);
foreach (self::$propertiesUnits as $propertyIndex => $unit) {
if (\array_key_exists($propertyIndex, $humanReadable)) {
$out[$humanReadable[$propertyIndex]] = $unit;
}
}
return $out;
}
return self::$propertiesUnits;
}
// * Static Methods
/**
* Convert Xml Inco values into proper Nutritional values
*
* @param array $incoNutritionalValues
* @return self
*/
public static function makeNutritionalValues(array $incoNutritionalValues): self
{
$incoNutritionalValues = \array_map(fn ($value) => \is_null($value) ?
$value : (\is_numeric($value) ? \floatval($value) : \strval($value)), $incoNutritionalValues);
$o = [];
foreach (self::$cleanValues as $k => $t) {
$o[$t] = $incoNutritionalValues[$k] ?? null;
}
return new self($o);
}
// * Implements Interfaces
/**
* Convert the object to its JSON representation.
*
* @param integer $options Json_encode php options.
* @param boolean $fullData Serialize all properties or ignore empty one. Default is false (ignore).
* @return string
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
*/
public function toJson($options = 0, bool $fullData = false)
{
// phpcs:enable
return \json_encode($this->jsonSerialize($fullData), $options);
}
/**
* Convert the object into something JSON serializable.
*
* @param boolean $fullData Serialize all properties or ignore empty one. Default is false (ignore).
* @return array<string,float|string|null>
*/
#[\ReturnTypeWillChange]
public function jsonSerialize(bool $fullData = false)
{
if ($fullData) {
return $this->toArray();
}
// * Save BDD space dont serialize empty values.
return array_filter($this->toArray(), fn ($value) => !\is_null($value));
}
/**
* Get the instance as an array.
*
* @return array<string,float|string|null>
*/
public function toArray()
{
return $this->values;
}
/**
* @return string
*/
public function __toString()
{
$values = $this->humanReadableValues();
$units = self::getUnits(true);
return \implode("\n", \array_map(
fn (float|string|null $value, string $index) =>
"{$index} : {$value}" . ($units[$index] ? ' ' . $units[$index] : ''),
$values,
\array_keys($values)
));
}
}