Skip to content

Commit e47dfc3

Browse files
committed
v1.0.2 Unit test written.
1 parent dffffa2 commit e47dfc3

File tree

9 files changed

+787
-38
lines changed

9 files changed

+787
-38
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/.idea/
22
/.vscode/
33
/.vs/
4+
/Example/
45
/vendor/
5-
/composer.lock
6+
/composer.lock
7+
/.phpunit.result.cache

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
### 1.0.2 [2022.07.05]
4+
5+
- Unit test written.
6+
37
### 1.0.1 [2022.03.19]
48

59
- Fixed the issue where errors were not handled in optional data.

README.md

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,6 @@ $validation = new Validation($_GET);
6969
$validation->rule('name', 'string');
7070
$validation->rule('year', 'integer|range(1970...2099)');
7171

72-
if($validation->validation()){
73-
// ... process
74-
}else{
75-
foreach ($validation->getError() as $err) {
76-
echo $err . "<br />\n";
77-
}
78-
}
79-
```
80-
**Again Usage**
81-
```php
82-
require_once "vendor/autoload.php";
83-
use \InitPHP\Validation\Validation;
84-
85-
$validation = new Validation($_GET);
86-
87-
// GET /?password=123a456&password_again=123a456
88-
89-
$validation->rule('password', 'string');
90-
$validation->rule('password', 'again(password_again)');
91-
9272
if($validation->validation()){
9373
// ... process
9474
}else{
@@ -124,6 +104,8 @@ if($validation->validation()){
124104
}
125105
```
126106

107+
**_You can review the `tests/Validation/ValidationUnitTest.php` file to view sample usages._**
108+
127109
## Getting Help
128110

129111
If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
"InitPHP\\Validation\\": "src/"
99
}
1010
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Tests\\InitPHP\\Validation\\": "tests/Validation/"
14+
}
15+
},
1116
"authors": [
1217
{
1318
"name": "Muhammet ŞAFAK",
@@ -20,5 +25,8 @@
2025
"require": {
2126
"php": ">=7.4",
2227
"ext-mbstring": "*"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "9.5"
2331
}
2432
}

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="Unit Tests">
5+
<directory>tests/Validation</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>

src/LocaleTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.io/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.2
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

src/RulesTrait.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.io/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.2
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -65,7 +65,7 @@ trait RulesTrait
6565
],
6666
];
6767

68-
private array $patterns = [
68+
protected array $patterns = [
6969
'uri' => '[A-Za-z0-9-\/_?&=]+',
7070
'slug' => '[-a-z0-9_-]',
7171
'url' => '[A-Za-z0-9-:.\/_?&=#]+',
@@ -81,7 +81,7 @@ trait RulesTrait
8181
'address' => '[\p{L}0-9\s.,()°-]+',
8282
'date_dmy' => '[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}',
8383
'date_ymd' => '[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}',
84-
'email' => '[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+[.]+[a-z-A-Z]'
84+
'email' => '[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+[.]?[a-z-A-Z]?'
8585
];
8686

8787
protected function rule_integer($data): bool
@@ -167,13 +167,19 @@ protected function rule_mail($data): bool
167167
return (bool)filter_var((string)$data, FILTER_VALIDATE_EMAIL);
168168
}
169169

170-
protected function rule_mailhost($data, $domain): bool
170+
protected function rule_mailhost($data, ...$domain): bool
171171
{
172172
if(filter_var($data, FILTER_VALIDATE_EMAIL)){
173-
$parse = explode('@', $data);
174-
$parseDNS = $parse[1] ?? null;
175-
if(trim($parseDNS) === trim($domain)){
176-
return true;
173+
$parse = explode('@', $data, 2);
174+
$mailHost = trim(($parse[1] ?? ''));
175+
foreach ($domain as $host) {
176+
$host = trim($host);
177+
if(empty($host)){
178+
continue;
179+
}
180+
if($mailHost === $host){
181+
return true;
182+
}
177183
}
178184
}
179185
return false;
@@ -184,15 +190,21 @@ protected function rule_url($data): bool
184190
return (bool)filter_var((string)$data, FILTER_VALIDATE_URL);
185191
}
186192

187-
protected function rule_urlhost($data, $domain): bool
193+
protected function rule_urlhost($data, ...$domains): bool
188194
{
189195
if(filter_var($data, FILTER_VALIDATE_URL)){
190196
$host = parse_url($data, PHP_URL_HOST);
191-
if($host === $domain){
192-
return true;
193-
}
194-
if(mb_substr($host, -(mb_strlen($domain) + 1)) === '.' . $domain){
195-
return true;
197+
foreach ($domains as $domain) {
198+
$domain = trim($domain);
199+
if(empty($domain)){
200+
continue;
201+
}
202+
if($domain === $host){
203+
return true;
204+
}
205+
if(mb_substr($host, (0 - (mb_strlen($domain) + 1))) === '.' . $domain){
206+
return true;
207+
}
196208
}
197209
}
198210
return false;
@@ -360,7 +372,7 @@ protected function rule_endwith($data, $endWith): bool
360372

361373
protected function rule_in($data, $search): bool
362374
{
363-
if(is_string($data) && is_numeric($data)){
375+
if(is_string($data) || is_numeric($data)){
364376
return mb_stripos((string)$data, (string)$search) !== FALSE;
365377
}
366378
if(is_array($data)){

src/Validation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 InitPHP
99
* @license http://initphp.github.io/license.txt MIT
10-
* @version 1.0
10+
* @version 1.0.2
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -81,6 +81,13 @@ public function clear(): self
8181
return $this;
8282
}
8383

84+
public function pattern(string $name, string $pattern = '[\w]+'): self
85+
{
86+
$name = strtolower($name);
87+
$this->patterns[$name] = $pattern;
88+
return $this;
89+
}
90+
8491
/**
8592
* @param string|string[] $key
8693
* @param string|callable|string[]|callable[] $rule
@@ -140,6 +147,7 @@ public function validation(): bool
140147
$this->callableProcessValidation($rule);
141148
}
142149
}
150+
$this->rule = [];
143151
return empty($this->error);
144152
}
145153

0 commit comments

Comments
 (0)