feat: harden xml parsing to avoid uncaught errors
This commit is contained in:
1 parent
3849ae306c
commit
3234c78f0d
9 files changed
+229
-147
No files matched your search
@@ -47,4 +47,23 @@ abstract class AbstractParser
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user