1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php
namespace oroboros\core\abstracts\libraries\validation\workers;
class AbstractFormValidationWorker
extends \oroboros\core\abstracts\libraries\validation\AbstractValidationWorker
implements \oroboros\core\interfaces\api\RegexApi
{
const WORKER_TYPE = 'form';
private $_errors = array();
private $_schema = array();
private $_verdict = FALSE;
private $_details = array();
private $_raw;
public function validate($data, array $schema) {
$this->_raw = $data;
$this->_schema = $schema;
foreach ($data as $key => $value) {
$this->_details[$key] = $this->_evaluateFormData($key, $value);
}
$result = \oroboros\Common::cast('object', array(
"valid" => (!(in_array(FALSE, $this->_details)) ? TRUE : FALSE),
"verdict" => ((in_array(FALSE, $this->_details)) ? $this->_details : TRUE),
"raw" => $this->_raw,
));
return $result;
}
protected function _evaluateFormData($key, $value) {
if (!isset($this->_schema['required']) || !isset($this->_schema['required'][$key])) {
$details = TRUE;
} else {
$schema = $this->_schema['required'][$key];
$schema = array_shift($schema);
$method = '_' . array_search($schema, $this->_schema['required'][$key]);
$details = $this->{$method}($value, $schema);
}
return $details;
}
private function _regex($subject, $pattern) {
$verdict = FALSE;
$check = function ($reg) {
if ((strpos($reg, '%') === 0)) {
return urldecode($reg);
} elseif (defined("self::" . $reg)) {
return constant("self::" . $reg);
} else {
return $reg;
}
};
$regex = ((is_array($pattern)) ? array_map($check, $pattern) : $check($pattern));
if (is_array($regex)) {
$result = array();
foreach ($regex as $key => $patternmatch) {
$result[$key] = $this->_regex($subject, $patternmatch);
}
$verdict = ((in_array(1, $result) || in_array(TRUE, $result)) ? TRUE : FALSE);
} else {
if (urldecode($regex) !== $regex) {
$pattern = urldecode($regex);
} elseif (defined('\\oroboros\\core\\libraries\\regex\\Regex::' . $regex)) {
$pattern = constant('\\oroboros\\core\\libraries\regex\\Regex::' . $regex);
}
$verdict = preg_match($regex, $subject);
}
return ($verdict) ? TRUE : FALSE;
}
private function _options($subject, $pattern) {
$verdict = FALSE;
$verdict = (in_array($subject, $pattern) ? TRUE : FALSE);
return $verdict;
}
private function _match($subject, $pattern) {
$verdict = TRUE;
return $verdict;
}
}