HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/bekkers/bekkersengineering.nl/app/KommaApp/Shop/Vies/Rules/VatNumber.php
<?php

namespace App\KommaApp\Shop\Vies\Rules;

use Illuminate\Contracts\Validation\Rule;

/**
 * Class VatNumber
 *
 * Validates a VAT number.
 * Notice that it also considers a VAT number invalid if the VIES service is unavailable
 *
 * @package App\KommaApp\Shop\Vies
 */
class VatNumber implements Rule
{
    /** @var ViesService $service */
    private $service;

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->service = \App::make(ViesService::class);
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $result = $this->service->validateVatNumber($value);

        if(is_a($result, ViesResult::class)) {
            return true;
        }
        else if(is_int($result))
        {
            if($result == 0) return false;
            else if($result == -1) return false; //Vat could not be validated
        }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return __('shop/views.not_valid');
    }
}