Files
accessibility-console/src/Core/RuleEngine.php
T

44 lines
1.4 KiB
PHP
Raw Normal View History

2026-05-13 09:10:43 +03:00
<?php
namespace YakNet\AccessibilityConsole\Core;
class RuleEngine
{
/** @var array<int, mixed> */
2026-05-13 09:10:43 +03:00
private array $rules = [];
2026-05-13 12:46:26 +03:00
public function addRule(mixed $rule): void
2026-05-13 09:10:43 +03:00
{
$this->rules[] = $rule;
}
/**
2026-05-13 12:13:32 +03:00
* @return Violation[]
2026-05-13 09:10:43 +03:00
*/
2026-05-13 12:13:32 +03:00
public function run(\DOMDocument $doc): array
2026-05-13 09:10:43 +03:00
{
2026-05-13 12:13:32 +03:00
$allViolations = [];
foreach ($this->rules as $rule) {
if ($rule instanceof AbstractRule) {
// Document-level rule (Core\AbstractRule style)
$violations = $rule->check($doc);
$allViolations = array_merge($allViolations, $violations);
} elseif ($rule instanceof \YakNet\AccessibilityConsole\Rules\RuleInterface) {
// Element-level rule (Rules\RuleInterface style)
$xpath = new \DOMXPath($doc);
$elements = $xpath->query('//*');
if ($elements !== false) {
2026-05-13 12:46:26 +03:00
foreach ($elements as $element) {
if ($element instanceof \DOMElement) {
$violation = $rule->check($element);
if ($violation) {
$allViolations[] = $violation;
}
2026-05-13 12:46:26 +03:00
}
}
}
}
2026-05-13 12:13:32 +03:00
}
return $allViolations;
2026-05-13 09:10:43 +03:00
}
}