diff --git a/code/NetefxValidator.php b/code/NetefxValidator.php index 01d97a4..4f25b31 100644 --- a/code/NetefxValidator.php +++ b/code/NetefxValidator.php @@ -9,78 +9,83 @@ * @author zauberfisch */ -class NetefxValidator extends Validator { - - protected $rules; - protected $javascriptValidationHandler = 'none'; +class NetefxValidator extends Validator +{ + + protected $rules; + protected $javascriptValidationHandler = 'none'; - /** - * @return array - */ - public function getRules() { - return $this->rules; - } - - /** - * if the first parameter is not an array, or we have more than one parameter, collate all parameters to an array, otherwise use the passed array - * @param array|mixed $items - */ - public function setRules($items = null) { - $this->rules = (!is_array($items) || count(func_get_args()) > 1) ? func_get_args() : $items; - } - - /** - * if the first parameter is not an array, or we have more than one parameter, collate all parameters to an array, otherwise use the passed array - * @param array|mixed $items - */ - public function __construct() { - $this->setRules(func_get_args()); - parent::__construct(); - - } + /** + * @return array + */ + public function getRules() + { + return $this->rules; + } + + /** + * if the first parameter is not an array, or we have more than one parameter, collate all parameters to an array, otherwise use the passed array + * @param array|mixed $items + */ + public function setRules($items = null) + { + $this->rules = (!is_array($items) || count(func_get_args()) > 1) ? func_get_args() : $items; + } + + /** + * if the first parameter is not an array, or we have more than one parameter, collate all parameters to an array, otherwise use the passed array + * @param array|mixed $items + */ + public function __construct() + { + $this->setRules(func_get_args()); + parent::__construct(); + } - - /** - * javascript not implemented yet - * @return string - */ - function javascript() { - $js = ""; - return $js; - } + + /** + * javascript not implemented yet + * @return string + */ + public function javascript() + { + $js = ""; + return $js; + } - /** - * calls validate() on all fields and rules - * @return boolean - */ - function php($data) { - $valid = true; + /** + * calls validate() on all fields and rules + * @return boolean + */ + public function php($data) + { + $valid = true; - $fields = $this->form->Fields(); - foreach($fields as $field) { - $valid = ($field->validate($this) && $valid); - } - if($this->rules) { - foreach($this->rules as $rule) { - - if (!$rule->validate($data)) { - $errorMessage = $rule->getErrorMessage(); - $errorMessageType = $rule->getErrorMessageType(); - $fieldName = $rule->getField(); - - $this->validationError( - $fieldName, - $errorMessage, - $errorMessageType - ); - $valid = false; - } - } - } + $fields = $this->form->Fields(); + foreach ($fields as $field) { + $valid = ($field->validate($this) && $valid); + } + if ($this->rules) { + foreach ($this->rules as $rule) { + if (!$rule->validate($data)) { + $errorMessage = $rule->getErrorMessage(); + $errorMessageType = $rule->getErrorMessageType(); + $fieldName = $rule->getField(); + + $this->validationError( + $fieldName, + $errorMessage, + $errorMessageType + ); + $valid = false; + } + } + } - if (!$valid) Session::set("NetefxValidatorError",true); + if (!$valid) { + Session::set("NetefxValidatorError", true); + } - return $valid; - } - + return $valid; + } } diff --git a/code/NetefxValidatorRule.php b/code/NetefxValidatorRule.php index b612c0a..d5dd2bd 100644 --- a/code/NetefxValidatorRule.php +++ b/code/NetefxValidatorRule.php @@ -5,133 +5,148 @@ * @author lx-berlin * @author zauberfisch */ -abstract class NetefxValidatorRule extends Object { - protected $field; - protected $args; - protected $errorMsg; - protected $errorMsgType; - - /** - * @return string - */ - public function getField() { - return $this->field; - } - /** - * @return array - */ - public function getArgs() { - return $this->args; - } - /** - * @return string - */ - public function getErrorMessage() { - return $this->errorMsg; - } - /** - * @return string - */ - public function getErrorMessageType() { - return $this->errorMsgType; - } - /** - * Name of the Field that this rule is applied to - * @param string $fieldName - */ - public function setField($fieldName) { - $this->field = $fieldName; - } - /** - * Arguments differ depending on the Rule - * @param mixed $args - */ - public function setArgs($args) { - $this->args = (is_array($args))?$args:array($args); - } - /** - * @param string $message - */ - public function setErrorMessage($message) { - $this->errorMsg = $message; - } - /** - * CSS class which will be added to the field if it is not valid (eg: 'validatonError') - * @param string $type - */ - public function setErrorMessageType($type) { - $this->errorMsgType = $type; - } - - /** - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param mixed $args additional arguments needed in speical rules like FUNCTION - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct(); - $this->setField($field); - $this->setErrorMessage($errorMsg); - $this->setArgs($args); - $this->setErrorMessageType($errorMsgType); - } - - /** - * evaluates the given expression, having names of other fields included in the characters @ and ~ resp. - * Since you need numeric expressions to do arithmetic operations and for security reasons you have to call checkNumeric() before. - * @param array $data - * @param string $expr - * @return string - */ - protected function evaluate($data,$expr) { - $expr = str_replace ('@','$data["',$expr); - $expr = str_replace ('~','"]',$expr); - $expr = "return ".$expr.";"; - return eval($expr); - } - - /** - * @param array $data - * @param string $expr - * @return boolean - */ - protected function checkNumeric($data,$expr) { - $pos = -1; - $pos2 = -1; - do { - $pos = strpos ($expr,'@', $pos+1); - $pos2 = strpos ($expr,'~', $pos2+1); - if ($pos !== false) { - $fieldname = substr ($expr,$pos+1,$pos2-$pos-1); - if (!is_numeric($data[$fieldname])) - return false; - } else { - return true; - } - } - while (true); - } - +abstract class NetefxValidatorRule extends Object +{ + protected $field; + protected $args; + protected $errorMsg; + protected $errorMsgType; + + /** + * @return string + */ + public function getField() + { + return $this->field; + } + /** + * @return array + */ + public function getArgs() + { + return $this->args; + } + /** + * @return string + */ + public function getErrorMessage() + { + return $this->errorMsg; + } + /** + * @return string + */ + public function getErrorMessageType() + { + return $this->errorMsgType; + } + /** + * Name of the Field that this rule is applied to + * @param string $fieldName + */ + public function setField($fieldName) + { + $this->field = $fieldName; + } + /** + * Arguments differ depending on the Rule + * @param mixed $args + */ + public function setArgs($args) + { + $this->args = (is_array($args))?$args:array($args); + } + /** + * @param string $message + */ + public function setErrorMessage($message) + { + $this->errorMsg = $message; + } + /** + * CSS class which will be added to the field if it is not valid (eg: 'validatonError') + * @param string $type + */ + public function setErrorMessageType($type) + { + $this->errorMsgType = $type; + } + + /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param mixed $args additional arguments needed in speical rules like FUNCTION + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct(); + $this->setField($field); + $this->setErrorMessage($errorMsg); + $this->setArgs($args); + $this->setErrorMessageType($errorMsgType); + } + + /** + * evaluates the given expression, having names of other fields included in the characters @ and ~ resp. + * Since you need numeric expressions to do arithmetic operations and for security reasons you have to call checkNumeric() before. + * @param array $data + * @param string $expr + * @return string + */ + protected function evaluate($data, $expr) + { + $expr = str_replace('@', '$data["', $expr); + $expr = str_replace('~', '"]', $expr); + $expr = "return ".$expr.";"; + return eval($expr); + } + + /** + * @param array $data + * @param string $expr + * @return boolean + */ + protected function checkNumeric($data, $expr) + { + $pos = -1; + $pos2 = -1; + do { + $pos = strpos($expr, '@', $pos+1); + $pos2 = strpos($expr, '~', $pos2+1); + if ($pos !== false) { + $fieldname = substr($expr, $pos+1, $pos2-$pos-1); + if (!is_numeric($data[$fieldname])) { + return false; + } + } else { + return true; + } + } while (true); + } + /** * converts custom number format to english number format (eg: german uses , instead of . for the decimal seperator) * @param string $number * @param string $separator (eg: "." or ",") */ - protected function numberFormatConversion($number, $separator) { - if (preg_match("/^[0-9".$separator."]{1,}$/", $number)>0) { - $number = str_replace($separator,".",$number); - return $number; - } - return false; + protected function numberFormatConversion($number, $separator) + { + if (preg_match("/^[0-9".$separator."]{1,}$/", $number)>0) { + $number = str_replace($separator, ".", $number); + return $number; + } + return false; } - /** + /** * This function needs to be overwritten * @param array $data * @return boolean */ abstract public function validate($data); - - public function jsValidation() {} -} \ No newline at end of file + + public function jsValidation() + { + } +} diff --git a/code/libraries/NetefxValidatorLibraryCheckbox.php b/code/libraries/NetefxValidatorLibraryCheckbox.php index 2376a77..16511de 100644 --- a/code/libraries/NetefxValidatorLibraryCheckbox.php +++ b/code/libraries/NetefxValidatorLibraryCheckbox.php @@ -7,72 +7,80 @@ * @package NetefxValidator */ -class NetefxValidatorLibraryCheckbox { +class NetefxValidatorLibraryCheckbox +{ -// ************************ Often used functions for validation ********************** - + // ************************ Often used functions for validation ********************** + // *** at least x checkboxes of a CheckboxField must be checked *** - static function min_number_checkboxes_checked ($data, $args) { - - - - $field = $args["field"]; + public static function min_number_checkboxes_checked($data, $args) + { + $field = $args["field"]; $min = (int)$args["min"]; - if (($data[$field]=="") AND ($min>0)) return false; + if (($data[$field]=="") and ($min>0)) { + return false; + } - $items = explode(",",$data[$field]); + $items = explode(",", $data[$field]); $checked = 0; - foreach ($items AS $key => $value) { + foreach ($items as $key => $value) { $checked++; } return ($checked >= $min); } // *** maximal x checkboxes of a CheckboxField can be checked *** - static function max_number_checkboxes_checked ($data, $args) { + public static function max_number_checkboxes_checked($data, $args) + { $field = $args["field"]; $max = (int)$args["max"]; - if ($data[$field]=="") return true; + if ($data[$field]=="") { + return true; + } - $items = explode(",",$data[$field]); + $items = explode(",", $data[$field]); $checked = 0; - foreach ($items AS $key => $value) { + foreach ($items as $key => $value) { $checked++; } - return ($checked <= $max); + return ($checked <= $max); } // *** at least x checkboxes of a (Has|Many)ManyDataObjectManager must be checked *** - static function min_number_many_DOM_checked ($data, $args) { - - $field = $args["field"]; + public static function min_number_many_DOM_checked($data, $args) + { + $field = $args["field"]; $min = (int)$args["min"]; //if ($field=="TargetCountries") debug::show($data[$field]); - - if (($data[$field]=="") AND ($min>0)) return false; + + if (($data[$field]=="") and ($min>0)) { + return false; + } $checked = 0; - foreach ($data[$field] AS $key => $value) { + foreach ($data[$field] as $key => $value) { $checked++; } return ($checked >= $min+1); } // *** maximal x checkboxes of a (Has|Many)ManyDataObjectManager can be checked *** - static function max_number_many_DOM_checked ($data, $args) { - - $field = $args["field"]; + public static function max_number_many_DOM_checked($data, $args) + { + $field = $args["field"]; $max = (int)$args["max"]; - if ($data[$field]=="") return true; + if ($data[$field]=="") { + return true; + } $checked = 0; - foreach ($data[$field] AS $key => $value) { + foreach ($data[$field] as $key => $value) { $checked++; } return ($checked <= $max+1); @@ -84,26 +92,25 @@ static function max_number_many_DOM_checked ($data, $args) { * @example $rule_excludedPersons_notInvited = new NetefxValidatorRuleFUNCTION ("excludedPersons", "you cannot invite a person and exclude her as well", 'error', * array('NetefxValidatorLibraryCheckbox', 'checkboxes_no_overlapping', array('field' => 'excludedPersons', otherField' => 'invitedPersons'))); */ - static function checkboxes_no_overlapping ($data, $args) { - - $field1 = $args["field"]; - $field2 = $args["otherField"]; - - if (($data[$field1]=="") OR ($data[$field2]=="")) { - return true; - } - - $items1 = explode(",",$data[$field1]); + public static function checkboxes_no_overlapping($data, $args) + { + $field1 = $args["field"]; + $field2 = $args["otherField"]; + + if (($data[$field1]=="") or ($data[$field2]=="")) { + return true; + } - $items2 = explode(",",$data[$field2]); + $items1 = explode(",", $data[$field1]); - foreach ($items2 AS $key => $value) { - if (in_array ($value, $items1)) { - return false; - } + $items2 = explode(",", $data[$field2]); + + foreach ($items2 as $key => $value) { + if (in_array($value, $items1)) { + return false; + } } - - return true; - } - -} \ No newline at end of file + + return true; + } +} diff --git a/code/libraries/NetefxValidatorLibraryDate.php b/code/libraries/NetefxValidatorLibraryDate.php index 6772c03..d5b2175 100644 --- a/code/libraries/NetefxValidatorLibraryDate.php +++ b/code/libraries/NetefxValidatorLibraryDate.php @@ -7,27 +7,29 @@ * @package NetefxValidator */ -class NetefxValidatorLibraryDate { +class NetefxValidatorLibraryDate +{ -// ************************ Often used functions for validation ********************** - + // ************************ Often used functions for validation ********************** + /** Date is at least x days in past * * $rule_past_1 = new NetefxValidatorRuleFUNCTION ("End", "End must be in past",'error', * array('NetefxValidatorLibraryDate', 'DateIsMinDaysBeforeToday', array('date' => 'End', 'min' => 1))); */ - static function DateIsMinDaysBeforeToday ($data, $args) { - $field_date = $args["date"]; + public static function DateIsMinDaysBeforeToday($data, $args) + { + $field_date = $args["date"]; // A) date is defined as a date (YYYY-MM-DD) - if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/',$field_date)) { - $date = $args["date"]; + if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/', $field_date)) { + $date = $args["date"]; } // B) date is defined as the name of another inputfield else { - $date = $data[$field_date]; + $date = $data[$field_date]; } $today1 = getdate(); @@ -35,13 +37,15 @@ static function DateIsMinDaysBeforeToday ($data, $args) { $min = (int)$args["min"]; - if ($date=="") return false; + if ($date=="") { + return false; + } $timestamp_dateFrom = strtotime($date); $timestamp_dateUntil = strtotime($today); $days_dif = ($timestamp_dateUntil - $timestamp_dateFrom)/86400; - return ($days_dif >= $min); + return ($days_dif >= $min); } @@ -52,16 +56,17 @@ static function DateIsMinDaysBeforeToday ($data, $args) { * */ - static function DateIsMinDaysAfterToday ($data, $args) { - $field_date = $args["date"]; + public static function DateIsMinDaysAfterToday($data, $args) + { + $field_date = $args["date"]; // A) date is defined as a date (YYYY-MM-DD) - if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/',$field_date)) { - $date = $args["date"]; + if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/', $field_date)) { + $date = $args["date"]; } // B) date is defined as the name of another inputfield else { - $date = $data[$field_date]; + $date = $data[$field_date]; } $today1 = getdate(); @@ -69,13 +74,15 @@ static function DateIsMinDaysAfterToday ($data, $args) { $min = (int)$args["min"]; - if ($date=="") return false; + if ($date=="") { + return false; + } $timestamp_dateUntil = strtotime($date); $timestamp_dateFrom = strtotime($today); $days_dif = ($timestamp_dateUntil - $timestamp_dateFrom)/86400; - return ($days_dif >= $min); + return ($days_dif >= $min); } /** date B is at least x days after date A @@ -85,23 +92,25 @@ static function DateIsMinDaysAfterToday ($data, $args) { * */ - static function UntilIsMinDaysAfterFrom ($data, $args) { - + public static function UntilIsMinDaysAfterFrom($data, $args) + { $field_dateFrom = $args["dateFrom"]; // A) dateFrom is defined as a date (YYYY-MM-DD) - if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/',$field_dateFrom)) { - $dateFrom = $args["dateFrom"]; + if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/', $field_dateFrom)) { + $dateFrom = $args["dateFrom"]; } // B) dateFrom is defined as the name of another inputfield else { - $dateFrom = $data[$field_dateFrom]; + $dateFrom = $data[$field_dateFrom]; } $dateUntil = $data[$args["dateUntil"]]; $min = (int)$args["min"]; - if (($dateFrom=="") OR ($dateUntil=="")) return false; + if (($dateFrom=="") or ($dateUntil=="")) { + return false; + } $timestamp_dateFrom = strtotime($dateFrom); $timestamp_dateUntil = strtotime($dateUntil); @@ -114,24 +123,28 @@ static function UntilIsMinDaysAfterFrom ($data, $args) { * date B is at least x days after date A (both empty allowed) * */ - static function UntilIsMinDaysAfterFromOptional ($data, $args) { - + public static function UntilIsMinDaysAfterFromOptional($data, $args) + { $field_dateFrom = $args["dateFrom"]; // A) dateFrom is defined as a date (YYYY-MM-DD) - if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/',$field_dateFrom)) { - $dateFrom = $args["dateFrom"]; + if (preg_match('/[\d]{4}+[\-]+[\d]{2}+[\-]+[\d]{2}/', $field_dateFrom)) { + $dateFrom = $args["dateFrom"]; } // B) dateFrom is defined as the name of another inputfield else { - $dateFrom = $data[$field_dateFrom]; + $dateFrom = $data[$field_dateFrom]; } $dateUntil = $data[$args["dateUntil"]]; $min = (int)$args["min"]; - if (($dateFrom=="") AND ($dateUntil=="")) return true; - if (($dateFrom=="") OR ($dateUntil=="")) return false; + if (($dateFrom=="") and ($dateUntil=="")) { + return true; + } + if (($dateFrom=="") or ($dateUntil=="")) { + return false; + } $timestamp_dateFrom = strtotime($dateFrom); $timestamp_dateUntil = strtotime($dateUntil); @@ -139,5 +152,4 @@ static function UntilIsMinDaysAfterFromOptional ($data, $args) { return ($days_dif >= $min); } - -} \ No newline at end of file +} diff --git a/code/libraries/NetefxValidatorLibraryFile.php b/code/libraries/NetefxValidatorLibraryFile.php index 227c259..977fede 100644 --- a/code/libraries/NetefxValidatorLibraryFile.php +++ b/code/libraries/NetefxValidatorLibraryFile.php @@ -7,29 +7,31 @@ * @package NetefxValidator */ -class NetefxValidatorLibraryFile { +class NetefxValidatorLibraryFile +{ -// ************************ Often used functions for validation ********************** - + // ************************ Often used functions for validation ********************** + /** check for allowed mime types for an upload (if exists) * * @example $rule_file_1 = new NetefxValidatorRuleFUNCTION("File", "wrong file type", 'error', * array('NetefxValidatorLibraryFile', 'check_mime_types', array('field' => 'File', 'allowedMimeTypes' => array("application/pdf","application/msword","x-unknown/x-unknown")))); */ - static function check_mime_types ($data, $args) { + public static function check_mime_types($data, $args) + { $field = $args["field"]; $arr_file = $data[$field]; $allowedMimeTypes = $args["allowedMimeTypes"]; - if ($allowedMimeTypes AND $arr_file["type"]) { - if (!in_array($arr_file["type"],$allowedMimeTypes)) { - return false; - } + if ($allowedMimeTypes and $arr_file["type"]) { + if (!in_array($arr_file["type"], $allowedMimeTypes)) { + return false; + } } - return true; + return true; } /** check if file for an upload exists (for required uploads) @@ -37,34 +39,36 @@ static function check_mime_types ($data, $args) { * @example $rule_image_1 = new NetefxValidatorRuleFUNCTION("Image", "you need to upload a photo", 'error', * array('NetefxValidatorLibraryFile','check_file_exists', array('field' => 'Image'))); */ - static function check_file_exists ($data, $args) { + public static function check_file_exists($data, $args) + { $field = $args["field"]; - $arr_file = $data[$field]; - return ($arr_file["error"]==0); + $arr_file = $data[$field]; + return ($arr_file["error"]==0); } /** check if file has correct size (0 for unlimited values) * * @example $rule_file_2 = new NetefxValidatorRuleFUNCTION("File", "Maximal file size is 10000 bytes", 'error' * array('NetefxValidatorLibraryFile', 'check_file_size', array('field' => 'File', 'minSize' => 0, 'maxSize' => 10000))); - */ + */ - static function check_file_size ($data, $args) { + public static function check_file_size($data, $args) + { $field = $args["field"]; $arr_file = $data[$field]; - $file_size = $arr_file["size"]; + $file_size = $arr_file["size"]; - $minSize = $args["minSize"]; - $maxSize = $args["maxSize"]; - - if ($minSize AND $file_size < $minSize) { - return false; + $minSize = $args["minSize"]; + $maxSize = $args["maxSize"]; + + if ($minSize and $file_size < $minSize) { + return false; + } + if ($maxSize and $file_size > $maxSize) { + return false; } - if ($maxSize AND $file_size > $maxSize) { - return false; - } - return true; + return true; } /** check if image has correct width and height (0 for unlimited values) @@ -72,42 +76,39 @@ static function check_file_size ($data, $args) { * @example $rule_image_3 = new NetefxValidatorRuleFUNCTION("Image", "image width at least 10, image height at most 500", 'error', * array('NetefxValidatorLibraryFile', 'check_image_size', * array('field' => 'Image', 'minWidth' => 10, 'maxWidth' => 0, 'minHeight' => 0, 'maxHeight' => 500))); - */ + */ - static function check_image_size ($data, $args) { + public static function check_image_size($data, $args) + { $field = $args["field"]; $arr_file = $data[$field]; if ($arr_file["tmp_name"]) { - $image_size = getImageSize($arr_file["tmp_name"]); - - if ($image_size) { - $width= $image_size[0]; - $height= $image_size[1]; - - $minWidth = $args["minWidth"]; - $minHeight = $args["minHeight"]; - $maxWidth = $args["maxWidth"]; - $maxHeight = $args["maxHeight"]; - - if ($minWidth AND $width < $minWidth) { - return false; - } - if ($minHeight AND $height < $minHeight) { - return false; - } - if ($maxWidth AND $width > $maxWidth) { - return false; - } - if ($maxHeight AND $height > $maxHeight) { - return false; - } - } - } + $image_size = getImageSize($arr_file["tmp_name"]); + + if ($image_size) { + $width= $image_size[0]; + $height= $image_size[1]; + + $minWidth = $args["minWidth"]; + $minHeight = $args["minHeight"]; + $maxWidth = $args["maxWidth"]; + $maxHeight = $args["maxHeight"]; + + if ($minWidth and $width < $minWidth) { + return false; + } + if ($minHeight and $height < $minHeight) { + return false; + } + if ($maxWidth and $width > $maxWidth) { + return false; + } + if ($maxHeight and $height > $maxHeight) { + return false; + } + } + } - return true; + return true; } - - - - -} \ No newline at end of file +} diff --git a/code/rules/NetefxValidatorRuleAND.php b/code/rules/NetefxValidatorRuleAND.php index 99a0882..1dba5ed 100644 --- a/code/rules/NetefxValidatorRuleAND.php +++ b/code/rules/NetefxValidatorRuleAND.php @@ -5,34 +5,41 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleAND extends NetefxValidatorRule { - /** - * Check if all of the subrules are valid +class NetefxValidatorRuleAND extends NetefxValidatorRule +{ + /** + * Check if all of the subrules are valid * @example $rule = new NetefxValidatorRuleAND('FieldName', 'This field must contain at least 3 characters and contain the word "Netefx"', null, array($rule1, $rule2)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param NetefxValidatorRule|array $rules single rule or array of rules - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $rules); - if ($this->getErrorMessage() === null) { - $messages = array(); - foreach ($this->getArgs() as $rule) - $messages[] = $rule->getErrorMessage(); - if (count($messages)) - $this->setErrorMessage(implode(_t('NetefxValidatorRuleAND.and', ' and '), $messages)); - } - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param NetefxValidatorRule|array $rules single rule or array of rules + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $rules); + if ($this->getErrorMessage() === null) { + $messages = array(); + foreach ($this->getArgs() as $rule) { + $messages[] = $rule->getErrorMessage(); + } + if (count($messages)) { + $this->setErrorMessage(implode(_t('NetefxValidatorRuleAND.and', ' and '), $messages)); + } + } + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - foreach ($this->args as $rule) - if (!($rule->validate($data))) - return false; - return true; - } -} \ No newline at end of file + */ + public function validate($data) + { + foreach ($this->args as $rule) { + if (!($rule->validate($data))) { + return false; + } + } + return true; + } +} diff --git a/code/rules/NetefxValidatorRuleBETWEEN.php b/code/rules/NetefxValidatorRuleBETWEEN.php index 76bb36b..328f22b 100644 --- a/code/rules/NetefxValidatorRuleBETWEEN.php +++ b/code/rules/NetefxValidatorRuleBETWEEN.php @@ -5,33 +5,39 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleBETWEEN extends NetefxValidatorRule { - /** - * check if field value is numeric and between the 2 given values or expressions +class NetefxValidatorRuleBETWEEN extends NetefxValidatorRule +{ + /** + * check if field value is numeric and between the 2 given values or expressions * @example $rule = new NetefxValidatorRuleBETWEEN('Number', 'Insert a number btween 10 and 20', null, array(10, 20)); * @example $rule = new NetefxValidatorRuleBETWEEN('Number', 'Insert a number btween 10 and 20, use "," as decimal seperator', null, array(10, 20, ',')); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args (the 3rd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args (the 3rd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[2]) ? $this->args[2] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - if (!$this->checkNumeric($data,$this->args[1])) - return false; - return (($data[$this->field] <= $this->evaluate($data,$this->args[1])) && - ($data[$this->field] >= $this->evaluate($data,$this->args[0]))); - } -} \ No newline at end of file + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[1])) { + return false; + } + return (($data[$this->field] <= $this->evaluate($data, $this->args[1])) && + ($data[$this->field] >= $this->evaluate($data, $this->args[0]))); + } +} diff --git a/code/rules/NetefxValidatorRuleCHARACTERSBETWEEN.php b/code/rules/NetefxValidatorRuleCHARACTERSBETWEEN.php index 95ef21c..079cc20 100644 --- a/code/rules/NetefxValidatorRuleCHARACTERSBETWEEN.php +++ b/code/rules/NetefxValidatorRuleCHARACTERSBETWEEN.php @@ -5,29 +5,34 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleCHARACTERSBETWEEN extends NetefxValidatorRule { - /** - * Check if field length is between 2 values - * @example $rule = new NetefxValidatorRuleCHARACTERSBETWEEN("FirstName", 'Please enter between 2 and 20 characters', null, array(2, 20)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args array(min, max) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** +class NetefxValidatorRuleCHARACTERSBETWEEN extends NetefxValidatorRule +{ + /** + * Check if field length is between 2 values + * @example $rule = new NetefxValidatorRuleCHARACTERSBETWEEN("FirstName", 'Please enter between 2 and 20 characters', null, array(2, 20)); + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args array(min, max) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - if (!$this->checkNumeric($data,$this->args[0])) - return false; - if (!$this->checkNumeric($data,$this->args[1])) - return false; - return ((strlen(trim($data[$this->field])) >= $this->evaluate($data,$this->args[0])) && - (strlen(trim($data[$this->field])) <= $this->evaluate($data,$this->args[1]))); - } -} \ No newline at end of file + */ + public function validate($data) + { + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[1])) { + return false; + } + return ((strlen(trim($data[$this->field])) >= $this->evaluate($data, $this->args[0])) && + (strlen(trim($data[$this->field])) <= $this->evaluate($data, $this->args[1]))); + } +} diff --git a/code/rules/NetefxValidatorRuleEMPTY.php b/code/rules/NetefxValidatorRuleEMPTY.php index 964e8de..c2fe9ac 100644 --- a/code/rules/NetefxValidatorRuleEMPTY.php +++ b/code/rules/NetefxValidatorRuleEMPTY.php @@ -5,27 +5,32 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleEMPTY extends NetefxValidatorRule { - /** - * Empty Check on given field, returned true if the field is empty +class NetefxValidatorRuleEMPTY extends NetefxValidatorRule +{ + /** + * Empty Check on given field, returned true if the field is empty * @example $rule = new NetefxValidatorRuleEMPTY("SomeField", "this field should be empty"); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error') { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleEMPTY.ErrorMessage', '%s needs to be empty'), - $field - ); - parent::__construct($field, $errorMsg, $errorMsgType, array()); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error') + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleEMPTY.ErrorMessage', '%s needs to be empty'), + $field + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, array()); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { return ($data[$this->field] == ''); } -} \ No newline at end of file +} diff --git a/code/rules/NetefxValidatorRuleEQUALS.php b/code/rules/NetefxValidatorRuleEQUALS.php index 5f33d14..0303439 100644 --- a/code/rules/NetefxValidatorRuleEQUALS.php +++ b/code/rules/NetefxValidatorRuleEQUALS.php @@ -5,30 +5,35 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleEQUALS extends NetefxValidatorRule { - /** - * Check if field value is numeric and equal to the given value or expression +class NetefxValidatorRuleEQUALS extends NetefxValidatorRule +{ + /** + * Check if field value is numeric and equal to the given value or expression * @example see NetefxValidator::validateGREATER() for examples * @see NetefxValidatorRule::validateTEXTIS() for text equals validation - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return ($data[$this->field] == $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + */ + public function validate($data) + { + $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return ($data[$this->field] == $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleEXISTS.php b/code/rules/NetefxValidatorRuleEXISTS.php index 971fcd1..8cd20da 100644 --- a/code/rules/NetefxValidatorRuleEXISTS.php +++ b/code/rules/NetefxValidatorRuleEXISTS.php @@ -5,23 +5,26 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleEXISTS extends NetefxValidatorRule { - /** - * Check if given field exists on the form +class NetefxValidatorRuleEXISTS extends NetefxValidatorRule +{ + /** + * Check if given field exists on the form * @example $ruleEXISTS = new NetefxValidatorRuleEXISTS('email');
$ruleREQUIRED = new NetefxValidatorRuleREQUIRED ('email');
$rule = new NetefxValidatorRuleIMPLIES ("email", "This field is required", "error", array($ruleEXISTS, $ruleREQUIRED)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error') { - parent::__construct($field, $errorMsg, $errorMsgType, array()); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error') + { + parent::__construct($field, $errorMsg, $errorMsgType, array()); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return (isset($data[$this->field])); + */ + public function validate($data) + { + return (isset($data[$this->field])); } -} \ No newline at end of file +} diff --git a/code/rules/NetefxValidatorRuleFUNCTION.php b/code/rules/NetefxValidatorRuleFUNCTION.php index 42a2bd4..9ca26e3 100644 --- a/code/rules/NetefxValidatorRuleFUNCTION.php +++ b/code/rules/NetefxValidatorRuleFUNCTION.php @@ -5,46 +5,55 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleFUNCTION extends NetefxValidatorRule { - /** - * Use a custom function to validate this field (the validator will pass 2 params to the custom function, first $data and as second the params you gave to the rule) - * @example $code = 'if ($data["myField"] == "test") return true; else return false;'; $rule = new NetefxValidatorRuleFUNCTION("myField", 'the value must be test', null, $code); - * @example // this will call NetefxValidatorLibrary::min_number_checkboxes_checked($data, $args);
$class = 'NetefxValidatorLibrary'
$function = 'min_number_checkboxes_checked';
$args = array('field' => 'myField', 'min' => 5);
$rule = new NetefxValidatorRuleFUNCTION('myField', array($class, $method, $args), 'at least 5 checkboxes need to be checked'); - * @example // this will call $object->myValidationMethod($data, $args);
$object = new MyObject();
$function = 'myValidationMethod';
$args = array('fieldName' => 'myField', 'someOtherThing' => 'yay');
$rule = new NetefxValidatorRuleFUNCTION('myField', 'this field is required', null, array($object, $function, $args)); - * @example $function = create_function('$data,$args', 'if ($data["myField"] == "test" && $args["someOtherThing"]) return true; else return false;');
$args = array('someOtherThing' => true);
$rule = new NetefxValidatorRuleFUNCTION("myField", 'this field is required', null, array($function, $args)); - * @example // since PHP 5.3.0
$function = function ($data, $args) {
$fieldName = $args['fieldName'];
if ($data[$fieldName] == 'test')
return true;
else
return false;
}
$args = array('fieldName' => 'myField', 'someOtherThing' => 'yay');
$rule = new NetefxValidatorRuleFUNCTION('myField', 'this field is required', null, array($function, $args)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param string|callback|array string code to eval (assing return value to $return), callback a callable function or array (see examples) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** - * @param array $data - * @return boolean - */ - public function validate($data) { - $args = $this->getArgs(); - $params = array( - 'data' => $data, - 'args' => null - ); - if (is_callable($args[0])) { - // Anonymous function - $function = $args[0]; - if (count($args) > 1) $params['args'] = $args[1]; - } elseif ((is_string($args[0]) || is_object($args[0])) && isset($args[1]) && is_string($args[1])) { - // classname + static method name OR object + method name to be called by call_user_func_array - $function = array($args[0], $args[1]); - if (count($args) > 2) $params['args'] = $args[2]; - } else { - // code seems to be a string, run it with eval - if (count($args) > 1) $params['args'] = $args[1]; - return eval($args[0]); - } - return call_user_func_array($function, $params); - } -} \ No newline at end of file +class NetefxValidatorRuleFUNCTION extends NetefxValidatorRule +{ + /** + * Use a custom function to validate this field (the validator will pass 2 params to the custom function, first $data and as second the params you gave to the rule) + * @example $code = 'if ($data["myField"] == "test") return true; else return false;'; $rule = new NetefxValidatorRuleFUNCTION("myField", 'the value must be test', null, $code); + * @example // this will call NetefxValidatorLibrary::min_number_checkboxes_checked($data, $args);
$class = 'NetefxValidatorLibrary'
$function = 'min_number_checkboxes_checked';
$args = array('field' => 'myField', 'min' => 5);
$rule = new NetefxValidatorRuleFUNCTION('myField', array($class, $method, $args), 'at least 5 checkboxes need to be checked'); + * @example // this will call $object->myValidationMethod($data, $args);
$object = new MyObject();
$function = 'myValidationMethod';
$args = array('fieldName' => 'myField', 'someOtherThing' => 'yay');
$rule = new NetefxValidatorRuleFUNCTION('myField', 'this field is required', null, array($object, $function, $args)); + * @example $function = create_function('$data,$args', 'if ($data["myField"] == "test" && $args["someOtherThing"]) return true; else return false;');
$args = array('someOtherThing' => true);
$rule = new NetefxValidatorRuleFUNCTION("myField", 'this field is required', null, array($function, $args)); + * @example // since PHP 5.3.0
$function = function ($data, $args) {
$fieldName = $args['fieldName'];
if ($data[$fieldName] == 'test')
return true;
else
return false;
}
$args = array('fieldName' => 'myField', 'someOtherThing' => 'yay');
$rule = new NetefxValidatorRuleFUNCTION('myField', 'this field is required', null, array($function, $args)); + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param string|callback|array string code to eval (assing return value to $return), callback a callable function or array (see examples) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** + * @param array $data + * @return boolean + */ + public function validate($data) + { + $args = $this->getArgs(); + $params = array( + 'data' => $data, + 'args' => null + ); + if (is_callable($args[0])) { + // Anonymous function + $function = $args[0]; + if (count($args) > 1) { + $params['args'] = $args[1]; + } + } elseif ((is_string($args[0]) || is_object($args[0])) && isset($args[1]) && is_string($args[1])) { + // classname + static method name OR object + method name to be called by call_user_func_array + $function = array($args[0], $args[1]); + if (count($args) > 2) { + $params['args'] = $args[2]; + } + } else { + // code seems to be a string, run it with eval + if (count($args) > 1) { + $params['args'] = $args[1]; + } + return eval($args[0]); + } + return call_user_func_array($function, $params); + } +} diff --git a/code/rules/NetefxValidatorRuleGREATER.php b/code/rules/NetefxValidatorRuleGREATER.php index 2700503..2e5cf34 100644 --- a/code/rules/NetefxValidatorRuleGREATER.php +++ b/code/rules/NetefxValidatorRuleGREATER.php @@ -5,37 +5,44 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleGREATER extends NetefxValidatorRule { - /** - * check if field value is numeric and greater than the given value or expression +class NetefxValidatorRuleGREATER extends NetefxValidatorRule +{ + /** + * check if field value is numeric and greater than the given value or expression * @example $rule = new NetefxValidatorRuleGREATER('FieldName', 'Insert a number greater than 10', null, 10); * @example $rule = new NetefxValidatorRuleGREATER('FieldName', 'Insert a number greater than 10 and use "," as decimal seperator', null, array(10, ',')); * @example $rule = new NetefxValidatorRuleGREATER('C', 'C > must be greater than "2 * FieldA + FieldB"', null, '2*@A~+@B~'); * @todo the first couple of lines are the same for all greater and smaller validations, they should be moved to a seperated function - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleGREATER.ErrorMessage', '%s needs to be greater than %s'), - $field, - $this->args[0] - ); - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleGREATER.ErrorMessage', '%s needs to be greater than %s'), + $field, + $this->args[0] + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return ($data[$this->field] > $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return ($data[$this->field] > $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleGREATEREQUAL.php b/code/rules/NetefxValidatorRuleGREATEREQUAL.php index 0b82c9f..6380439 100644 --- a/code/rules/NetefxValidatorRuleGREATEREQUAL.php +++ b/code/rules/NetefxValidatorRuleGREATEREQUAL.php @@ -5,34 +5,41 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleGREATEREQUAL extends NetefxValidatorRule { - /** - * Check if field value is numeric and greater or equal to the given value or expression +class NetefxValidatorRuleGREATEREQUAL extends NetefxValidatorRule +{ + /** + * Check if field value is numeric and greater or equal to the given value or expression * @example see NetefxValidator::validateGREATER() for examples - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleGREATEREQUAL.ErrorMessage', '%s needs to be greater or equal to %s'), - $field, - $this->args[0] - ); - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleGREATEREQUAL.ErrorMessage', '%s needs to be greater or equal to %s'), + $field, + $this->args[0] + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return ($data[$this->field] >= $this->evaluate($data,$this->args[0])); + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return ($data[$this->field] >= $this->evaluate($data, $this->args[0])); } -} \ No newline at end of file +} diff --git a/code/rules/NetefxValidatorRuleIMPLIES.php b/code/rules/NetefxValidatorRuleIMPLIES.php index 9c2c34b..b71aa86 100644 --- a/code/rules/NetefxValidatorRuleIMPLIES.php +++ b/code/rules/NetefxValidatorRuleIMPLIES.php @@ -5,26 +5,30 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleIMPLIES extends NetefxValidatorRule { - /** - * Check if first rule is valid the second needs to be valid as well. If first rule is not valid true will be returned. +class NetefxValidatorRuleIMPLIES extends NetefxValidatorRule +{ + /** + * Check if first rule is valid the second needs to be valid as well. If first rule is not valid true will be returned. * @example $rule = new NetefxValidatorRuleIMPLIES('Username', 'If you choose credit card as payment method, it is required to enter your credit card number', null, array($subRule1, $subRule2)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args 2 sub rules - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args 2 sub rules + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - if ($this->args[0]->validate($data)) - return ($this->args[1]->validate($data)); - return true; - } -} \ No newline at end of file + */ + public function validate($data) + { + if ($this->args[0]->validate($data)) { + return ($this->args[1]->validate($data)); + } + return true; + } +} diff --git a/code/rules/NetefxValidatorRuleINARRAY.php b/code/rules/NetefxValidatorRuleINARRAY.php index 8641f30..a8f9255 100644 --- a/code/rules/NetefxValidatorRuleINARRAY.php +++ b/code/rules/NetefxValidatorRuleINARRAY.php @@ -5,27 +5,32 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleINARRAY extends NetefxValidatorRule { - /** - * Check if the field is equal to one of the given strings +class NetefxValidatorRuleINARRAY extends NetefxValidatorRule +{ + /** + * Check if the field is equal to one of the given strings * @example $rule = new NetefxValidatorRuleINARRAY('Flat', 'Only Paris and Berlin are available at the moment.', null, array('Paris', 'Berlin')); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args array of strings|ints to be checked - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args array of strings|ints to be checked + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - foreach ($this->args as $text) - if (strcmp($data[$this->field],$text)==0) - return true; - return false; - } -} \ No newline at end of file + */ + public function validate($data) + { + foreach ($this->args as $text) { + if (strcmp($data[$this->field], $text)==0) { + return true; + } + } + return false; + } +} diff --git a/code/rules/NetefxValidatorRuleMAXCHARACTERS.php b/code/rules/NetefxValidatorRuleMAXCHARACTERS.php index f1b59b1..c6d90d6 100644 --- a/code/rules/NetefxValidatorRuleMAXCHARACTERS.php +++ b/code/rules/NetefxValidatorRuleMAXCHARACTERS.php @@ -5,26 +5,30 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleMAXCHARACTERS extends NetefxValidatorRule { - /** - * Check length of the field +class NetefxValidatorRuleMAXCHARACTERS extends NetefxValidatorRule +{ + /** + * Check length of the field * @example $rule = new NetefxValidatorRuleMAXCHARACTERS('FirstName', 'Please enter no more than two characters', null, 2); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int $max - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $max = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $max); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int $max + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $max = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $max); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return (strlen(trim($data[$this->field])) <= $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + */ + public function validate($data) + { + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return (strlen(trim($data[$this->field])) <= $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleMINCHARACTERS.php b/code/rules/NetefxValidatorRuleMINCHARACTERS.php index 24bf552..bb666ca 100644 --- a/code/rules/NetefxValidatorRuleMINCHARACTERS.php +++ b/code/rules/NetefxValidatorRuleMINCHARACTERS.php @@ -5,26 +5,30 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleMINCHARACTERS extends NetefxValidatorRule { - /** - * Check length of the field +class NetefxValidatorRuleMINCHARACTERS extends NetefxValidatorRule +{ + /** + * Check length of the field * @example $rule = new NetefxValidatorRuleMINCHARACTERS('FirstName', 'Please enter at least two characters', null, 2); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int $min - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $min = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $min); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int $min + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $min = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $min); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return (strlen(trim($data[$this->field])) >= $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + */ + public function validate($data) + { + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return (strlen(trim($data[$this->field])) >= $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleNOT.php b/code/rules/NetefxValidatorRuleNOT.php index 8d79ae9..cd7ceea 100644 --- a/code/rules/NetefxValidatorRuleNOT.php +++ b/code/rules/NetefxValidatorRuleNOT.php @@ -5,24 +5,27 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleNOT extends NetefxValidatorRule { - /** - * returns true if the subrule is not valid +class NetefxValidatorRuleNOT extends NetefxValidatorRule +{ + /** + * returns true if the subrule is not valid * @example $rule = new NetefxValidatorRuleNOT ('Username', 'Your Username can not contain "Testuser"', null, $subRule); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param NetefxValidatorRule $rule - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rule = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $rule); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param NetefxValidatorRule $rule + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rule = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $rule); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return (!($this->args[0]->validate($data))); - } -} \ No newline at end of file + */ + public function validate($data) + { + return (!($this->args[0]->validate($data))); + } +} diff --git a/code/rules/NetefxValidatorRuleNOTINARRAY.php b/code/rules/NetefxValidatorRuleNOTINARRAY.php index 8638b4a..937e16f 100644 --- a/code/rules/NetefxValidatorRuleNOTINARRAY.php +++ b/code/rules/NetefxValidatorRuleNOTINARRAY.php @@ -5,27 +5,32 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleNOTINARRAY extends NetefxValidatorRule { - /** - * Check if the field is not equal to one of the given strings +class NetefxValidatorRuleNOTINARRAY extends NetefxValidatorRule +{ + /** + * Check if the field is not equal to one of the given strings * @example $rule = new NetefxValidatorRuleNOTINARRAY('Flat', 'Paris and Berlin are not available at the moment.', null, array('Paris', 'Berlin')); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args array of strings|ints to be checked - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args array of strings|ints to be checked + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - foreach ($this->args as $text) - if (strcmp($data[$this->field],$text)==0) - return false; - return true; - } -} \ No newline at end of file + */ + public function validate($data) + { + foreach ($this->args as $text) { + if (strcmp($data[$this->field], $text)==0) { + return false; + } + } + return true; + } +} diff --git a/code/rules/NetefxValidatorRuleOR.php b/code/rules/NetefxValidatorRuleOR.php index 1e52a62..4290768 100644 --- a/code/rules/NetefxValidatorRuleOR.php +++ b/code/rules/NetefxValidatorRuleOR.php @@ -5,34 +5,41 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleOR extends NetefxValidatorRule { - /** - * Check if at least 1 of the subrules is valid +class NetefxValidatorRuleOR extends NetefxValidatorRule +{ + /** + * Check if at least 1 of the subrules is valid * @example $rule = new NetefxValidatorRuleOR('FieldName', 'This field must contain at least 3 characters or be empty', null, array($rule1, $rule2)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param NetefxValidatorRule|array $rules single rule or array of rules - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $rules); - if ($this->getErrorMessage() === null) { - $messages = array(); - foreach ($this->getArgs() as $rule) - $messages[] = $rule->getErrorMessage(); - if (count($messages)) - $this->setErrorMessage(implode(_t('NetefxValidatorRuleOR.or', ' or '), $messages)); - } - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param NetefxValidatorRule|array $rules single rule or array of rules + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $rules); + if ($this->getErrorMessage() === null) { + $messages = array(); + foreach ($this->getArgs() as $rule) { + $messages[] = $rule->getErrorMessage(); + } + if (count($messages)) { + $this->setErrorMessage(implode(_t('NetefxValidatorRuleOR.or', ' or '), $messages)); + } + } + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - foreach ($this->args as $rule) - if ($rule->validate($data)) - return true; - return false; - } -} \ No newline at end of file + */ + public function validate($data) + { + foreach ($this->args as $rule) { + if ($rule->validate($data)) { + return true; + } + } + return false; + } +} diff --git a/code/rules/NetefxValidatorRuleREGEXP.php b/code/rules/NetefxValidatorRuleREGEXP.php index f9736a8..a96eb1c 100644 --- a/code/rules/NetefxValidatorRuleREGEXP.php +++ b/code/rules/NetefxValidatorRuleREGEXP.php @@ -5,25 +5,28 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleREGEXP extends NetefxValidatorRule { - /** - * Check if the given expression matches +class NetefxValidatorRuleREGEXP extends NetefxValidatorRule +{ + /** + * Check if the given expression matches * @example $rule = new NetefxValidatorRuleREGEXP('Name', 'This field is required', null, '/^.{2,}$/'); * @uses preg_match() - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param string $expression - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $expression = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $expression); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param string $expression + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $expression = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $expression); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return preg_match($this->args[0], $data[$this->field]) > 0; - } -} \ No newline at end of file + */ + public function validate($data) + { + return preg_match($this->args[0], $data[$this->field]) > 0; + } +} diff --git a/code/rules/NetefxValidatorRuleREQUIRED.php b/code/rules/NetefxValidatorRuleREQUIRED.php index a8c509f..0802c83 100644 --- a/code/rules/NetefxValidatorRuleREQUIRED.php +++ b/code/rules/NetefxValidatorRuleREQUIRED.php @@ -5,27 +5,32 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleREQUIRED extends NetefxValidatorRule { - /** - * Check if the given field is filled out +class NetefxValidatorRuleREQUIRED extends NetefxValidatorRule +{ + /** + * Check if the given field is filled out * @example $rule = new NetefxValidatorRuleREQUIRED("FirstName", "FirstName is required", null); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error') { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleREQUIRED.ErrorMessage', '%s is required'), - $field - ); - parent::__construct($field, $errorMsg, $errorMsgType, array()); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error') + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleREQUIRED.ErrorMessage', '%s is required'), + $field + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, array()); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { return (trim($data[$this->field]) != ''); } -} \ No newline at end of file +} diff --git a/code/rules/NetefxValidatorRuleSMALLER.php b/code/rules/NetefxValidatorRuleSMALLER.php index 5557690..af7b4c9 100644 --- a/code/rules/NetefxValidatorRuleSMALLER.php +++ b/code/rules/NetefxValidatorRuleSMALLER.php @@ -5,34 +5,41 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleSMALLER extends NetefxValidatorRule { - /** - * Check if field value is numeric and smaller than the given value or expression +class NetefxValidatorRuleSMALLER extends NetefxValidatorRule +{ + /** + * Check if field value is numeric and smaller than the given value or expression * @example see NetefxValidator::validateGREATER() for examples - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleSMALLER.ErrorMessage', '%s needs to be smaller than %s'), - $field, - $this->args[0] - ); - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleSMALLER.ErrorMessage', '%s needs to be smaller than %s'), + $field, + $this->args[0] + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { + */ + public function validate($data) + { $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return ($data[$this->field] < $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return ($data[$this->field] < $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleSMALLEREQUAL.php b/code/rules/NetefxValidatorRuleSMALLEREQUAL.php index 860d701..626d622 100644 --- a/code/rules/NetefxValidatorRuleSMALLEREQUAL.php +++ b/code/rules/NetefxValidatorRuleSMALLEREQUAL.php @@ -5,34 +5,41 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleSMALLEREQUAL extends NetefxValidatorRule { - /** - * Check if field value is numeric and smaller or equal to the given value or expression +class NetefxValidatorRuleSMALLEREQUAL extends NetefxValidatorRule +{ + /** + * Check if field value is numeric and smaller or equal to the given value or expression * @example see NetefxValidator::validateGREATER() for examples - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - if ($errorMsg === null) $errorMsg = sprintf( - _t('NetefxValidatorRuleSMALLEREQUAL.ErrorMessage', '%s needs to be smaller or equal to %s'), - $field, - $this->args[0] - ); - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param int|string|array $args number, expression or array (if array, the 2nd value in the array can set what character is used as decimal point) + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + if ($errorMsg === null) { + $errorMsg = sprintf( + _t('NetefxValidatorRuleSMALLEREQUAL.ErrorMessage', '%s needs to be smaller or equal to %s'), + $field, + $this->args[0] + ); + } + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); - if (!is_numeric($data[$this->field])) - return false; - if (!$this->checkNumeric($data,$this->args[0])) - return false; - return ($data[$this->field] <= $this->evaluate($data,$this->args[0])); - } -} \ No newline at end of file + */ + public function validate($data) + { + $data[$this->field] = $this->numberFormatConversion($data[$this->field], (isset($this->args[1]) ? $this->args[1] : ".")); + if (!is_numeric($data[$this->field])) { + return false; + } + if (!$this->checkNumeric($data, $this->args[0])) { + return false; + } + return ($data[$this->field] <= $this->evaluate($data, $this->args[0])); + } +} diff --git a/code/rules/NetefxValidatorRuleTEXTCONTAINS.php b/code/rules/NetefxValidatorRuleTEXTCONTAINS.php index a9e6f53..58cb154 100644 --- a/code/rules/NetefxValidatorRuleTEXTCONTAINS.php +++ b/code/rules/NetefxValidatorRuleTEXTCONTAINS.php @@ -5,24 +5,27 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleTEXTCONTAINS extends NetefxValidatorRule { - /** - * Check if the field contains a string +class NetefxValidatorRuleTEXTCONTAINS extends NetefxValidatorRule +{ + /** + * Check if the field contains a string * @example $rule = new NetefxValidatorRuleTEXTCONTAINS('Company', 'Company must contain "Netefx"', null, 'Netefx'); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param string $needle - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $needle = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $needle); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param string $needle + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $needle = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $needle); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return (strpos($data[$this->field],$this->args[0]) !== false); - } -} \ No newline at end of file + */ + public function validate($data) + { + return (strpos($data[$this->field], $this->args[0]) !== false); + } +} diff --git a/code/rules/NetefxValidatorRuleTEXTEQUALS.php b/code/rules/NetefxValidatorRuleTEXTEQUALS.php index 2e4e8de..b40c35f 100644 --- a/code/rules/NetefxValidatorRuleTEXTEQUALS.php +++ b/code/rules/NetefxValidatorRuleTEXTEQUALS.php @@ -5,25 +5,28 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleTEXTEQUALS extends NetefxValidatorRule { - /** - * Check if 2 fields are equal +class NetefxValidatorRuleTEXTEQUALS extends NetefxValidatorRule +{ + /** + * Check if 2 fields are equal * @example $rule = new NetefxValidatorRuleTEXTEQUALS('Password2', 'Password', 'Password und Password2 need to match'); * @see NetefxValidatorRuleTEXTIS to check if the field is equal to a string - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param string $otherField - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $otherField = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $otherField); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param string $otherField + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $otherField = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $otherField); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return (strcmp($data[$this->field],$data[$this->args[0]])==0); - } -} \ No newline at end of file + */ + public function validate($data) + { + return (strcmp($data[$this->field], $data[$this->args[0]])==0); + } +} diff --git a/code/rules/NetefxValidatorRuleTEXTIS.php b/code/rules/NetefxValidatorRuleTEXTIS.php index 2b5cbaa..3366067 100644 --- a/code/rules/NetefxValidatorRuleTEXTIS.php +++ b/code/rules/NetefxValidatorRuleTEXTIS.php @@ -5,24 +5,27 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleTEXTIS extends NetefxValidatorRule { - /** - * Check if the field is equal to a string +class NetefxValidatorRuleTEXTIS extends NetefxValidatorRule +{ + /** + * Check if the field is equal to a string * @example $rule = new NetefxValidatorRuleTEXTIS('Company', 'Netefx', 'Company must be "Netefx"'); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param string $str - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $str = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $str); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param string $str + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $str = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $str); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - return (strcmp($data[$this->field],$this->args[0])==0); - } -} \ No newline at end of file + */ + public function validate($data) + { + return (strcmp($data[$this->field], $this->args[0])==0); + } +} diff --git a/code/rules/NetefxValidatorRuleUNIQUE.php b/code/rules/NetefxValidatorRuleUNIQUE.php index b2acfed..eadf01d 100644 --- a/code/rules/NetefxValidatorRuleUNIQUE.php +++ b/code/rules/NetefxValidatorRuleUNIQUE.php @@ -5,40 +5,45 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleUNIQUE extends NetefxValidatorRule { - /** - * Check if any other DataObject has the same value already - * @example $rule = new NetefxValidatorRuleUNIQUE('Nickname', 'This Nickname is already in use.', null, array('Member', 'Nickname', 'MemberID', 'MemberID')); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param array $args first argument is the classname, the second the DB field name on the class, the third is optional the ID to excluse from the uniqe rule or the name of another field in the form (default is field ID in the form), the 4th is optional the DB field name to check for the exclude id - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $args); - } - - /** - * @param array $data - * @return boolean - */ - public function validate($data) { - $newValue = Convert::raw2sql($data[$this->field]); - $classCame = $this->args[0]; - $fieldName = $this->args[1]; - if (isset($this->args[2]) && is_numeric($this->args[2])) { - $id = $this->args[2]; - } else { - $idFieldInForm = (isset($this->args[2]))?$this->args[2]:'ID'; - $id = (isset($data[$idFieldInForm]))?Convert::raw2sql($data[$idFieldInForm]):false; - } - $idFieldInDB = (isset($this->args[3]))?$this->args[3]:'ID'; - if (is_string($id)) - $id = "'$id'"; - $filter = "\"$fieldName\" = '$newValue'"; - if ((is_numeric($id) && $id > 0) || is_string($id)) - $filter .= " AND \"$idFieldInDB\" <> $id"; - $otherEntry = DataObject::get_one($classCame, $filter); - return ($otherEntry && $otherEntry->exists()) ? false : true; - } -} \ No newline at end of file +class NetefxValidatorRuleUNIQUE extends NetefxValidatorRule +{ + /** + * Check if any other DataObject has the same value already + * @example $rule = new NetefxValidatorRuleUNIQUE('Nickname', 'This Nickname is already in use.', null, array('Member', 'Nickname', 'MemberID', 'MemberID')); + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param array $args first argument is the classname, the second the DB field name on the class, the third is optional the ID to excluse from the uniqe rule or the name of another field in the form (default is field ID in the form), the 4th is optional the DB field name to check for the exclude id + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $args = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $args); + } + + /** + * @param array $data + * @return boolean + */ + public function validate($data) + { + $newValue = Convert::raw2sql($data[$this->field]); + $classCame = $this->args[0]; + $fieldName = $this->args[1]; + if (isset($this->args[2]) && is_numeric($this->args[2])) { + $id = $this->args[2]; + } else { + $idFieldInForm = (isset($this->args[2]))?$this->args[2]:'ID'; + $id = (isset($data[$idFieldInForm]))?Convert::raw2sql($data[$idFieldInForm]):false; + } + $idFieldInDB = (isset($this->args[3]))?$this->args[3]:'ID'; + if (is_string($id)) { + $id = "'$id'"; + } + $filter = "\"$fieldName\" = '$newValue'"; + if ((is_numeric($id) && $id > 0) || is_string($id)) { + $filter .= " AND \"$idFieldInDB\" <> $id"; + } + $otherEntry = DataObject::get_one($classCame, $filter); + return ($otherEntry && $otherEntry->exists()) ? false : true; + } +} diff --git a/code/rules/NetefxValidatorRuleXOR.php b/code/rules/NetefxValidatorRuleXOR.php index bbbf527..b027dca 100644 --- a/code/rules/NetefxValidatorRuleXOR.php +++ b/code/rules/NetefxValidatorRuleXOR.php @@ -5,31 +5,36 @@ * @author lx-berlin * @author zauberfisch */ -class NetefxValidatorRuleXOR extends NetefxValidatorRule { - /** - * returns true if exactly 1 of the subrules is valid, more or less will return false +class NetefxValidatorRuleXOR extends NetefxValidatorRule +{ + /** + * returns true if exactly 1 of the subrules is valid, more or less will return false * @example $rule = new NetefxValidatorRuleXOR ('FieldName', 'Please choose exactly 1 option.', null, array($subRule1, $subRule2, $subRule3, ...)); - * @param string $field name of the field - * @param string $errorMsg the message to be displayed - * @param string $errorMsgType the css class added to the field on validation error - * @param NetefxValidatorRule|array $rules single rule or array of rules - */ - public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) { - parent::__construct($field, $errorMsg, $errorMsgType, $rules); - } - - /** + * @param string $field name of the field + * @param string $errorMsg the message to be displayed + * @param string $errorMsgType the css class added to the field on validation error + * @param NetefxValidatorRule|array $rules single rule or array of rules + */ + public function __construct($field, $errorMsg = null, $errorMsgType = 'error', $rules = null) + { + parent::__construct($field, $errorMsg, $errorMsgType, $rules); + } + + /** * @param array $data * @return boolean - */ - public function validate($data) { - $valid = false; - foreach ($this->args as $rule) { - if ($rule->validate($data)) { - if ($valid) return false; - $valid = true; - } - } - return $valid; - } -} \ No newline at end of file + */ + public function validate($data) + { + $valid = false; + foreach ($this->args as $rule) { + if ($rule->validate($data)) { + if ($valid) { + return false; + } + $valid = true; + } + } + return $valid; + } +} diff --git a/code/tests/NetefxValidatorTest.php b/code/tests/NetefxValidatorTest.php index 7fbc126..1688f2e 100644 --- a/code/tests/NetefxValidatorTest.php +++ b/code/tests/NetefxValidatorTest.php @@ -5,885 +5,917 @@ * @author lx-berlin * @author Zauberfisch */ -class NetefxValidatorTest extends SapphireTest { - - public static $fixture_file = 'NetefxValidator/code/tests/NetefxValidatorTest.yml'; - - public function testREQUIRED() { - $rule = new NetefxValidatorRuleREQUIRED('testField'); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - } - - public function testEMPTY() { - $rule = new NetefxValidatorRuleEMPTY('testField'); - - $data = array('testField' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertFalse($rule->validate($data)); - } - - public function testEXISTS() { - $rule = new NetefxValidatorRuleEXISTS('testField'); - - $data = array(); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - } - - public function testOR() { - $rule = new NetefxValidatorRuleOR('testFieldOR', null, null, array( - new NetefxValidatorRuleREQUIRED('testField'), - new NetefxValidatorRuleEMPTY('testField2'), - new NetefxValidatorRuleEXISTS('testField3') - )); - - $data = array('testField' => 'test', 'testField2' => 'test'); - $this->assertTrue($rule->validate($data)); // rule 1 is valid - - $data = array('testField' => '', 'testField2' => ''); - $this->assertTrue($rule->validate($data)); // rule 2 is valid - - $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 3 is valid - - - $data = array('testField' => 'test', 'testField2' => ''); - $this->assertTrue($rule->validate($data)); // rule 1 & 2 are valid - - $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 2 & 3 are valid - - $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 1 & 3 is valid - - - $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 1 & 2 & 3 is valid - - $data = array('testField' => '', 'testField2' => 'test'); - $this->assertFalse($rule->validate($data)); // none valid - - } - - - public function testAND() { - $rule = new NetefxValidatorRuleAND('testFieldAND', null, null, array( - new NetefxValidatorRuleREQUIRED('testField'), - new NetefxValidatorRuleEMPTY('testField2'), - new NetefxValidatorRuleEXISTS('testField3') - )); - - $data = array('testField' => 'test', 'testField2' => 'test'); - $this->assertFalse($rule->validate($data)); // rule 1 is valid - - $data = array('testField' => '', 'testField2' => ''); - $this->assertFalse($rule->validate($data)); // rule 2 is valid - - $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 3 is valid - - - $data = array('testField' => 'test', 'testField2' => ''); - $this->assertFalse($rule->validate($data)); // rule 1 & 2 are valid - - $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 2 & 3 are valid - - $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 1 & 3 is valid - - - $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 1 & 2 & 3 is valid - - $data = array('testField' => '', 'testField2' => 'test'); - $this->assertFalse($rule->validate($data)); // none valid - } - - public function testXOR() { - $rule = new NetefxValidatorRuleXOR('testFieldXOR', null, null, array( - new NetefxValidatorRuleREQUIRED('testField'), - new NetefxValidatorRuleEMPTY('testField2'), - new NetefxValidatorRuleEXISTS('testField3') - )); - - $data = array('testField' => 'test', 'testField2' => 'test'); - $this->assertTrue($rule->validate($data)); // rule 1 is valid - - $data = array('testField' => '', 'testField2' => ''); - $this->assertTrue($rule->validate($data)); // rule 2 is valid - - $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); - $this->assertTrue($rule->validate($data)); // rule 3 is valid - - - $data = array('testField' => 'test', 'testField2' => ''); - $this->assertFalse($rule->validate($data)); // rule 1 & 2 are valid - - $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 2 & 3 are valid - - $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 1 & 3 is valid - - - $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); - $this->assertFalse($rule->validate($data)); // rule 1 & 2 & 3 is valid - - $data = array('testField' => '', 'testField2' => 'test'); - $this->assertFalse($rule->validate($data)); // none valid - } - - public function testNOT() { - $rule = new NetefxValidatorRuleNOT('testField', null, null, new NetefxValidatorRuleREQUIRED('testField2')); - - $data = array('testField2' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField2' => 'test'); - $this->assertFalse($rule->validate($data)); - } - - - public function testIMPLIES() { - $rule = new NetefxValidatorRuleIMPLIES('testFieldIMPLIES', null, null, array(new NetefxValidatorRuleREQUIRED('testField'), new NetefxValidatorRuleREQUIRED('testField2'))); - - $data = array('testField' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '', 'testField2' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '', 'testField2' => 'test'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test', 'testField2' => 'test'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test', 'testField2' => ''); - $this->assertFalse($rule->validate($data)); - } - - public function testGREATER() { - $rule = new NetefxValidatorRuleGREATER('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - - - $rule = new NetefxValidatorRuleGREATER('testField', null, null, array(10, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - } - - - public function testGREATEREQUAL() { - $rule = new NetefxValidatorRuleGREATEREQUAL('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - - - $rule = new NetefxValidatorRuleGREATEREQUAL('testField', null, null, array(10, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - } - - public function testSMALLER() { - $rule = new NetefxValidatorRuleSMALLER('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - - - $rule = new NetefxValidatorRuleSMALLER('testField', null, null, array(10, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - } - - public function testSMALLEREQUAL() { - $rule = new NetefxValidatorRuleSMALLEREQUAL('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - - - $rule = new NetefxValidatorRuleSMALLEREQUAL('testField', null, null, array(10, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - } - - public function testEQUALS() { - $rule = new NetefxValidatorRuleEQUALS('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - - - $rule = new NetefxValidatorRuleEQUALS('testField', null, null, array(10, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertFalse($rule->validate($data)); - } - - public function testBETWEEN() { - $rule = new NetefxValidatorRuleBETWEEN('testField', null, null, array(10, 20)); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '19'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '20'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '20.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '20,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '21'); - $this->assertFalse($rule->validate($data)); - - $rule = new NetefxValidatorRuleBETWEEN('testField', null, null, array(10, 20, ',')); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9.9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '9,9'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.0'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,0'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '10.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '10,1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '11'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '19'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '20'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '20.1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '20,1'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '21'); - $this->assertFalse($rule->validate($data)); - } - - public function testREGEXP() { - $rule = new NetefxValidatorRuleREGEXP('testField'); - // TODO write this test - } - - public function testTEXTEQUALS() { - $rule = new NetefxValidatorRuleTEXTEQUALS('testField', null, null, 'testField2'); - - $data = array('testField' => '', 'testField2' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '', 'testField2' => ' '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test', 'testField2' => 'test'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test', 'testField2' => 'Test'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test', 'testField2' => 'test2'); - $this->assertFalse($rule->validate($data)); - } - - public function testTEXTIS() { - $rule = new NetefxValidatorRuleTEXTIS('testField', null, null, 'test'); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'TeSt'); - $this->assertFalse($rule->validate($data)); - - - $rule = new NetefxValidatorRuleTEXTIS('testField', null, null, ''); - - $data = array('testField' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => ' '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertFalse($rule->validate($data)); - } - - public function testTEXTCONTAINS() { - $rule = new NetefxValidatorRuleTEXTCONTAINS('testField', null, null, 'test'); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'TeSt'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'testtestttest'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'xtesxtx'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'testxx'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'xxtestxx'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'xxtest'); - $this->assertTrue($rule->validate($data)); - } - - public function testINARRAY() { - $rule = new NetefxValidatorRuleINARRAY('testField', null, null, array('zauberfisch', 'is', 'awesum')); - - $data = array('testField' => 'zauberfisch is awesum'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'awesum'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => ' is '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'fisch'); - $this->assertFalse($rule->validate($data)); - } - - public function testNOTINARRAY() { - $rule = new NetefxValidatorRuleNOTINARRAY('testField', null, null, array('zauberfisch', 'is', 'awesum')); - - $data = array('testField' => 'zauberfisch is awesum'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'awesum'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => ' is '); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'fisch'); - $this->assertTrue($rule->validate($data)); - } - - public function testMINCHARACTERS() { - $rule = new NetefxValidatorRuleMINCHARACTERS('testField', null, null, 10); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '123456789'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '1234567890'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '12345678901'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'testtest '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => ' testtest '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test test '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test test1'); - $this->assertTrue($rule->validate($data)); - } - - public function testMAXCHARACTERS() { - $rule = new NetefxValidatorRuleMAXCHARACTERS('testField', null, null, 9); - - $data = array('testField' => ''); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '123456789'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '1234567890'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '12345678901'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => ' testtest '); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test test '); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test test1'); - $this->assertFalse($rule->validate($data)); - } - - public function testCHARACTERSBETWEEN() { - $rule = new NetefxValidatorRuleCHARACTERSBETWEEN('testField', null, null, array(9, 11)); - - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '12345678'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => '123456789'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '1234567890'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '12345678901'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => ' testtest '); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'test test '); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test test1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test test 1'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => 'test test 12'); - $this->assertFalse($rule->validate($data)); - } - - public function testUNIQUE() { - $rule = new NetefxValidatorRuleUNIQUE('testField', null, null, array('Page', 'URLSegment')); - - $data = array('testField' => 'home'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 'siteThatDoesNotExist'); - $this->assertTrue($rule->validate($data)); - - $data = array('testField' => '1-1-test-product'); - $this->assertFalse($rule->validate($data)); - - $data = array('testField' => 1930); - $this->assertFalse($rule->validate($data)); - - - $rule = new NetefxValidatorRuleUNIQUE('testField', null, null, array('Page', 'URLSegment', 'testField', 'URLSegment')); - - $data = array('testField' => 'home'); - $this->assertTrue($rule->validate($data)); - } - - public function testFUNCTION() { - $code = 'if ($data["testField"] == "test") $return = true; else $return = false;'; - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, $code); - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $class = 'NetefxValidatorTest'; - $function = 'helper_FUNCTION_var'; - $args = false; - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($class, $function, $args)); - $data = array('testField' => 'test'); - $this->assertFalse($rule->validate($data)); - - $class = 'NetefxValidatorTest'; - $function = 'helper_FUNCTION_var'; - $args = true; - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($class, $function, $args)); - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - - $object = new NetefxValidatorTest(); - $function = 'helperFUNCTIONvar'; - $args = false; - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($object, $function, $args)); - $data = array('testField' => 'test'); - $this->assertFalse($rule->validate($data)); - - $object = new NetefxValidatorTest(); - $function = 'helperFUNCTIONvar'; - $args = true; - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($object, $function, $args)); - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - - $function = create_function('$data,$args', 'if ($data["testField"] == "test") return true; else return false;'); - $args = array(); - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($function, $args)); - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - - $function = function($data, $args) { - if ($data['testField'] == 'test') - return true; - else - return false; - }; - $args = array(); - $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($function, $args)); - $data = array('testField' => 'test'); - $this->assertTrue($rule->validate($data)); - $data = array('testField' => ''); - $this->assertFalse($rule->validate($data)); - } - - public static function helper_FUNCTION_var($data = null, $args = null) { return $args; } - public function helperFUNCTIONvar($data = null, $args = null) { return $args; } -} \ No newline at end of file +class NetefxValidatorTest extends SapphireTest +{ + + public static $fixture_file = 'NetefxValidator/code/tests/NetefxValidatorTest.yml'; + + public function testREQUIRED() + { + $rule = new NetefxValidatorRuleREQUIRED('testField'); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + } + + public function testEMPTY() + { + $rule = new NetefxValidatorRuleEMPTY('testField'); + + $data = array('testField' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertFalse($rule->validate($data)); + } + + public function testEXISTS() + { + $rule = new NetefxValidatorRuleEXISTS('testField'); + + $data = array(); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + } + + public function testOR() + { + $rule = new NetefxValidatorRuleOR('testFieldOR', null, null, array( + new NetefxValidatorRuleREQUIRED('testField'), + new NetefxValidatorRuleEMPTY('testField2'), + new NetefxValidatorRuleEXISTS('testField3') + )); + + $data = array('testField' => 'test', 'testField2' => 'test'); + $this->assertTrue($rule->validate($data)); // rule 1 is valid + + $data = array('testField' => '', 'testField2' => ''); + $this->assertTrue($rule->validate($data)); // rule 2 is valid + + $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 3 is valid + + + $data = array('testField' => 'test', 'testField2' => ''); + $this->assertTrue($rule->validate($data)); // rule 1 & 2 are valid + + $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 2 & 3 are valid + + $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 1 & 3 is valid + + + $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 1 & 2 & 3 is valid + + $data = array('testField' => '', 'testField2' => 'test'); + $this->assertFalse($rule->validate($data)); // none valid + } + + + public function testAND() + { + $rule = new NetefxValidatorRuleAND('testFieldAND', null, null, array( + new NetefxValidatorRuleREQUIRED('testField'), + new NetefxValidatorRuleEMPTY('testField2'), + new NetefxValidatorRuleEXISTS('testField3') + )); + + $data = array('testField' => 'test', 'testField2' => 'test'); + $this->assertFalse($rule->validate($data)); // rule 1 is valid + + $data = array('testField' => '', 'testField2' => ''); + $this->assertFalse($rule->validate($data)); // rule 2 is valid + + $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 3 is valid + + + $data = array('testField' => 'test', 'testField2' => ''); + $this->assertFalse($rule->validate($data)); // rule 1 & 2 are valid + + $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 2 & 3 are valid + + $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 1 & 3 is valid + + + $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 1 & 2 & 3 is valid + + $data = array('testField' => '', 'testField2' => 'test'); + $this->assertFalse($rule->validate($data)); // none valid + } + + public function testXOR() + { + $rule = new NetefxValidatorRuleXOR('testFieldXOR', null, null, array( + new NetefxValidatorRuleREQUIRED('testField'), + new NetefxValidatorRuleEMPTY('testField2'), + new NetefxValidatorRuleEXISTS('testField3') + )); + + $data = array('testField' => 'test', 'testField2' => 'test'); + $this->assertTrue($rule->validate($data)); // rule 1 is valid + + $data = array('testField' => '', 'testField2' => ''); + $this->assertTrue($rule->validate($data)); // rule 2 is valid + + $data = array('testField' => '', 'testField2' => 'test', 'testField3' => ''); + $this->assertTrue($rule->validate($data)); // rule 3 is valid + + + $data = array('testField' => 'test', 'testField2' => ''); + $this->assertFalse($rule->validate($data)); // rule 1 & 2 are valid + + $data = array('testField' => '', 'testField2' => '', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 2 & 3 are valid + + $data = array('testField' => 'test', 'testField2' => 'test', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 1 & 3 is valid + + + $data = array('testField' => 'test', 'testField2' => '', 'testField3' => ''); + $this->assertFalse($rule->validate($data)); // rule 1 & 2 & 3 is valid + + $data = array('testField' => '', 'testField2' => 'test'); + $this->assertFalse($rule->validate($data)); // none valid + } + + public function testNOT() + { + $rule = new NetefxValidatorRuleNOT('testField', null, null, new NetefxValidatorRuleREQUIRED('testField2')); + + $data = array('testField2' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField2' => 'test'); + $this->assertFalse($rule->validate($data)); + } + + + public function testIMPLIES() + { + $rule = new NetefxValidatorRuleIMPLIES('testFieldIMPLIES', null, null, array(new NetefxValidatorRuleREQUIRED('testField'), new NetefxValidatorRuleREQUIRED('testField2'))); + + $data = array('testField' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '', 'testField2' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '', 'testField2' => 'test'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test', 'testField2' => 'test'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test', 'testField2' => ''); + $this->assertFalse($rule->validate($data)); + } + + public function testGREATER() + { + $rule = new NetefxValidatorRuleGREATER('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + + + $rule = new NetefxValidatorRuleGREATER('testField', null, null, array(10, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + } + + + public function testGREATEREQUAL() + { + $rule = new NetefxValidatorRuleGREATEREQUAL('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + + + $rule = new NetefxValidatorRuleGREATEREQUAL('testField', null, null, array(10, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + } + + public function testSMALLER() + { + $rule = new NetefxValidatorRuleSMALLER('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + + + $rule = new NetefxValidatorRuleSMALLER('testField', null, null, array(10, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + } + + public function testSMALLEREQUAL() + { + $rule = new NetefxValidatorRuleSMALLEREQUAL('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + + + $rule = new NetefxValidatorRuleSMALLEREQUAL('testField', null, null, array(10, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + } + + public function testEQUALS() + { + $rule = new NetefxValidatorRuleEQUALS('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + + + $rule = new NetefxValidatorRuleEQUALS('testField', null, null, array(10, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertFalse($rule->validate($data)); + } + + public function testBETWEEN() + { + $rule = new NetefxValidatorRuleBETWEEN('testField', null, null, array(10, 20)); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '19'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '20'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '20.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '20,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '21'); + $this->assertFalse($rule->validate($data)); + + $rule = new NetefxValidatorRuleBETWEEN('testField', null, null, array(10, 20, ',')); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9.9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '9,9'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.0'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,0'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '10.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '10,1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '11'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '19'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '20'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '20.1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '20,1'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '21'); + $this->assertFalse($rule->validate($data)); + } + + public function testREGEXP() + { + $rule = new NetefxValidatorRuleREGEXP('testField'); + // TODO write this test + } + + public function testTEXTEQUALS() + { + $rule = new NetefxValidatorRuleTEXTEQUALS('testField', null, null, 'testField2'); + + $data = array('testField' => '', 'testField2' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '', 'testField2' => ' '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test', 'testField2' => 'test'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test', 'testField2' => 'Test'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test', 'testField2' => 'test2'); + $this->assertFalse($rule->validate($data)); + } + + public function testTEXTIS() + { + $rule = new NetefxValidatorRuleTEXTIS('testField', null, null, 'test'); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'TeSt'); + $this->assertFalse($rule->validate($data)); + + + $rule = new NetefxValidatorRuleTEXTIS('testField', null, null, ''); + + $data = array('testField' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => ' '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertFalse($rule->validate($data)); + } + + public function testTEXTCONTAINS() + { + $rule = new NetefxValidatorRuleTEXTCONTAINS('testField', null, null, 'test'); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'TeSt'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'testtestttest'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'xtesxtx'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'testxx'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'xxtestxx'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'xxtest'); + $this->assertTrue($rule->validate($data)); + } + + public function testINARRAY() + { + $rule = new NetefxValidatorRuleINARRAY('testField', null, null, array('zauberfisch', 'is', 'awesum')); + + $data = array('testField' => 'zauberfisch is awesum'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'awesum'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => ' is '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'fisch'); + $this->assertFalse($rule->validate($data)); + } + + public function testNOTINARRAY() + { + $rule = new NetefxValidatorRuleNOTINARRAY('testField', null, null, array('zauberfisch', 'is', 'awesum')); + + $data = array('testField' => 'zauberfisch is awesum'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'awesum'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => ' is '); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'fisch'); + $this->assertTrue($rule->validate($data)); + } + + public function testMINCHARACTERS() + { + $rule = new NetefxValidatorRuleMINCHARACTERS('testField', null, null, 10); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '123456789'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '1234567890'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '12345678901'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'testtest '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => ' testtest '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test test '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test test1'); + $this->assertTrue($rule->validate($data)); + } + + public function testMAXCHARACTERS() + { + $rule = new NetefxValidatorRuleMAXCHARACTERS('testField', null, null, 9); + + $data = array('testField' => ''); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '123456789'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '1234567890'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '12345678901'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => ' testtest '); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test test '); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test test1'); + $this->assertFalse($rule->validate($data)); + } + + public function testCHARACTERSBETWEEN() + { + $rule = new NetefxValidatorRuleCHARACTERSBETWEEN('testField', null, null, array(9, 11)); + + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '12345678'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => '123456789'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '1234567890'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '12345678901'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => ' testtest '); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'test test '); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test test1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test test 1'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => 'test test 12'); + $this->assertFalse($rule->validate($data)); + } + + public function testUNIQUE() + { + $rule = new NetefxValidatorRuleUNIQUE('testField', null, null, array('Page', 'URLSegment')); + + $data = array('testField' => 'home'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 'siteThatDoesNotExist'); + $this->assertTrue($rule->validate($data)); + + $data = array('testField' => '1-1-test-product'); + $this->assertFalse($rule->validate($data)); + + $data = array('testField' => 1930); + $this->assertFalse($rule->validate($data)); + + + $rule = new NetefxValidatorRuleUNIQUE('testField', null, null, array('Page', 'URLSegment', 'testField', 'URLSegment')); + + $data = array('testField' => 'home'); + $this->assertTrue($rule->validate($data)); + } + + public function testFUNCTION() + { + $code = 'if ($data["testField"] == "test") $return = true; else $return = false;'; + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, $code); + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $class = 'NetefxValidatorTest'; + $function = 'helper_FUNCTION_var'; + $args = false; + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($class, $function, $args)); + $data = array('testField' => 'test'); + $this->assertFalse($rule->validate($data)); + + $class = 'NetefxValidatorTest'; + $function = 'helper_FUNCTION_var'; + $args = true; + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($class, $function, $args)); + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + + $object = new NetefxValidatorTest(); + $function = 'helperFUNCTIONvar'; + $args = false; + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($object, $function, $args)); + $data = array('testField' => 'test'); + $this->assertFalse($rule->validate($data)); + + $object = new NetefxValidatorTest(); + $function = 'helperFUNCTIONvar'; + $args = true; + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($object, $function, $args)); + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + + $function = create_function('$data,$args', 'if ($data["testField"] == "test") return true; else return false;'); + $args = array(); + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($function, $args)); + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + + $function = function ($data, $args) { + if ($data['testField'] == 'test') { + return true; + } else { + return false; + } + }; + $args = array(); + $rule = new NetefxValidatorRuleFUNCTION("testField", null, null, array($function, $args)); + $data = array('testField' => 'test'); + $this->assertTrue($rule->validate($data)); + $data = array('testField' => ''); + $this->assertFalse($rule->validate($data)); + } + + public static function helper_FUNCTION_var($data = null, $args = null) + { + return $args; + } + public function helperFUNCTIONvar($data = null, $args = null) + { + return $args; + } +}