80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet\XmlParser;
|
|
|
|
use App\BusProNet\Traits\TypeConversionTrait;
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
abstract class AbstractParser
|
|
{
|
|
use TypeConversionTrait;
|
|
|
|
protected function getIntOrNullAttribute(?string $value): ?int
|
|
{
|
|
if (null === $value || '' === $value) {
|
|
return null;
|
|
}
|
|
|
|
return (int) $value;
|
|
}
|
|
|
|
protected function getStringOrNullValue(Crawler $node): ?string
|
|
{
|
|
return 0 < $node->count() ? $node->text() : null;
|
|
}
|
|
|
|
protected function getIntOrNullValue(Crawler $node): ?int
|
|
{
|
|
return 0 < $node->count() ? (int) $node->text() : null;
|
|
}
|
|
|
|
protected function getFloatOrNullValue(Crawler $node): ?float
|
|
{
|
|
return 0 < $node->count() ? $this->stringToFloat($node->text()) : null;
|
|
}
|
|
|
|
protected function getDateOrNullValue(Crawler $node): ?\DateTimeImmutable
|
|
{
|
|
return 0 < $node->count() ? $this->stringToDate($node->text()) : null;
|
|
}
|
|
|
|
protected function getDateTimeOrNullValue(Crawler $node): ?\DateTimeImmutable
|
|
{
|
|
return 0 < $node->count() ? $this->stringToDateTime($node->text()) : null;
|
|
}
|
|
|
|
protected function getBoolValue(Crawler $node): bool
|
|
{
|
|
return 0 < $node->count() && $this->stringToBool($node->text());
|
|
}
|
|
|
|
/** @return list<string> */
|
|
protected function getArrayValue(Crawler $node, string $separator = ','): array
|
|
{
|
|
if (0 === $node->count()) {
|
|
return [];
|
|
}
|
|
|
|
return $this->stringToArray($node->text(), $separator);
|
|
}
|
|
|
|
protected function getAttrOrNullValue(Crawler $node, string $attribute): ?string
|
|
{
|
|
if (0 === $node->count()) {
|
|
return null;
|
|
}
|
|
|
|
return $node->attr($attribute);
|
|
}
|
|
|
|
protected function getRequiredAttrValue(Crawler $node, string $attribute, string $context): string
|
|
{
|
|
$value = $this->getAttrOrNullValue($node, $attribute);
|
|
if (null === $value || '' === trim($value)) {
|
|
throw new \InvalidArgumentException(sprintf('Missing required attribute "%s" in %s.', $attribute, $context));
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
}
|