Files
ednotif-bundle/src/Shared/Mapper/StandardResponseMapper.php

54 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Shared\Mapper;
use Malio\EdnotifBundle\Shared\Dto\AnomalyDto;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
final class StandardResponseMapper
{
public function map(mixed $standardResponseNode): StandardResponseDto
{
$result = (bool) ($standardResponseNode->Resultat ?? false);
$anomalyNode = is_object($standardResponseNode) ? ($standardResponseNode->Anomalie ?? null) : null;
$anomaly = null;
if (is_object($anomalyNode)) {
$anomaly = new AnomalyDto(
code: $this->toNullableString($anomalyNode->Code ?? null),
severity: $this->toNullableInt($anomalyNode->Severite ?? null),
message: $this->toNullableString($anomalyNode->Message ?? null),
);
}
return new StandardResponseDto($result, $anomaly);
}
private function toNullableString(mixed $value): ?string
{
if (null === $value) {
return null;
}
$stringValue = trim((string) $value);
return '' === $stringValue ? null : $stringValue;
}
private function toNullableInt(mixed $value): ?int
{
if (null === $value) {
return null;
}
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return null;
}
}