Skip to content

Commit

Permalink
url scheme in method case-insensitive comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Triplkrypl committed May 6, 2022
1 parent 7c40bab commit 1741c4c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Rule/General/InRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @template TValue
* @extends Comparison<TValue, array<TValue>>
*/
final class InRule extends Comparison
class InRule extends Comparison
{
/**
* @param TValue $compared
Expand Down
54 changes: 54 additions & 0 deletions src/Rule/String/InRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace SimpleAsFuck\Validator\Rule\String;

use SimpleAsFuck\Validator\Factory\Exception;
use SimpleAsFuck\Validator\Model\RuleChain;
use SimpleAsFuck\Validator\Model\Validated;
use SimpleAsFuck\Validator\Rule\General\Compared;

/**
* @template Tstring of string
* @extends \SimpleAsFuck\Validator\Rule\General\InRule<Tstring>
*/
final class InRule extends \SimpleAsFuck\Validator\Rule\General\InRule
{
private bool $ignoreCharacterSize;

/**
* @param RuleChain<Tstring> $ruleChain
* @param Validated<mixed> $validated
* @param Compared<Tstring, array<Tstring>> $compared
* @param non-empty-array<Tstring> $comparedTo
*/
public function __construct(
?Exception $exceptionFactory,
RuleChain $ruleChain,
Validated $validated,
string $valueName,
Compared $compared,
$comparedTo,
bool $ignoreCharacterSize
) {
parent::__construct($exceptionFactory, $ruleChain, $validated, $valueName, $compared, $comparedTo);
$this->ignoreCharacterSize = $ignoreCharacterSize;
}

/**
* @param Tstring $compared
* @param array<Tstring> $comparedTo
*/
protected function compare($compared, $comparedTo): void
{
if ($this->ignoreCharacterSize) {
/** @var Tstring $compared */
$compared = strtolower($compared);
/** @var array<Tstring> $comparedTo */
$comparedTo = array_map(fn (string $value): string => strtolower($value), $comparedTo);
}

parent::compare($compared, $comparedTo);
}
}
6 changes: 3 additions & 3 deletions src/Rule/Url/Scheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function __construct(?Exception $exceptionFactory, RuleChain $ruleChain,
public function in(array $values): InRule
{
/** @var InRule<Tstring> $inRule */
$inRule = new InRule(
$inRule = new \SimpleAsFuck\Validator\Rule\String\InRule(
$this->exceptionFactory(),
/** @phpstan-ignore-next-line */
$this->ruleChain(),
$this->validated(),
$this->valueName(),
/** @phpstan-ignore-next-line */
new ComparedValue(),
$values
$values,
true
);
return $inRule;
}
Expand Down

0 comments on commit 1741c4c

Please sign in to comment.