<?php

namespace Planaday\Utility\FormBuilder\Validators;

use Planaday\Utility\FormBuilder\Condition;

trait BasicValidator {
	protected array $conditions = [];

	public function addCondition(Condition ...$condition): static {
		$this->conditions = array_merge( $this->conditions, $condition );

		return $this;
	}

	public function validate( mixed $data ): bool|string {
		$check = true;
		foreach ($this->conditions as $condition) {
			$check = $check && $condition->check();
		}

		if (!$check) {
			return true;
		}

		return $this->runValidator( $data );
	}

	protected abstract function runValidator( mixed $data ): bool|string;
}