<?php
/**
 * Created by PhpStorm.
 * User: billy
 * Date: 11/02/2017
 * Time: 4:19 PM
 */

namespace App\Modules\Validators;

use App\Modules\Models\Ota\OtaVersion;

class OtaVersionCheckValidator
{
    public function validate($attribute, $value, $params, $validator){
        //return true if field value is foo

        $values = explode('.', $value);

        $versions = OtaVersion::all();

        $latest_version_array = array(0,0,0,0);
        foreach ($versions as $version)
        {
            $versionArray = explode('.', $version->version_code);
            for ($i = 0; $i < 4; $i++)
            {
                if (intval($versionArray[$i]) > intval($latest_version_array[$i]))
                {
                    $latest_version_array = $versionArray;
                    break;
                }
            }
        }

        for ($i = 0; $i < 4; $i++)
        {
            if (intval($values[$i]) > intval($latest_version_array[$i]))
            {
                return true;
            }
            else if (intval($values[$i]) < intval($latest_version_array[$i]))
            {
                return false;
            }
        }

        return false;
    }
}