Skip to content

Commit

Permalink
- updated phpunit xml
Browse files Browse the repository at this point in the history
- updated readme
  • Loading branch information
lezhnev74 committed Feb 10, 2021
1 parent 31fda37 commit 63dee4e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ try {

### Optional String Validation
```php
$pattern = ":string :regexp('^[ab]+$')";
$pattern = ":string :regexp('#^[ab]+$#')";
$builder = \PASVL\Validation\ValidatorBuilder::forString($pattern);
$validator = $builder->build();
$validator->validate("abab"); // the string is valid
Expand Down Expand Up @@ -121,7 +121,7 @@ It looks like this:
```
- as rule with subrules
```php
$pattern = ["name" => ":string :regexp('\d*')"]; // the value must be a string which contains only digits
$pattern = ["name" => ":string :regexp('#\d*#')"]; // the value must be a string which contains only digits
```
- as rule with quantifiers
```php
Expand Down Expand Up @@ -153,7 +153,7 @@ To add new custom rules, follow these steps:
## Built-in Rules
This package comes with a few built-in rules and their corresponding sub-rules (see in folder `src/Validation/Rules/Library`):
- `:string` - the value must be string
- `:regexp(<string>)` - provide a regular expression without trailing delimiters
- `:regexp(<string>)` - provide a regular expression(the same as for `preg_match()`)
- `:url`
- `:email`
- `:uuid`
Expand Down Expand Up @@ -200,7 +200,8 @@ This package comes with a few built-in rules and their corresponding sub-rules (
## 🏆 Contributors
- **[Greg Corrigan](https://github.com/corrigang)**. Greg spotted a problem with nullable values reported as invalid.
- **Henry Combrinck**. Henry tested the library extensively on real data and found tricky bugs and edge cases. Awesome contribution to make the package valuable to the community.
- **[@Averor](https://github.com/Averor)**. Found a bug in parenthesis parsing.
- **[@Averor](https://github.com/Averor)**. Found a bug in parentheses parsing.
- **[Julien Gidel](https://github.com/JuGid)**. Improved `regexp` sub-rule.

## License
This project is licensed under the terms of the MIT license.
Expand Down
17 changes: 10 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
stopOnFailure="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="All tests">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
</php>
</phpunit>
18 changes: 11 additions & 7 deletions src/Validation/Rules/Library/RuleString.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,22 @@ public function min(int $len): void

public function regexp(string $expr): void
{
if(@preg_match($expr, '') === FALSE) {
throw new RuleFailed(sprintf("regexp is not a valid regular expression %s", $expr));
}
error_clear_last();
if (@preg_match($expr, '') === false) {
$lastError = error_get_last() ? error_get_last()['message'] : '';
throw new RuleFailed(sprintf("regexp is not a valid regular expression %s: %s", $expr, $lastError));
}

if(!preg_match($expr, $this->value)) {
throw new RuleFailed(sprintf("string does not match regular expression %s", $expr));
}
if (!preg_match($expr, $this->value)) {
throw new RuleFailed(sprintf("string does not match regular expression %s", $expr));
}
}

public function url(): void
{
if (filter_var($this->value, FILTER_VALIDATE_URL) === false) throw new RuleFailed("string must be url");
if (filter_var($this->value, FILTER_VALIDATE_URL) === false) {
throw new RuleFailed("string must be url");
}
}

public function between(int $min, int $max): void
Expand Down

0 comments on commit 63dee4e

Please sign in to comment.