Skip to content

Commit

Permalink
Added comparison operator constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jan 4, 2018
1 parent 4fc476e commit 376e79f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 27 deletions.
78 changes: 67 additions & 11 deletions src/Validation/Validation.php
Expand Up @@ -38,6 +38,62 @@ class Validation
*/
const DEFAULT_LOCALE = 'en_US';

/**
* Same as operator.
*
* @var string
*/
const COMPARE_SAME_AS = '===';

/**
* Not same as comparison operator.
*
* @var string
*/
const COMPARE_NOT_SAME_AS = '!==';

/**
* Equal to comparison operator.
*
* @var string
*/
const COMPARE_EQUAL_TO = '==';

/**
* Not equal to comparison operator.
*
* @var string
*/
const COMPARE_NOT_EQUAL_TO = '!=';

/**
* Greater than comparison operator.
*
* @var string
*/
const COMPARE_GREATER_THAN = '>';

/**
* Greater than or equal to comparison operator.
*
* @var string
*/
const COMPARE_GREATER_THAN_OR_EQUAL_TO = '>=';

/**
* Less than comparison operator.
*
* @var string
*/
const COMPARE_LESS_THAN = '<';

/**
* Less than or equal to comparison operator.
*
* @var string
*/
const COMPARE_LESS_THAN_OR_EQUAL_TO = '<=';

/**
* Some complex patterns needed in multiple places
*
Expand Down Expand Up @@ -260,49 +316,49 @@ public static function comparison($check1, $operator, $check2)
$operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
switch ($operator) {
case 'isgreater':
case '>':
case static::COMPARE_GREATER_THAN:
if ($check1 > $check2) {
return true;
}
break;
case 'isless':
case '<':
case static::COMPARE_LESS_THAN:
if ($check1 < $check2) {
return true;
}
break;
case 'greaterorequal':
case '>=':
case static::COMPARE_GREATER_THAN_OR_EQUAL_TO:
if ($check1 >= $check2) {
return true;
}
break;
case 'lessorequal':
case '<=':
case static::COMPARE_LESS_THAN_OR_EQUAL_TO:
if ($check1 <= $check2) {
return true;
}
break;
case 'equalto':
case '==':
case static::COMPARE_EQUAL_TO:
if ($check1 == $check2) {
return true;
}
break;
case 'notequal':
case '!=':
case static::COMPARE_NOT_EQUAL_TO:
if ($check1 != $check2) {
return true;
}
break;
case 'sameas':
case '===':
case static::COMPARE_SAME_AS:
if ($check1 === $check2) {
return true;
}
break;
case 'notsameas':
case '!==':
case static::COMPARE_NOT_SAME_AS:
if ($check1 !== $check2) {
return true;
}
Expand All @@ -326,7 +382,7 @@ public static function comparison($check1, $operator, $check2)
*/
public static function compareWith($check, $field, $context)
{
return self::compareFields($check, $field, '===', $context);
return self::compareFields($check, $field, static::COMPARE_SAME_AS, $context);
}

