Skip to content

Commit

Permalink
Removing old Validation class syntax. Better pass the parameters than…
Browse files Browse the repository at this point in the history
… arrays

Using is_scalar instead in some Validation methods as it makes it more flexible
  • Loading branch information
lorenzo authored and markstory committed Nov 5, 2015
1 parent 0c01172 commit c4c2701
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 111 deletions.
70 changes: 13 additions & 57 deletions src/Validation/Validation.php
Expand Up @@ -82,10 +82,6 @@ public static function notEmpty($check)
*/
public static function notBlank($check)
{
if (is_array($check)) {
extract(static::_defaults($check));
}

if (empty($check) && $check !== '0') {
return false;
}
Expand All @@ -105,10 +101,6 @@ public static function notBlank($check)
*/
public static function alphaNumeric($check)
{
if (is_array($check)) {
extract(static::_defaults($check));
}

if (empty($check) && $check !== '0') {
return false;
}
Expand All @@ -127,6 +119,9 @@ public static function alphaNumeric($check)
*/
public static function lengthBetween($check, $min, $max)
{
if (!is_string($check)) {
return false;
}
$length = mb_strlen($check);
return ($length >= $min && $length <= $max);
}
Expand All @@ -145,9 +140,6 @@ public static function lengthBetween($check, $min, $max)
public static function blank($check)
{
trigger_error('Validation::blank() is deprecated.', E_USER_DEPRECATED);
if (is_array($check)) {
extract(static::_defaults($check));
}
return !static::_check($check, '/[^\\s]/');
}

Expand All @@ -166,8 +158,8 @@ public static function blank($check)
*/
public static function cc($check, $type = 'fast', $deep = false, $regex = null)
{
if (is_array($check)) {
extract(static::_defaults($check));
if (!is_scalar($check)) {
return false;
}

$check = str_replace(['-', ' '], '', $check);
Expand All @@ -177,7 +169,7 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null)

if ($regex !== null) {
if (static::_check($check, $regex)) {
return static::luhn($check, $deep);
return !$deep || static::luhn($check, $deep);
}
}
$cards = [
Expand Down Expand Up @@ -228,19 +220,16 @@ public static function cc($check, $type = 'fast', $deep = false, $regex = null)
/**
* Used to compare 2 numeric values.
*
* @param string|array $check1 if string is passed for, a string must also be passed for $check2
* @param string $check1 if string is passed for, a string must also be passed for $check2
* used as an array it must be passed as ['check1' => value, 'operator' => 'value', 'check2' => value]
* @param string $operator Can be either a word or operand
* is greater >, is less <, greater or equal >=
* less or equal <=, is less <, equal to ==, not equal !=
* @param int $check2 only needed if $check1 is a string
* @return bool Success
*/
public static function comparison($check1, $operator = null, $check2 = null)
public static function comparison($check1, $operator, $check2)
{
if (is_array($check1)) {
extract($check1, EXTR_OVERWRITE);
}
if ((float)$check1 != $check1) {
return false;
}
Expand Down Expand Up @@ -318,7 +307,7 @@ public static function compareWith($check, $field, $context)
*/
public static function containsNonAlphaNumeric($check, $count = 1)
{
if (!is_string($check)) {
if (!is_scalar($check)) {
return false;
}

Expand All @@ -336,9 +325,6 @@ public static function containsNonAlphaNumeric($check, $count = 1)
*/
public static function custom($check, $regex = null)
{
if (is_array($check)) {
extract(static::_defaults($check));
}
if ($regex === null) {
static::$errors[] = 'You must define a regular expression for Validation::custom()';
return false;
Expand Down Expand Up @@ -548,8 +534,8 @@ public static function decimal($check, $places = null, $regex = null)
*/
public static function email($check, $deep = false, $regex = null)
{
if (is_array($check)) {
extract(static::_defaults($check));
if (!is_string($check)) {
return false;
}

if ($regex === null) {
Expand Down Expand Up @@ -854,31 +840,7 @@ public static function uuid($check)
*/
protected static function _check($check, $regex)
{
return is_string($regex) && preg_match($regex, $check);
}

/**
* Get the values to use when value sent to validation method is
* an array.
*
* @param array $params Parameters sent to validation method
* @return array
*/
protected static function _defaults($params)
{
static::_reset();
$defaults = [
'check' => null,
'regex' => null,
'country' => null,
'deep' => false,
'type' => null
];
$params += $defaults;
if ($params['country'] !== null) {
$params['country'] = mb_strtolower($params['country']);
}
return $params;
return is_string($regex) && is_scalar($check) && preg_match($regex, $check);
}

/**
Expand All @@ -891,13 +853,7 @@ protected static function _defaults($params)
*/
public static function luhn($check, $deep = false)
{
if (is_array($check)) {
extract(static::_defaults($check));
}
if ($deep !== true) {
return true;
}
if ((int)$check === 0) {
if (!is_scalar($check) || (int)$check === 0) {
return false;
}
$sum = 0;
Expand Down
71 changes: 17 additions & 54 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -122,17 +122,18 @@ public function testAlphaNumeric()
*/
public function testAlphaNumericPassedAsArray()
{
$this->assertTrue(Validation::alphaNumeric(['check' => 'frferrf']));
$this->assertTrue(Validation::alphaNumeric(['check' => '12234']));
$this->assertTrue(Validation::alphaNumeric(['check' => '1w2e2r3t4y']));
$this->assertTrue(Validation::alphaNumeric(['check' => '0']));
$this->assertFalse(Validation::alphaNumeric(['check' => '12 234']));
$this->assertFalse(Validation::alphaNumeric(['check' => 'dfd 234']));
$this->assertFalse(Validation::alphaNumeric(['check' => "\n"]));
$this->assertFalse(Validation::alphaNumeric(['check' => "\t"]));
$this->assertFalse(Validation::alphaNumeric(['check' => "\r"]));
$this->assertFalse(Validation::alphaNumeric(['check' => ' ']));
$this->assertFalse(Validation::alphaNumeric(['check' => '']));
$this->assertTrue(Validation::alphaNumeric('frferrf'));
$this->assertTrue(Validation::alphaNumeric('12234'));
$this->assertTrue(Validation::alphaNumeric('1w2e2r3t4y'));
$this->assertTrue(Validation::alphaNumeric('0'));
$this->assertFalse(Validation::alphaNumeric('12 234'));
$this->assertFalse(Validation::alphaNumeric('dfd 234'));
$this->assertFalse(Validation::alphaNumeric("\n"));
$this->assertFalse(Validation::alphaNumeric("\t"));
$this->assertFalse(Validation::alphaNumeric("\r"));
$this->assertFalse(Validation::alphaNumeric(' '));
$this->assertFalse(Validation::alphaNumeric(''));
$this->assertFalse(Validation::alphaNumeric(['foo']));
}

/**
Expand Down Expand Up @@ -657,9 +658,8 @@ public function testLuhn()
*/
public function testCustomRegexForCc()
{
$this->assertTrue(Validation::cc('12332105933743585', null, null, '/123321\\d{11}/'));
$this->assertFalse(Validation::cc('1233210593374358', null, null, '/123321\\d{11}/'));
$this->assertFalse(Validation::cc('12312305933743585', null, null, '/123321\\d{11}/'));
$this->assertTrue(Validation::cc('370482756063980', null, false, '/123321\\d{11}/'));
$this->assertFalse(Validation::cc('1233210593374358', null, false, '/123321\\d{11}/'));
}

/**
Expand Down Expand Up @@ -864,43 +864,6 @@ public function testComparisonTypeChecks()
$this->assertFalse(Validation::comparison('0x02', '>=', 1.5), 'hex string data fails');
}

/**
* testComparisonAsArray method
*
* @return void
*/
public function testComparisonAsArray()
{
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'is greater', 'check2' => 6]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>', 'check2' => 6]));
$this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => 'is less', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => '<', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'greater or equal', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>=', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'greater or equal', 'check2' => 6]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '>=', 'check2' => 6]));
$this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => 'less or equal', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 6, 'operator' => '<=', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'equal to', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '==', 'check2' => 7]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => 'not equal', 'check2' => 6]));
$this->assertTrue(Validation::comparison(['check1' => 7, 'operator' => '!=', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'is greater', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'is less', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '<', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'greater or equal', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>=', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => 'greater or equal', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 6, 'operator' => '>=', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'less or equal', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '<=', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'equal to', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '==', 'check2' => 6]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => 'not equal', 'check2' => 7]));
$this->assertFalse(Validation::comparison(['check1' => 7, 'operator' => '!=', 'check2' => 7]));
}

/**
* testCustom method
*
Expand All @@ -921,9 +884,9 @@ public function testCustom()
*/
public function testCustomAsArray()
{
$this->assertTrue(Validation::custom(['check' => '12345', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
$this->assertFalse(Validation::custom(['check' => 'Text', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
$this->assertFalse(Validation::custom(['check' => '123.45', 'regex' => '/(?<!\\S)\\d++(?!\\S)/']));
$this->assertTrue(Validation::custom('12345', '/(?<!\\S)\\d++(?!\\S)/'));
$this->assertFalse(Validation::custom('Text', '/(?<!\\S)\\d++(?!\\S)/'));
$this->assertFalse(Validation::custom('123.45', '/(?<!\\S)\\d++(?!\\S)/'));
}

/**
Expand Down

0 comments on commit c4c2701

Please sign in to comment.