Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Wixel/GUMP
Browse files Browse the repository at this point in the history
  • Loading branch information
filisko committed Feb 21, 2020
2 parents b04476a + e7a19b2 commit cbd8f1a
Show file tree
Hide file tree
Showing 14 changed files with 556 additions and 102 deletions.
122 changes: 47 additions & 75 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa
if (count(array_intersect($look_for, $rules)) > 0 || (isset($input[$field]))) {

if (isset($input[$field])) {
if (is_array($input[$field]) && in_array('required_file', $ruleset)) {
$input_array = $input[$field];
} else {
$input_array = array($input[$field]);
}
$input_array = array($input[$field]);
} else {
$input_array = array('');
}
Expand Down Expand Up @@ -1135,14 +1131,12 @@ protected function validate_max_len($field, $input, $param = null)
return;
}

if (function_exists('mb_strlen')) {
if (Helpers::functionExists('mb_strlen')) {
if (mb_strlen($input[$field]) <= (int) $param) {
return;
}
} else {
if (strlen($input[$field]) <= (int) $param) {
return;
}
} else if (strlen($input[$field]) <= (int) $param) {
return;
}

return array(
Expand Down Expand Up @@ -1170,14 +1164,12 @@ protected function validate_min_len($field, $input, $param = null)
return;
}

if (function_exists('mb_strlen')) {
if (Helpers::functionExists('mb_strlen')) {
if (mb_strlen($input[$field]) >= (int) $param) {
return;
}
} else {
if (strlen($input[$field]) >= (int) $param) {
return;
}
} else if (strlen($input[$field]) >= (int) $param) {
return;
}

return array(
Expand Down Expand Up @@ -1205,14 +1197,12 @@ protected function validate_exact_len($field, $input, $param = null)
return;
}

if (function_exists('mb_strlen')) {
if (Helpers::functionExists('mb_strlen')) {
if (mb_strlen($input[$field]) == (int) $param) {
return;
}
} else {
if (strlen($input[$field]) == (int) $param) {
return;
}
} else if (strlen($input[$field]) == (int) $param) {
return;
}

return array(
Expand Down Expand Up @@ -1646,20 +1636,19 @@ protected function validate_valid_cc($field, $input, $param = null)

$number = preg_replace('/\D/', '', $input[$field]);

if (function_exists('mb_strlen')) {
if (Helpers::functionExists('mb_strlen')) {
$number_length = mb_strlen($number);
} else {
$number_length = strlen($number);
}


/**
* Bail out if $number_length is 0.
* This can be the case if a user has entered only alphabets
*
* @since 1.5
*/
if( $number_length == 0 ) {
if ($number_length == 0 ) {
return array(
'field' => $field,
'value' => $input[$field],
Expand All @@ -1668,7 +1657,6 @@ protected function validate_valid_cc($field, $input, $param = null)
);
}


$parity = $number_length % 2;

$total = 0;
Expand Down Expand Up @@ -1972,33 +1960,33 @@ protected function validate_starts($field, $input, $param = null)
}
}

/**
* Checks if a file was uploaded.
*
* Usage: '<index>' => 'required_file'
*
* @param string $field
* @param array $input
*
* @return mixed
*/
protected function validate_required_file($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}

if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}
/**
* Checks if a file was uploaded.
*
* Usage: '<index>' => 'required_file'
*
* @param string $field
* @param array $input
*
* @return mixed
*/
protected function validate_required_file($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}

if (is_array($input[$field]) && $input[$field]['error'] === 0) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

/**
* Check the uploaded file for extension for now
Expand All @@ -2017,7 +2005,7 @@ protected function validate_extension($field, $input, $param = null)
return;
}

if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
if (is_array($input[$field]) && $input[$field]['error'] === 0) {
$param = trim(strtolower($param));
$allowed_extensions = explode(';', $param);

Expand All @@ -2027,14 +2015,14 @@ protected function validate_extension($field, $input, $param = null)
if ($extension && in_array(strtolower($extension), $allowed_extensions)) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}

/**
Expand Down Expand Up @@ -2095,22 +2083,6 @@ protected function validate_guidv4($field, $input, $param = null)
);
}

/**
* Trims whitespace only when the value is a scalar.
*
* @param mixed $value
*
* @return mixed
*/
private function trimScalar($value)
{
if (is_scalar($value)) {
$value = trim($value);
}

return $value;
}

/**
* Determine if the provided value is a valid phone number.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,15 @@ public function validate($rule, $value)
'test' => $rule
]);
}

public function filter($rule, $value)
{
$result = $this->gump->filter([
'test' => $value
], [
'test' => $rule
]);

return $result['test'];
}
}
34 changes: 34 additions & 0 deletions tests/Filters/BasicTagsFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Filters;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class BasicTagsFilterTest
*
* @package Tests
*/
class BasicTagsFilterTest extends BaseTestCase
{
private const FILTER = 'basic_tags';

/**
* @dataProvider successProvider
*/
public function testSuccess($input, $expected)
{
$result = $this->filter(self::FILTER, $input);

$this->assertEquals($result, $expected);
}

public function successProvider()
{
return [
['<script>alert(1);</script>hello', 'alert(1);hello']
];
}
}
33 changes: 33 additions & 0 deletions tests/Filters/LowercaseFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Filters;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class LowerFilterTest
*
* @package Tests
*/
class LowerFilterTest extends BaseTestCase
{
private const FILTER = 'lower_case';

/**
* @dataProvider successProvider
*/
public function testSuccess($input, $expected)
{
$result = $this->filter(self::FILTER, $input);
$this->assertEquals($result, $expected);
}

public function successProvider()
{
return [
['Hello', 'hello']
];
}
}
35 changes: 35 additions & 0 deletions tests/Filters/SlugFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\Filters;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class SlugFilterTest
*
* @package Tests
*/
class SlugFilterTest extends BaseTestCase
{
private const FILTER = 'slug';

/**
* @dataProvider successProvider
*/
public function testSuccess($input, $expected)
{
$result = $this->filter(self::FILTER, $input);
$this->assertEquals($result, $expected);
}

public function successProvider()
{
return [
['test spaceñ', 'test-spacen'],
['test space', 'test-space'],
['test', 'test'],
];
}
}
37 changes: 37 additions & 0 deletions tests/Filters/WholeNumberFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\Filters;

use GUMP;
use Exception;
use Tests\BaseTestCase;

/**
* Class WholeNumberFilterTest
*
* @package Tests
*/
class WholeNumberFilterTest extends BaseTestCase
{
private const FILTER = 'whole_number';

/**
* @dataProvider successProvider
*/
public function testSuccess($input, $expected)
{
$result = $this->filter(self::FILTER, $input);

$this->assertEquals($result, $expected);
}

public function successProvider()
{
return [
[1, 1],
['-1', '-1'],
[4.2, 4],
['042', '42'],
];
}
}
9 changes: 5 additions & 4 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ public function testCustomValidatorsReturnRightErrorStructure()
]], $result);
}

public function testRequired()
public function testRequiredAndRequiredFile()
{
$result = $this->gump->validate([
'test' => 'failing'
'field_without_validation_rules' => '123'
], [
'nonexistent' => 'required'
'some_field' => 'required',
'file_field' => 'required_file',
]);

$this->assertTrue(count($result) ===1);
$this->assertTrue(count($result) === 2);
}
}
Loading

0 comments on commit cbd8f1a

Please sign in to comment.