/**
Expand Down Expand Up @@ -1226,10 +1282,10 @@ public static function uploadedFile($file, array $options = [])
if ($options['optional'] && $error === UPLOAD_ERR_NO_FILE) {
return true;
}
if (isset($options['minSize']) && !static::fileSize($file, '>=', $options['minSize'])) {
if (isset($options['minSize']) && !static::fileSize($file, static::COMPARE_GREATER_THAN_OR_EQUAL_TO, $options['minSize'])) {
return false;
}
if (isset($options['maxSize']) && !static::fileSize($file, '<=', $options['maxSize'])) {
if (isset($options['maxSize']) && !static::fileSize($file, static::COMPARE_LESS_THAN_OR_EQUAL_TO, $options['maxSize'])) {
return false;
}
if (isset($options['types']) && !static::mimeType($file, $options['types'])) {
Expand Down
32 changes: 16 additions & 16 deletions src/Validation/Validator.php
Expand Up @@ -861,7 +861,7 @@ public function greaterThan($field, $value, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThan', $extra + [
'rule' => ['comparison', '>', $value]
'rule' => ['comparison', Validation::COMPARE_GREATER_THAN, $value]
]);
}

Expand All @@ -881,7 +881,7 @@ public function greaterThanOrEqual($field, $value, $message = null, $when = null
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThanOrEqual', $extra + [
'rule' => ['comparison', '>=', $value]
'rule' => ['comparison', Validation::COMPARE_GREATER_THAN_OR_EQUAL_TO, $value]
]);
}

Expand All @@ -901,7 +901,7 @@ public function lessThan($field, $value, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThan', $extra + [
'rule' => ['comparison', '<', $value]
'rule' => ['comparison', Validation::COMPARE_LESS_THAN, $value]
]);
}

Expand All @@ -921,7 +921,7 @@ public function lessThanOrEqual($field, $value, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThanOrEqual', $extra + [
'rule' => ['comparison', '<=', $value]
'rule' => ['comparison', Validation::COMPARE_LESS_THAN_OR_EQUAL_TO, $value]
]);
}

Expand All @@ -941,7 +941,7 @@ public function equals($field, $value, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'equals', $extra + [
'rule' => ['comparison', '==', $value]
'rule' => ['comparison', Validation::COMPARE_EQUAL_TO, $value]
]);
}

Expand All @@ -961,7 +961,7 @@ public function notEquals($field, $value, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'notEquals', $extra + [
'rule' => ['comparison', '!=', $value]
'rule' => ['comparison', Validation::COMPARE_NOT_EQUAL_TO, $value]
]);
}

Expand All @@ -983,7 +983,7 @@ public function sameAs($field, $secondField, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'sameAs', $extra + [
'rule' => ['compareFields', $secondField, '===']
'rule' => ['compareFields', $secondField, Validation::COMPARE_SAME_AS]
]);
}

Expand All @@ -1004,7 +1004,7 @@ public function notSameAs($field, $secondField, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'notSameAs', $extra + [
'rule' => ['compareFields', $secondField, '!==']
'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_SAME_AS]
]);
}

Expand All @@ -1025,7 +1025,7 @@ public function equalToField($field, $secondField, $message = null, $when = null
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'equalToField', $extra + [
'rule' => ['compareFields', $secondField, '==']
'rule' => ['compareFields', $secondField, Validation::COMPARE_EQUAL_TO]
]);
}

Expand All @@ -1046,7 +1046,7 @@ public function notEqualToField($field, $secondField, $message = null, $when = n
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'notEqualToField', $extra + [
'rule' => ['compareFields', $secondField, '!=']
'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_EQUAL_TO]
]);
}

Expand All @@ -1067,7 +1067,7 @@ public function greaterThanField($field, $secondField, $message = null, $when =
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThanField', $extra + [
'rule' => ['compareFields', $secondField, '>']
'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER_THAN]
]);
}

Expand All @@ -1088,7 +1088,7 @@ public function greaterThanOrEqualToField($field, $secondField, $message = null,
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThanOrEqualToField', $extra + [
'rule' => ['compareFields', $secondField, '>=']
'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER_THAN_OR_EQUAL_TO]
]);
}

Expand All @@ -1109,7 +1109,7 @@ public function lessThanField($field, $secondField, $message = null, $when = nul
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThanField', $extra + [
'rule' => ['compareFields', $secondField, '<']
'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS_THAN]
]);
}

Expand All @@ -1130,7 +1130,7 @@ public function lessThanOrEqualToField($field, $secondField, $message = null, $w
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThanOrEqualToField', $extra + [
'rule' => ['compareFields', $secondField, '<=']
'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS_THAN_OR_EQUAL_TO]
]);
}

Expand Down Expand Up @@ -1853,7 +1853,7 @@ public function hasAtLeast($field, $count, $message = null, $when = null)
$value = $value['_ids'];
}

return Validation::numElements($value, '>=', $count);
return Validation::numElements($value, Validation::COMPARE_GREATER_THAN_OR_EQUAL_TO, $count);
}
]);
}
Expand All @@ -1880,7 +1880,7 @@ public function hasAtMost($field, $count, $message = null, $when = null)
$value = $value['_ids'];
}

return Validation::numElements($value, '<=', $count);
return Validation::numElements($value, Validation::COMPARE_LESS_THAN_OR_EQUAL_TO, $count);
}
]);
}
Expand Down

0 comments on commit 376e79f

Please sign in to comment.