67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\BusProNet\XmlParser;
|
|
|
|
use App\BusProNet\Model\AgeConstraintResult;
|
|
|
|
/**
|
|
* Parser for birth year constraint data in JG:YYYY-YYYY format.
|
|
*
|
|
* This parser handles constraint data that specifies birth year ranges,
|
|
* such as 'JG:2007-2009' (Jahrgang 2007 to 2009) or 'JG:2007' (single year).
|
|
*/
|
|
class BirthYearConstraintParser implements AgeConstraintParserInterface
|
|
{
|
|
private const BIRTH_YEAR_PREFIX = 'JG:';
|
|
|
|
public function canParse(string $constraintData): bool
|
|
{
|
|
return str_starts_with($constraintData, self::BIRTH_YEAR_PREFIX);
|
|
}
|
|
|
|
public function parse(string $constraintData): AgeConstraintResult
|
|
{
|
|
if (false === $this->canParse($constraintData)) {
|
|
throw new \InvalidArgumentException('Cannot parse constraint data: ' . $constraintData);
|
|
}
|
|
|
|
$yearData = substr($constraintData, strlen(self::BIRTH_YEAR_PREFIX));
|
|
|
|
// Parse range format "2007-2009"
|
|
if (str_contains($yearData, '-')) {
|
|
[$fromYear, $toYear] = explode('-', $yearData, 2);
|
|
|
|
return new AgeConstraintResult(
|
|
type: 'birth_year',
|
|
birthYearFrom: (int) trim($fromYear),
|
|
birthYearTo: (int) trim($toYear),
|
|
metadata: [
|
|
'range_type' => 'birth_year_range',
|
|
'original_format' => $yearData,
|
|
],
|
|
rawData: $constraintData
|
|
);
|
|
}
|
|
|
|
// Parse single year format "2007"
|
|
$year = (int) trim($yearData);
|
|
|
|
return new AgeConstraintResult(
|
|
type: 'birth_year',
|
|
birthYearFrom: $year,
|
|
birthYearTo: $year,
|
|
metadata: [
|
|
'range_type' => 'birth_year_single',
|
|
'original_format' => $yearData,
|
|
],
|
|
rawData: $constraintData
|
|
);
|
|
}
|
|
|
|
public function getConstraintType(): string
|
|
{
|
|
return 'birth_year';
|
|
}
|
|
} |