<?php
/**
 * ValidationResult class.
 *
 * Represents the result of a validation attempt.
 *
 * @package Ilabs\Firevert\Core\Form\Validation
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Form\Validation;

/**
 * Class ValidationResult
 */
final class ValidationResult {

	/**
	 * Whether validation passed.
	 *
	 * @var bool
	 */
	private bool $valid;

	/**
	 * Validation errors.
	 *
	 * @var array<string, string[]>
	 */
	private array $errors;

	/**
	 * Constructor.
	 *
	 * @param bool                    $valid Whether validation passed.
	 * @param array<string, string[]> $errors Array of validation errors.
	 */
	private function __construct( bool $valid, array $errors = array() ) {
		$this->valid  = $valid;
		$this->errors = $errors;
	}

	/**
	 * Returns a successful result.
	 *
	 * @return self
	 */
	public static function success(): self {
		return new self( true );
	}

	/**
	 * Returns a failure result with errors.
	 *
	 * @param array<string, string[]> $errors Validation errors.
	 *
	 * @return self
	 */
	public static function failure( array $errors ): self {
		return new self( false, $errors );
	}

	/**
	 * Whether the validation passed.
	 *
	 * @return bool
	 */
	public function is_valid(): bool {
		return $this->valid;
	}

	/**
	 * Returns validation errors.
	 *
	 * @return array<string, string[]>
	 */
	public function get_errors(): array {
		return $this->errors;
	}
}
