<?php
namespace Qii\Driver\Entity;

use Qii\Driver\Response;

class Verify {
    public $Key;
    public $Value;
    public $Valid;
    public $Message;
    public $Limit;
    public $Validator;
    public function __construct($key, $value, $valid, $message, $limit = null){
        $this->Key = $key;
        $this->Value = $value;
        $this->Valid = $valid;
        $this->Message = $message;
        $this->Limit = $limit;
        $this->Validator = _library('Validate');
    }

    public function valid() {
        $verify = [];
        switch ($this->Valid) {
            case 'required':
            case 'email':
            case 'idcode':
            case 'number':
            case 'float':
            case 'http':
            case 'qq':
            case 'postcode':
            case 'ip':
            case 'phone':
            case 'telephone':
            case 'en':
            case 'cn':
            case 'account':
            case 'date':
            case 'datetime':
            case 'safe':
            case 'password':
            case 'string':
            case 'filename':
                $verify = $this->Validator->verify(
                    [$this->Key => $this->Value],
                    [$this->Key => [$this->Valid => true]],
                    [$this->Key => [$this->Valid => $this->Message]]
                );
                break;
            case 'minlength':
            case 'maxlength':
            case 'minnumber':
            case 'maxnumber':
            case 'length':
            case 'rangeof':
            case 'sets':
            case 'setsarray':
            case 'decimal':
                $verify = $this->Validator->verify(
                    [$this->Key => $this->Value],
                    [$this->Key => [$this->Valid => $this->Limit]],
                    [$this->Key => [$this->Valid => $this->Message]]
                );
                break;
        }
        if($verify !== true) {
            return Response::Fail('valid', ['_result' => $verify]);
        }
        return Response::Success('valid', ['_result' => true]);
    }
}