<?php


namespace Fifa\ConnectId\Sdk\Model\Validation;


class DocumentValidator implements DocumentValidatorInterface
{
    /**
     * @var DocumentValidatorInterface[]
     */
    public $validators;

    /**
     * DocumentValidator constructor.
     * @param mixed ...$validators
     */
    public function __construct(... $validators)
    {
        $this->validators = $validators;
    }

    /**
     * @param string $base64Content
     * @return DocumentValidationResult
     */
    public function validate(string $base64Content): DocumentValidationResult
    {
        /* @var DocumentValidationResult[] $results */
        $results = [];
        foreach ($this->validators as $validator) {
            $results[] = $validator->validate($base64Content);
        }

        $concatenatedResult = new DocumentValidationResult();
        $concatenatedErrorMessage = 'Provided content does not meet any validation criteria: ';

        foreach ($results as $result) {
            if ($result->isValid()) {
                return $concatenatedResult;
            } else {
                $concatenatedErrorMessage .= $result->getMessage();
            }
        }

        return $concatenatedResult->failed($concatenatedErrorMessage);
    }
}