function testAllowNullNoAllowEmptyWithNullData() { $validator = new CompExpValidator(); $validator->setDataCanBeEmpty(false); $validator->setDataCanBeNull(true); $this->assertTrue($validator->validate(null, "<30")); }
public function validate($data, $criteria = null) { $regExpValidator = new RegExpValidator(); $regExpValidator->setDataCanBeNull($this->getDataCanBeNull()); $regExpValidator->setDataCanBeEmpty($this->getDataCanBeEmpty()); $compExpValidator = new CompExpValidator(); $compExpValidator->setDataCanBeNull($this->getDataCanBeNull()); $compExpValidator->setDataCanBeEmpty($this->getDataCanBeEmpty()); $dateExpValidator = new DateExpValidator(); $dateExpValidator->setDataCanBeNull($this->getDataCanBeNull()); $dateExpValidator->setDataCanBeEmpty($this->getDataCanBeEmpty()); $timeExpValidator = new TimeExpValidator(); $timeExpValidator->setDataCanBeNull($this->getDataCanBeNull()); $timeExpValidator->setDataCanBeEmpty($this->getDataCanBeEmpty()); $currencyExpValidator = new currencyExpValidator(); $currencyExpValidator->setDataCanBeNull($this->getDataCanBeNull()); $currencyExpValidator->setDataCanBeEmpty($this->getDataCanBeEmpty()); //if $data is single variable, make it array //then continue with looping and checking if (!is_array($this->criteria_arr)) { //something is wrong... should never hit this case throw new Exception("Criteria array not array"); $this->success = false; return $this->success; } /* NOTE, IF NO CRITERIA ARE DEFINED, THEN DATA AUTOMATICALLY PASSES. THIS IS HANDLED NATURALLY BY EXECUTION OF THE FUNCTION UNLESS NOT NULL OR NOT EMPTY IS SPECIFIEID AND DATA IS... */ if (count($this->criteria_arr) < 1) { //test for null data if (is_null($data)) { if (!$this->getDataCanBeNull()) { throw new Exception("Data cannot be null!"); $this->success = false; return $this->success; } else { $this->success = true; return $this->success; } } //test for empty data if ('' == $data) { if (!$this->getDataCanBeEmpty()) { throw new Exception("Data cannot be null!"); $this->success = false; return $this->success; } else { $this->success = true; return $this->success; } } } //make sure we're starting from beginning of array //if we've looped through partial criteria validating an item //and fail, then the next time through the criteria array //will only validate from where the previous run stopped //example that will break if we don't have this: //$validator = new EZValidator(); //$validator->addCriteria(__EZV_INTEGER_REGEXP); //$validator->addCriteria("<30"); //$validator->addCriteria(">8"); //$this->assertFalse($validator->validate("8")); //$this->assertFalse($validator->validate("30")); reset($this->criteria_arr); while (list($key, $crit) = each($this->criteria_arr)) { switch ($this->type_arr[$key]) { case __EZV_REGEXP: if (!$regExpValidator->validate($data, $crit)) { return false; } else { break; } case __EZV_COMPEXP: if (!$compExpValidator->validate($data, $crit)) { return false; } else { break; } case __EZV_DATEEXP: if (!$dateExpValidator->validate($data, $crit)) { return false; } else { break; } case __EZV_TIMEEXP: if (!$timeExpValidator->validate($data, $crit)) { return false; } else { break; } case __EZV_CURRENCYEXP: if (!$currencyExpValidator->validate($data, $crit)) { return false; } else { break; } //should never happen... // throw new Error("Cannot handle date or currency types at this time"); // return false; //should never happen... // throw new Error("Cannot handle date or currency types at this time"); // return false; case __EZV_UNKNOWN: default: if ($this->dieSilently) { return false; } else { //should never happen... throw new Error("Cannot determine type"); return false; } } } //if we've made it here, check passed in criteria_count //if we've made it here, this data has passed! return true; }