<?php


namespace Fifa\ConnectId\Sdk\Model\Validation;


use Exception;

class PictureValidator implements DocumentValidatorInterface
{
    /**
     * Validates provided content
     * @param string $base64Content
     * @return DocumentValidationResult
     */
    public function validate(string $base64Content): DocumentValidationResult
    {
        $validationResult = new DocumentValidationResult();

        try {
            $decoded = base64_decode($base64Content);
            $imageInfo = getimagesizefromstring($decoded);
            if (!$imageInfo) {
                return $validationResult->failed('Given content is not correct base64 encoded image.');
            }
        } catch (Exception $ex) {
            return $validationResult->failed(sprintf('Given content is not correct base64 encoded image. Supported types: Bmp, Gif, Jpeg, Png, Tiff. Details: %s', $ex->getMessage()));
        }

        return $validationResult;
    }
}