Skip to content

Commit

Permalink
Set unique error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Jan 10, 2025
1 parent a371c26 commit 9063ba5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Classes/Validation/Dom/DomNodeListValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getNodeList(): DOMNodeList
public function validateHasAny(): DomNodeListValidator
{
if (!$this->nodeList->length > 0) {
$this->addError('There must be at least one element');
$this->addError('There must be at least one element', 1736504345);
}
return $this;
}
Expand All @@ -122,7 +122,7 @@ public function validateHasAny(): DomNodeListValidator
public function validateHasOne(): DomNodeListValidator
{
if ($this->nodeList->length != 1) {
$this->addError('There must be an element');
$this->addError('There must be an element', 1736504354);
}
return $this;
}
Expand All @@ -135,17 +135,17 @@ public function validateHasOne(): DomNodeListValidator
public function validateHasNoneOrOne(): DomNodeListValidator
{
if (!($this->nodeList->length == 0 || $this->nodeList->length == 1)) {
$this->addError('There must be no more than one element');
$this->addError('There must be no more than one element', 1736504361);
}
return $this;
}

private function addError(string $prefix): void
private function addError(string $prefix, int $code): void
{
$message = $prefix . ' that matches the XPath expression "' . $this->expression . '"';
if ($this->contextNode) {
$message .= ' under "' . $this->contextNode->getNodePath() . '"';
}
$this->result->addError(new Error($message, 23));
$this->result->addError(new Error($message, $code));
}
}
14 changes: 7 additions & 7 deletions Classes/Validation/Dom/DomNodeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function validateHasContentWithEmail(): DomNodeValidator
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->result->addError(new Error('Email "' . $this->node->nodeValue . '" in the content of "' . $this->node->getNodePath() . '" is not valid.', 1724234607));
$this->result->addError(new Error('Email "' . $this->node->nodeValue . '" in the content of "' . $this->node->getNodePath() . '" is not valid.', 1736504169));
}

return $this;
Expand All @@ -99,7 +99,7 @@ public function validateHasContentWithUrl(): DomNodeValidator
}

if (!filter_var($this->node->nodeValue, FILTER_VALIDATE_URL)) {
$this->result->addError(new Error('URL "' . $this->node->nodeValue . '" in the content of "' . $this->node->getNodePath() . '" is not valid.', 1724234607));
$this->result->addError(new Error('URL "' . $this->node->nodeValue . '" in the content of "' . $this->node->getNodePath() . '" is not valid.', 1736504177));
}

return $this;
Expand All @@ -125,7 +125,7 @@ public function validateHasAttributeWithUrl(string $name): DomNodeValidator
// @phpstan-ignore-next-line
$value = $this->node->getAttribute($name);
if (!filter_var($value, FILTER_VALIDATE_URL)) {
$this->result->addError(new Error('URL "' . $value . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is not valid.', 1724234607));
$this->result->addError(new Error('URL "' . $value . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is not valid.', 1736504189));
}

return $this;
Expand All @@ -152,7 +152,7 @@ public function validateHasAttributeWithValue(string $name, array $values): DomN
// @phpstan-ignore-next-line
$value = $this->node->getAttribute($name);
if (!in_array($value, $values)) {
$this->result->addError(new Error('Value "' . $value . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is not permissible.', 1724234607));
$this->result->addError(new Error('Value "' . $value . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is not permissible.', 1736504197));
}

return $this;
Expand All @@ -179,7 +179,7 @@ public function validateHasUniqueAttribute(string $name, string $contextExpressi
// @phpstan-ignore-next-line
$value = $this->node->getAttribute($name);
if ($this->xpath->query($contextExpression . '[@' . $name . '="' . $value . '"]')->length > 1) {
$this->result->addError(new Error('"' . $name . '" attribute with value "' . $value . '" of "' . $this->node->getNodePath() . '" already exists.', 1724234607));
$this->result->addError(new Error('"' . $name . '" attribute with value "' . $value . '" of "' . $this->node->getNodePath() . '" already exists.', 1736504203));
}

return $this;
Expand Down Expand Up @@ -210,7 +210,7 @@ public function validateHasAttribute(string $name): DomNodeValidator

// @phpstan-ignore-next-line
if (!$this->node->hasAttribute($name)) {
$this->result->addError(new Error('Mandatory "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is missing.', 1724234607));
$this->result->addError(new Error('Mandatory "' . $name . '" attribute of "' . $this->node->getNodePath() . '" is missing.', 1736504217));
}
return $this;
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public function validateHasReferenceToId(string $name, string $targetExpression)
}

if ($foundElements !== 1) {
$this->result->addError(new Error('Value "' . $identifier . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" must reference one element under XPath expression "' . $targetExpression, 1724234607));
$this->result->addError(new Error('Value "' . $identifier . '" in the "' . $name . '" attribute of "' . $this->node->getNodePath() . '" must reference one element under XPath expression "' . $targetExpression, 1736504228));
}

return $this;
Expand Down

0 comments on commit 9063ba5

Please sign in to comment.