Metadata about validation */ private array $metadata = []; /** @var array Results from individual layers */ private array $layerResults = []; /** @var array Sanitized data after validation */ private array $sanitizedData = []; /** @var float Execution time in milliseconds */ private float $executionTime = 0.0; /** @var array Warnings from validation process */ private array $warnings = []; /** * Set validation result * * @param bool $valid Whether validation passed * @return void * @since 1.0.0 */ public function setValid(bool $valid): void { $this->valid = $valid; } /** * Check if validation passed * * @return bool * @since 1.0.0 */ public function isValid(): bool { return $this->valid; } /** * Set error message * * @param string $error Error message * @return void * @since 1.0.0 */ public function setError(string $error): void { $this->error = $error; } /** * Get error message * * @return string * @since 1.0.0 */ public function getError(): string { return $this->error; } /** * Set metadata * * @param array $metadata Metadata array * @return void * @since 1.0.0 */ public function setMetadata(array $metadata): void { $this->metadata = $metadata; } /** * Get metadata * * @return array * @since 1.0.0 */ public function getMetadata(): array { return $this->metadata; } /** * Add metadata item * * @param string $key Metadata key * @param mixed $value Metadata value * @return void * @since 1.0.0 */ public function addMetadata(string $key, mixed $value): void { $this->metadata[$key] = $value; } /** * Add layer result * * @param string $layer Layer name * @param ValidationLayerResult $result Layer result * @return void * @since 1.0.0 */ public function addLayerResult(string $layer, ValidationLayerResult $result): void { $this->layerResults[$layer] = $result; // Collect warnings from layer if ($result->hasWarnings()) { $this->warnings = array_merge($this->warnings, $result->getWarnings()); } } /** * Get layer result * * @param string $layer Layer name * @return ValidationLayerResult|null * @since 1.0.0 */ public function getLayerResult(string $layer): ?ValidationLayerResult { return $this->layerResults[$layer] ?? null; } /** * Get all layer results * * @return array * @since 1.0.0 */ public function getLayerResults(): array { return $this->layerResults; } /** * Set sanitized data * * @param array $data Sanitized data * @return void * @since 1.0.0 */ public function setSanitizedData(array $data): void { $this->sanitizedData = $data; } /** * Get sanitized data * * @return array * @since 1.0.0 */ public function getSanitizedData(): array { return $this->sanitizedData; } /** * Set execution time * * @param float $time Execution time in milliseconds * @return void * @since 1.0.0 */ public function setExecutionTime(float $time): void { $this->executionTime = $time; } /** * Get execution time * * @return float Execution time in milliseconds * @since 1.0.0 */ public function getExecutionTime(): float { return $this->executionTime; } /** * Add warning * * @param string $warning Warning message * @return void * @since 1.0.0 */ public function addWarning(string $warning): void { $this->warnings[] = $warning; } /** * Get warnings * * @return array * @since 1.0.0 */ public function getWarnings(): array { return $this->warnings; } /** * Check if result has warnings * * @return bool * @since 1.0.0 */ public function hasWarnings(): bool { return !empty($this->warnings); } /** * Get failed layers * * @return array * @since 1.0.0 */ public function getFailedLayers(): array { $failedLayers = []; foreach ($this->layerResults as $layer => $result) { if (!$result->isValid()) { $failedLayers[] = $layer; } } return $failedLayers; } /** * Get passed layers * * @return array * @since 1.0.0 */ public function getPassedLayers(): array { $passedLayers = []; foreach ($this->layerResults as $layer => $result) { if ($result->isValid()) { $passedLayers[] = $layer; } } return $passedLayers; } /** * Get validation summary * * @return array * @since 1.0.0 */ public function getSummary(): array { $layerSummary = []; foreach ($this->layerResults as $layer => $result) { $layerSummary[$layer] = [ 'valid' => $result->isValid(), 'error' => $result->getError(), 'warnings' => $result->getWarnings() ]; } return [ 'overall_valid' => $this->valid, 'overall_error' => $this->error, 'execution_time_ms' => $this->executionTime, 'warnings_count' => count($this->warnings), 'layers_passed' => count($this->getPassedLayers()), 'layers_failed' => count($this->getFailedLayers()), 'layers' => $layerSummary, 'sanitized_fields' => array_keys($this->sanitizedData), 'metadata' => $this->metadata ]; } /** * Convert result to JSON * * @return string JSON representation * @since 1.0.0 */ public function toJson(): string { return wp_json_encode($this->getSummary()); } /** * Check if specific layer passed * * @param string $layer Layer name * @return bool * @since 1.0.0 */ public function isLayerValid(string $layer): bool { $result = $this->getLayerResult($layer); return $result ? $result->isValid() : false; } /** * Get first error (from first failed layer) * * @return string * @since 1.0.0 */ public function getFirstError(): string { if (!empty($this->error)) { return $this->error; } foreach ($this->layerResults as $result) { if (!$result->isValid() && $result->getError()) { return $result->getError(); } } return ''; } /** * Get all errors from all layers * * @return array * @since 1.0.0 */ public function getAllErrors(): array { $errors = []; if (!empty($this->error)) { $errors[] = $this->error; } foreach ($this->layerResults as $layer => $result) { if (!$result->isValid() && $result->getError()) { $errors[] = "{$layer}: " . $result->getError(); } } return $errors; } /** * Check if validation was fast enough (under performance threshold) * * @param float $threshold Threshold in milliseconds * @return bool * @since 1.0.0 */ public function isPerformant(float $threshold = 10.0): bool { return $this->executionTime <= $threshold; } /** * Get security score (0-100 based on layers passed and performance) * * @return int Security score * @since 1.0.0 */ public function getSecurityScore(): int { $totalLayers = count($this->layerResults); $passedLayers = count($this->getPassedLayers()); if ($totalLayers === 0) { return 0; } $layerScore = ($passedLayers / $totalLayers) * 80; // Max 80 points for passing layers // Performance bonus (max 20 points) $performanceScore = $this->isPerformant() ? 20 : max(0, 20 - ($this->executionTime - 10)); // Warning penalty $warningPenalty = count($this->warnings) * 2; return max(0, min(100, (int)($layerScore + $performanceScore - $warningPenalty))); } }