<?php

namespace Planaday\Utility\FormBuilder\Validators;

use Planaday\Utility\FormBuilder\Condition;
use Planaday\Utility\FormBuilder\Validator;

class ConditionValidator implements Validator {
	use BasicValidator;

	public function __construct(
		protected string $checkName,
		protected Condition $condition,
		protected string $errorMessage = 'Condition failed',
	) {}

	public function getName(): string {
		return $this->checkName;
	}
	public function runValidator(mixed $data): bool|string {
		if ($this->condition->check()){
			return true;
		}

		return $this->errorMessage;
	}

	public function jsonSerialize(): array {
		return [
			'type' => 'condition',
			'condition' => $this->condition,
			'errorMessage' => $this->errorMessage,
			'checkName' => $this->checkName,
			'conditions' => $this->conditions
		];
	}
}