132 lines
2.9 KiB
PHP
132 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\XmlInco;
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
use Illuminate\Contracts\Support\Jsonable;
|
|
|
|
/**
|
|
* Des listes d'allégations
|
|
*
|
|
* @property-read string[] $health
|
|
* @property-read string[] $nutrition
|
|
* @property-read string[] $other
|
|
*
|
|
* @package App\Lib
|
|
*/
|
|
class Allegations implements \Stringable, \JsonSerializable, Jsonable, Arrayable
|
|
{
|
|
/**
|
|
* Valeur minimale, ou exacte
|
|
*
|
|
* @var string[]
|
|
*/
|
|
private array $health;
|
|
|
|
/**
|
|
* Valeur minimale, ou exacte
|
|
*
|
|
* @var string[]
|
|
*/
|
|
private array $nutrition;
|
|
|
|
/**
|
|
* Valeur minimale, ou exacte
|
|
*
|
|
* @var string[]
|
|
*/
|
|
private array $other;
|
|
|
|
/**
|
|
* Allegation
|
|
*
|
|
* ! All array are filtered keeping only strings.
|
|
*
|
|
* @param string[] $health Health allegations.
|
|
* @param string[] $nutrition Nutrition allegations.
|
|
* @param string[] $other Other allegations.
|
|
* @return void
|
|
*/
|
|
public function __construct(array $health = [], array $nutrition = [], array $other = [])
|
|
{
|
|
$this->health = \array_filter($health, fn ($el) => \is_string($el));
|
|
$this->nutrition = \array_filter($nutrition, fn ($el) => \is_string($el));
|
|
$this->other = \array_filter($other, fn ($el) => \is_string($el));
|
|
}
|
|
|
|
// * Methods
|
|
|
|
/**
|
|
* Getter
|
|
*
|
|
* @param string $name
|
|
* @return array
|
|
* @throws \Error If property access is unhandled.
|
|
*/
|
|
public function __get(string $name): array
|
|
{
|
|
if (\in_array($name, ['health', 'nutrition', 'other'])) {
|
|
return $this->{$name};
|
|
}
|
|
$class = __CLASS__;
|
|
throw new \Error("Undefined property: {$class}::{$name}");
|
|
}
|
|
|
|
/**
|
|
* Get all
|
|
*
|
|
* @return array<string,string[]>
|
|
*/
|
|
public function all(): array
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
|
|
// * Implements Interfaces
|
|
|
|
/**
|
|
* Convert the object to its JSON representation.
|
|
*
|
|
* @param integer $options
|
|
* @return string
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
|
*/
|
|
public function toJson($options = 0)
|
|
{
|
|
// phpcs:enable
|
|
return \json_encode($this->jsonSerialize(), $options);
|
|
}
|
|
|
|
/**
|
|
* Convert the object into something JSON serializable.
|
|
*
|
|
* @return array<string,string[]>
|
|
*/
|
|
#[\ReturnTypeWillChange]
|
|
public function jsonSerialize()
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
|
|
/**
|
|
* Get the instance as an array.
|
|
*
|
|
* @return array<string,string[]>
|
|
*/
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'health' => $this->health,
|
|
'nutrition' => $this->nutrition,
|
|
'other' => $this->other
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function __toString()
|
|
{
|
|
return \implode("\n", \array_map(fn (array $arr) => \implode(', ', $arr), $this->toArray()));
|
|
}
|
|
}
|