Skip to content

Commit 8d738a4

Browse files
committed
Support validating arrays
1 parent 4a6e970 commit 8d738a4

File tree

5 files changed

+88
-37
lines changed

5 files changed

+88
-37
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
name: CI
22

3-
on: [push]
3+
on: [ push ]
44

55
jobs:
6-
build-php-7-4:
6+
tests:
7+
name: Tests PHP ${{ matrix.php }}
78
runs-on: ubuntu-latest
9+
continue-on-error: ${{ matrix.experimental }}
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
php: [ 7.3, 7.4, 8.0 ]
14+
experimental: [ false ]
815

916
steps:
10-
- uses: actions/checkout@v2
11-
- uses: php-actions/composer@master
12-
- name: PHPUnit Tests
13-
uses: php-actions/phpunit@dynamic-docker
14-
with:
15-
bootstrap: vendor/autoload.php
16-
configuration: tests/phpunit.xml
17-
args: --coverage-text
18-
php_version: 7.4
19-
version: 9.5.0
20-
build-php-8-0:
21-
runs-on: ubuntu-latest
17+
- name: Checkout
18+
uses: actions/checkout@v2
2219

23-
steps:
24-
- uses: actions/checkout@v2
25-
- uses: php-actions/composer@master
26-
- name: PHPUnit Tests
27-
uses: php-actions/phpunit@dynamic-docker
20+
- name: Set up PHP ${{ matrix.php }}
21+
uses: shivammathur/setup-php@v2
2822
with:
29-
bootstrap: vendor/autoload.php
30-
configuration: tests/phpunit.xml
31-
args: --coverage-text
32-
php_version: 8.0
33-
version: 9.5.0
23+
php-version: ${{ matrix.php }}
24+
25+
- name: Install Composer dependencies
26+
run: |
27+
composer install --no-progress --prefer-dist --optimize-autoloader
28+
29+
- name: Run Tests
30+
run: php vendor/bin/phpunit --configuration tests\phpunit.xml

.idea/php.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Validator.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,55 @@ public function __construct(array $config = [])
3232
$this->useSession = $config['useSession'] ?? false;
3333
}
3434

35+
/**
36+
* @param Request|array $params
37+
* @param array $rules
38+
* @param mixed $default
39+
* @return $this
40+
* @throws \Exception
41+
*/
42+
public function validate($params, array $rules, $default = null): Validator
43+
{
44+
if ($params instanceof Request) {
45+
return $this->validateRequest($params, $rules, $default);
46+
} elseif (is_array($params)) {
47+
return $this->validateArray($params, $rules, $default);
48+
}
49+
50+
throw new \Exception('Unknown type given for $params.');
51+
}
52+
3553
/**
3654
* @param Request $request
3755
* @param array $rules
38-
* @param null $default
56+
* @param mixed $default
3957
* @return $this
4058
*/
41-
public function validate(Request $request, array $rules, $default = null): Validator
59+
public function validateRequest(Request $request, array $rules, $default = null)
4260
{
43-
//$this->values = [];
44-
//$this->errors = [];
4561
$params = $request->getParsedBody();
62+
return $this->runValidation($params, $rules, $default);
63+
}
64+
65+
/**
66+
* @param array $params
67+
* @param array $rules
68+
* @param mixed $default
69+
* @return $this
70+
*/
71+
public function validateArray(array $params, array $rules, $default = null)
72+
{
73+
return $this->runValidation($params, $rules, $default);
74+
}
75+
76+
/**
77+
* @param array $params
78+
* @param array $rules
79+
* @param mixed $default
80+
* @return Validator
81+
*/
82+
protected function runValidation(array $params, array $rules, $default = null)
83+
{
4684
foreach ($rules as $field => $rule) {
4785
try {
4886
$param = isset($params[$field]) ? $params[$field] : $default;
@@ -57,7 +95,6 @@ public function validate(Request $request, array $rules, $default = null): Valid
5795
$_SESSION['TS_PHPValidator_Errors'] = $this->errors;
5896
$_SESSION['TS_PHPValidator_Values'] = $this->values;
5997
}
60-
6198
return $this;
6299
}
63100

tests/PHPValidatorTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public function testNonValidRequest()
4141
$this->assertTrue($v->failed());
4242
}
4343

44+
public function testValidArray()
45+
{
46+
$array = ["email" => "[email protected]", "password" => "Abcd1234_"];
47+
$v = (new Validator())->validate($array, [
48+
'email' => v::noWhitespace()->notEmpty()->email(),
49+
'password' => v::noWhitespace()->notEmpty()->length(8)->alnum('_')
50+
]);
51+
52+
$this->assertTrue($v->isValid());
53+
}
54+
55+
public function testNonValidArray()
56+
{
57+
$array = ["email" => "example", "password" => "1234"];
58+
$v = (new Validator())->validate($array, [
59+
'email' => v::noWhitespace()->notEmpty()->email(),
60+
'password' => v::noWhitespace()->notEmpty()->length(8)->alnum('_')
61+
]);
62+
63+
$this->assertTrue($v->failed());
64+
}
65+
4466
public function testMiddleware()
4567
{
4668
$config = [
@@ -113,7 +135,8 @@ protected function createServerRequest(
113135
string $uri,
114136
string $method = 'GET',
115137
array $data = []
116-
): Request {
138+
): Request
139+
{
117140
$headers = [
118141
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
119142
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',

tests/phpunit.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,4 @@
55
<directory suffix="Test.php">.</directory>
66
</testsuite>
77
</testsuites>
8-
9-
<filter>
10-
<whitelist>
11-
<directory suffix=".php">../src</directory>
12-
</whitelist>
13-
</filter>
148
</phpunit>

0 commit comments

Comments
 (0)