<?php


namespace Fifa\ConnectId\Sdk\ApiModelValidation;


use DateTime;
use Exception;
use Fifa\ConnectId\Sdk\Registration\ValidationException;

class DateTimeValidatingParser
{
    /**
     * @param string|null $stringToValidate
     * @param string $validationSubjectName
     * @return DateTime
     * @throws ValidationException
     */
    public static function parse(?string $stringToValidate, string $validationSubjectName): DateTime
    {
        ApiModelValidator::cannotBeNullOrWhitespace($stringToValidate, $validationSubjectName);
        try{
            return new DateTime($stringToValidate);
        }
        catch (Exception $exe)
        {
            throw new ValidationException(sprintf("[%s] value cannot be parsed to a DateTime. Provided value: %s", $validationSubjectName, $stringToValidate),0,$exe);
        }
    }
}