<?php


namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators;


use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;

class NameserverValidator extends BaseValidator
{
    protected $required = false;
    protected $public = true;

    public function __construct($required = false , $public = true)
    {
        $this->required = $required;
        $this->public = $public;
    }

    protected function validate($data, $additionalData = null)
    {
        if($additionalData->get('formData')['osTemplate'] && $additionalData->get('formData')['osTemplate']=='installationFromIso' ){
            return true;
        }

        if (!$this->required && empty($data))
        {
            return true;
        }
        if (!filter_var($data, FILTER_VALIDATE_IP))
        {
            $this->addValidationError('IP Address is not valid', false);
            return false;
        }
        if (!$this->public && filter_var($data, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE))
        {
            $this->addValidationError('The IP Address is not private', false);
            return false;
        }
        return true;
    }
}