本文介绍了检查哪个函数是true和false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于可怕的标题很抱歉,让我尝试在下面解释。

我写了一堆小函数,或false。

I have written a bunch of small functions that either returns true or false.

validateName()
validateEmail()
validateAddr()
validateBirtd()
validateUsername()



现在我正在循环使用CSV导入的大量数据文件和签入哪些数据有效(返回true或false)。

Now I am looping through a lot of data imported with a CSV file, and checkin which data is valid or not (returns true or false).

我这样做:

if (validateName($data[0]) == true AND validateEmail($data[1]) == true AND validateAddr($data[3]) == true AND validateBirtd($data[5]) == true AND validateUsername($data[6])==true) {
 // create array to import etc etc
}else{
 // create other array with data who failed validation, to show user later..etc etc
}

我的问题是 - 有更聪明的方法吗?是否可以为每个失败的验证创建一个列表?说3个条目失败validateEmail()函数,10都失败validateEmail和validateName()。

My question is - is there a more clever way to do this? Would it be possible to create a list of for each failed validation ? Say 3 entrys has fails the validateEmail() function, and 10 both fails validateEmail and validateName().

我有一种方法来排序,这样我可以告诉用户这些条目失败的电子邮件验证和这些条目失败的名称和电子邮件验证。

Would there be a way for me to sort this so I can tell the user "these entrys failed email validation" and "these entrys failed Name and email validation".

我想过一次验证一个字段,但是这样,如果一个条目有多个验证错误,我会有重复。

I thought about validating one field at a time, but this way I would have duplicates if one entry has more than one validation error.

推荐答案

如果有某种逻辑我不知道在哪里可以做这个

解决方案

您可以使用迭代器来同时获取CSV内容和过滤器。您还可以为每个CSV索引添加不同的回调

You can use Iterators to get CSV content and filter at the same time. you can also add different callback to each CSV index

示例

$csv = new CSVFilter(new CSVIterator("log.txt"));
$csv->addFilter(0, "validateName"); //<------------ Means validate Index at 0
$csv->addFilter(1, "validateEmail");
$csv->addFilter(2, "validateAddr");
$csv->addFilter(3, "validateBirtd");
$csv->addFilter(4, "validateName");
$csv->addFilter(5, "validateUsername");

foreach ( $csv as $data ) {
    var_dump($data);
}

//To get Errors
var_dump($csv->getErrors());

CSV过滤器

class CSVFilter extends FilterIterator {
    protected $filter = array();
    protected $errors = array();

    public function __construct(Iterator $iterator) {
        parent::__construct($iterator);
    }

    public function addFilter($index, Callable $callable) {
        $this->filter[$index] = $callable;
        $this->errors[$callable] = 0;
    }

    public function getErrors() {
        return $this->errors;
    }

    public function accept() {
        $line = $this->getInnerIterator()->current();
        $x = true;
        foreach ($this->filter  as $key => $var ) {
            if (isset($line[$key])) {
                $func = $this->filter[$key];
                $func($var) or $this->errors[$func] ++ and $x = false;
            }
        }
        return $x;
    }
}

CSVIterator >

CSVIterator

class CSVIterator implements \Iterator {
    protected $fileHandle;
    protected $line;
    protected $i;

    public function __construct($fileName) {
        if (! $this->fileHandle = fopen($fileName, 'r')) {
            throw new \RuntimeException('Couldn\'t open file "' . $fileName . '"');
        }
    }

    public function rewind() {
        fseek($this->fileHandle, 0);
        $this->line = fgetcsv($this->fileHandle);
        $this->i = 0;
    }

    public function valid() {
        return false !== $this->line;
    }

    public function current() {
        return array_map("trim", $this->line);
    }

    public function key() {
        return $this->i;
    }

    public function next() {
        if (false !== $this->line) {
            $this->line = fgetcsv($this->fileHandle);
            $this->i ++;
        }
    }

    public function __destruct() {
        fclose($this->fileHandle);
    }
}

简单随机函数 / p>

Simple Random Functions

function validateName($var) {
    return mt_rand(0, 5);
}

function validateEmail($var) {
    return mt_rand(0, 5);
}

function validateAddr($var) {
    return mt_rand(0, 5);
}

function validateBirtd($var) {
    return mt_rand(0, 5);
}

function validateUsername($var) {
    return mt_rand(0, 5);
}

这篇关于检查哪个函数是true和false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:34