Skip to content

Commit

Permalink
v1.0.2 Unit test written.
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Jul 5, 2022
1 parent dffffa2 commit e47dfc3
Show file tree
Hide file tree
Showing 9 changed files with 787 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/.idea/
/.vscode/
/.vs/
/Example/
/vendor/
/composer.lock
/composer.lock
/.phpunit.result.cache
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### 1.0.2 [2022.07.05]

- Unit test written.

### 1.0.1 [2022.03.19]

- Fixed the issue where errors were not handled in optional data.
Expand Down
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,6 @@ $validation = new Validation($_GET);
$validation->rule('name', 'string');
$validation->rule('year', 'integer|range(1970...2099)');

if($validation->validation()){
// ... process
}else{
foreach ($validation->getError() as $err) {
echo $err . "<br />\n";
}
}
```
**Again Usage**
```php
require_once "vendor/autoload.php";
use \InitPHP\Validation\Validation;

$validation = new Validation($_GET);

// GET /?password=123a456&password_again=123a456

$validation->rule('password', 'string');
$validation->rule('password', 'again(password_again)');

if($validation->validation()){
// ... process
}else{
Expand Down Expand Up @@ -124,6 +104,8 @@ if($validation->validation()){
}
```

**_You can review the `tests/Validation/ValidationUnitTest.php` file to view sample usages._**

## Getting Help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.
Expand Down
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"InitPHP\\Validation\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\InitPHP\\Validation\\": "tests/Validation/"
}
},
"authors": [
{
"name": "Muhammet ŞAFAK",
Expand All @@ -20,5 +25,8 @@
"require": {
"php": ">=7.4",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "9.5"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests/Validation</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/LocaleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @version 1.0.2
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
42 changes: 27 additions & 15 deletions src/RulesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @version 1.0.2
* @link https://www.muhammetsafak.com.tr
*/

Expand Down Expand Up @@ -65,7 +65,7 @@ trait RulesTrait
],
];

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

protected function rule_integer($data): bool
Expand Down Expand Up @@ -167,13 +167,19 @@ protected function rule_mail($data): bool
return (bool)filter_var((string)$data, FILTER_VALIDATE_EMAIL);
}

protected function rule_mailhost($data, $domain): bool
protected function rule_mailhost($data, ...$domain): bool
{
if(filter_var($data, FILTER_VALIDATE_EMAIL)){
$parse = explode('@', $data);
$parseDNS = $parse[1] ?? null;
if(trim($parseDNS) === trim($domain)){
return true;
$parse = explode('@', $data, 2);
$mailHost = trim(($parse[1] ?? ''));
foreach ($domain as $host) {
$host = trim($host);
if(empty($host)){
continue;
}
if($mailHost === $host){
return true;
}
}
}
return false;
Expand All @@ -184,15 +190,21 @@ protected function rule_url($data): bool
return (bool)filter_var((string)$data, FILTER_VALIDATE_URL);
}

protected function rule_urlhost($data, $domain): bool
protected function rule_urlhost($data, ...$domains): bool
{
if(filter_var($data, FILTER_VALIDATE_URL)){
$host = parse_url($data, PHP_URL_HOST);
if($host === $domain){
return true;
}
if(mb_substr($host, -(mb_strlen($domain) + 1)) === '.' . $domain){
return true;
foreach ($domains as $domain) {
$domain = trim($domain);
if(empty($domain)){
continue;
}
if($domain === $host){
return true;
}
if(mb_substr($host, (0 - (mb_strlen($domain) + 1))) === '.' . $domain){
return true;
}
}
}
return false;
Expand Down Expand Up @@ -360,7 +372,7 @@ protected function rule_endwith($data, $endWith): bool

protected function rule_in($data, $search): bool
{
if(is_string($data) && is_numeric($data)){
if(is_string($data) || is_numeric($data)){
return mb_stripos((string)$data, (string)$search) !== FALSE;
}
if(is_array($data)){
Expand Down
10 changes: 9 additions & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2022 InitPHP
* @license http://initphp.github.io/license.txt MIT
* @version 1.0
* @version 1.0.2
* @link https://www.muhammetsafak.com.tr
*/

Expand Down Expand Up @@ -81,6 +81,13 @@ public function clear(): self
return $this;
}

public function pattern(string $name, string $pattern = '[\w]+'): self
{
$name = strtolower($name);
$this->patterns[$name] = $pattern;
return $this;
}

/**
* @param string|string[] $key
* @param string|callable|string[]|callable[] $rule
Expand Down Expand Up @@ -140,6 +147,7 @@ public function validation(): bool
$this->callableProcessValidation($rule);
}
}
$this->rule = [];
return empty($this->error);
}

Expand Down
Loading

0 comments on commit e47dfc3

Please sign in to comment.