121 lines
2.9 KiB
PHP
121 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Uwil\ArcadiaGenesisPhpClient\Lib\ApiDto\Dto\IncoCompo;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Base implementation for NutriDto.
|
||
|
|
*
|
||
|
|
* Concrete classes must implement:
|
||
|
|
* - fieldLabels(): array<string, string> - [ propertyName => 'Human label' ]
|
||
|
|
* - fieldUnits(): array<string, string|null> - [ propertyName => 'unit'|null ]
|
||
|
|
*
|
||
|
|
* toArray() is built automatically via reflection on public properties.
|
||
|
|
*/
|
||
|
|
abstract readonly class AbstractIncoCompoPartDto implements IncoCompoPartDto
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Map of [ propertyName => 'Human readable label' ].
|
||
|
|
*
|
||
|
|
* @return array<string, string>
|
||
|
|
*/
|
||
|
|
abstract protected static function fieldLabels(): array;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Map of [ propertyName => 'unit string or null' ].
|
||
|
|
*
|
||
|
|
* @return array<string, string|null>
|
||
|
|
*/
|
||
|
|
abstract protected static function fieldUnits(): array;
|
||
|
|
|
||
|
|
// -------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*/
|
||
|
|
public function toArray(): array
|
||
|
|
{
|
||
|
|
// Reflection on public properties of the concrete child class.
|
||
|
|
$ref = new \ReflectionObject($this);
|
||
|
|
$result = [];
|
||
|
|
|
||
|
|
foreach ($ref->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
|
||
|
|
$name = $property->getName();
|
||
|
|
$result[$name] = $property->getValue($this);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*/
|
||
|
|
public function humanReadableValues(): array
|
||
|
|
{
|
||
|
|
$values = $this->toArray();
|
||
|
|
$out = [];
|
||
|
|
|
||
|
|
foreach (static::fieldLabels() as $property => $label) {
|
||
|
|
$out[$label] = $values[$property] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*/
|
||
|
|
public static function units(): array
|
||
|
|
{
|
||
|
|
return static::fieldUnits();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*/
|
||
|
|
public static function humanReadableUnits(): array
|
||
|
|
{
|
||
|
|
$out = [];
|
||
|
|
|
||
|
|
foreach (static::fieldLabels() as $property => $label) {
|
||
|
|
$units = static::fieldUnits();
|
||
|
|
if (\array_key_exists($property, $units)) {
|
||
|
|
$out[$label] = $units[$property];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $out;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*
|
||
|
|
* @return array<string, float|string|null>
|
||
|
|
*/
|
||
|
|
public function jsonSerialize(): array
|
||
|
|
{
|
||
|
|
return array_filter(
|
||
|
|
$this->toArray(),
|
||
|
|
static fn (mixed $value): bool => $value !== null,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritDoc}
|
||
|
|
*/
|
||
|
|
public function __toString(): string
|
||
|
|
{
|
||
|
|
$values = $this->humanReadableValues();
|
||
|
|
$units = static::humanReadableUnits();
|
||
|
|
|
||
|
|
$lines = [];
|
||
|
|
foreach ($values as $label => $value) {
|
||
|
|
$unit = $units[$label] ?? null;
|
||
|
|
$lines[] = $label . ' : ' . $value . ($unit !== null ? ' ' . $unit : '');
|
||
|
|
}
|
||
|
|
|
||
|
|
return \implode("\n", $lines);
|
||
|
|
}
|
||
|
|
}
|