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

47 lines
1.5 KiB
PHP
Raw Normal View History

2026-05-13 09:10:43 +03:00
<?php
namespace YakNet\AccessibilityConsole\Core;
class RuleEngine
{
2026-05-13 12:46:26 +03:00
/** @var array */
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) {
2026-05-13 12:46:26 +03:00
if (method_exists($rule, 'check')) {
// Determine if it's a document-level rule or element-level rule
$reflection = new \ReflectionMethod($rule, 'check');
$params = $reflection->getParameters();
if (isset($params[0]) && $params[0]->getType() && $params[0]->getType()->getName() === 'DOMDocument') {
// Document-level rule (Core\AbstractRule style)
$violations = $rule->check($doc);
$allViolations = array_merge($allViolations, $violations);
} else {
// Element-level rule (Rules\RuleInterface style)
// We need to iterate over all elements
$xpath = new \DOMXPath($doc);
$elements = $xpath->query('//*');
foreach ($elements as $element) {
$violation = $rule->check($element);
if ($violation) {
$allViolations[] = $violation;
}
}
}
}
2026-05-13 12:13:32 +03:00
}
return $allViolations;
2026-05-13 09:10:43 +03:00
}
}