constDATA_NOT_FOUND='Data not found. Run setData()';constFILTER_NOT_FOUND='Filter not found. Run setFilter()';constVALIDATOR_NOT_FOUND='Validator not found. Run setValidator()';protected$filter;protected$validator;protected$data;publicfunctionsetFilter(Filter$filter){$this->filter=$filter;}publicfunctionsetValidator(Validator$validator){$this->validator=$validator;}publicfunctionsetData($data){$this->data=$data;}
public function validate()
{
if (!$this->data)
throw new RuntimeException(self::DATA_NOT_FOUND);
if (!$this->validator)
throw new RuntimeException(self::VALIDATOR_NOT_FOUND);
$valid = $this->validator->process($this->data);
foreach ($this->elements as $element) {
if (isset($this->validator->getResults()
[$element->getName()])) {
$element->setErrors($this->validator->getResults()
[$element->getName()]->messages);
}
}
return $valid;
}
public function filter()
{
if (!$this->data)
throw new RuntimeException(self::DATA_NOT_FOUND);
if (!$this->filter)
throw new RuntimeException(self::FILTER_NOT_FOUND);
$this->filter->process($this->data);
foreach ($this->filter->getResults() as $key => $result) {
if (isset($this->elements[$key])) {
$this->elements[$key]
->setSingleAttribute('value', $result->item);
if (isset($result->messages)
&& count($result->messages)) {
foreach ($result->messages as $message) {
$this->elements[$key]->addSingleError($message);
}
}
}
}
}