Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse negation character #746

Merged
merged 14 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## 2.1.0 under development

- Enh #740: Use `Yiisoft\NetworkUtilities\IpRanges` in `Ip` rule: add `getIpRanges()` method and deprecate
`getRanges()`, `getNetworks()`, `isAllowed()` methods (@vjik)
`getRanges()`, `getNetworks()`, `isAllowed()` methods (@vjik)
- Enh #746: Use `NEGATION_CHARACTER` constant from `network-utilities` package in `IpHandler` instead of declaring its own
constant (@arogachev)

## 2.0.0 August 02, 2024

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"psr/http-message": "^1.0|^2.0",
"yiisoft/arrays": "^2.1|^3.0",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/network-utilities": "^1.1",
"yiisoft/network-utilities": "^1.2",
"yiisoft/strings": "^2.1",
"yiisoft/translator": "^2.1|^3.0"
},
Expand Down
11 changes: 2 additions & 9 deletions src/Rule/IpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Yiisoft\NetworkUtilities\IpHelper;
use Yiisoft\NetworkUtilities\IpRanges;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Result;
use Yiisoft\Validator\RuleHandlerInterface;
Expand All @@ -21,14 +22,6 @@
*/
final class IpHandler implements RuleHandlerInterface
{
/**
* Negation character.
*
* Used to negate {@see $ranges} or {@see $network} or to negate value validated when {@see $allowNegation}
* is used.
*/
private const NEGATION_CHARACTER = '!';

public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result
{
if (!$rule instanceof Ip) {
Expand Down Expand Up @@ -82,7 +75,7 @@ public function validate(mixed $value, RuleInterface $rule, ValidationContext $c
private static function getIpParsePattern(): string
{
return '/^(?<not>' .
self::NEGATION_CHARACTER .
IpRanges::NEGATION_CHARACTER .
')?(?<ipCidr>(?<ip>(?:' . IpHelper::IPV4_PATTERN . ')|(?:' . IpHelper::IPV6_PATTERN . '))(?:\/(?<cidr>-?\d+))?)$/';
}

Expand Down
Loading