-
Notifications
You must be signed in to change notification settings - Fork 774
Introduce ContainsCount validator #1606
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # ContainsCount | ||
|
|
||
| - `ContainsCount(mixed $containsValue, int $count)` | ||
| - `ContainsCount(mixed $containsValue, int $count, bool $identical)` | ||
|
|
||
| Validates if the input contains a value a specific number of times. | ||
|
|
||
| For strings: | ||
|
|
||
| ```php | ||
| v::containsCount('ipsum', 2)->assert('ipsum lorem ipsum'); | ||
| // Validation passes successfully | ||
| ``` | ||
|
|
||
| For arrays: | ||
|
|
||
| ```php | ||
| v::containsCount('ipsum', 2)->assert(['ipsum', 'lorem', 'ipsum']); | ||
| // Validation passes successfully | ||
| ``` | ||
|
|
||
| A third parameter may be passed for identical comparison instead of equal comparison. | ||
|
|
||
| ```php | ||
| v::containsCount(1, 1, true)->assert([1, 2, 3]); | ||
| // Validation passes successfully | ||
|
|
||
| v::containsCount('1', 1, true)->assert([1, 2, 3]); | ||
| // → `[1, 2, 3]` must contain "1" only once | ||
| ``` | ||
|
|
||
| ## Templates | ||
|
|
||
| ### `ContainsCount::TEMPLATE_TIMES` | ||
|
|
||
| | Mode | Template | | ||
| | ---------- | ---------------------------------------------------------------- | | ||
| | `default` | {{subject}} must contain {{containsValue}} {{count}} time(s) | | ||
| | `inverted` | {{subject}} must not contain {{containsValue}} {{count}} time(s) | | ||
|
|
||
| ### `ContainsCount::TEMPLATE_ONCE` | ||
|
|
||
| | Mode | Template | | ||
| | ---------- | -------------------------------------------------------- | | ||
| | `default` | {{subject}} must contain {{containsValue}} only once | | ||
| | `inverted` | {{subject}} must not contain {{containsValue}} only once | | ||
|
|
||
| ## Template placeholders | ||
|
|
||
| | Placeholder | Description | | ||
| | --------------- | ---------------------------------------------------------------- | | ||
| | `containsValue` | The value to search for in the input. | | ||
| | `subject` | The validated input or the custom validator name (if specified). | | ||
| | `count` | Number of times that the needle might appear in the haystack. | | ||
|
|
||
| ## Categorization | ||
|
|
||
| - Arrays | ||
| - Strings | ||
|
|
||
| ## Changelog | ||
|
|
||
| | Version | Description | | ||
| | ------: | ----------- | | ||
| | 3.0.0 | Created | | ||
|
|
||
| --- | ||
|
|
||
| See also: | ||
|
|
||
| - [Contains](Contains.md) | ||
| - [ContainsAny](ContainsAny.md) | ||
| - [EndsWith](EndsWith.md) | ||
| - [Equals](Equals.md) | ||
| - [Equivalent](Equivalent.md) | ||
| - [Identical](Identical.md) | ||
| - [In](In.md) | ||
| - [Regex](Regex.md) | ||
| - [StartsWith](StartsWith.md) | ||
| - [Unique](Unique.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com> | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Validation\Validators; | ||
|
|
||
| use Attribute; | ||
| use Respect\Validation\Message\Template; | ||
| use Respect\Validation\Result; | ||
| use Respect\Validation\Validator; | ||
|
|
||
| use function array_reduce; | ||
| use function is_array; | ||
| use function is_scalar; | ||
| use function mb_strtolower; | ||
| use function mb_substr_count; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
| #[Template( | ||
| '{{subject}} must contain {{containsValue}} {{count}} time(s)', | ||
|
henriquemoody marked this conversation as resolved.
|
||
| '{{subject}} must not contain {{containsValue}} {{count}} time(s)', | ||
| self::TEMPLATE_TIMES, | ||
| )] | ||
| #[Template( | ||
| '{{subject}} must contain {{containsValue}} only once', | ||
| '{{subject}} must not contain {{containsValue}} only once', | ||
| self::TEMPLATE_ONCE, | ||
| )] | ||
| final readonly class ContainsCount implements Validator | ||
| { | ||
| public const string TEMPLATE_TIMES = '__times__'; | ||
| public const string TEMPLATE_ONCE = '__once__'; | ||
|
|
||
| public function __construct( | ||
| private mixed $containsValue, | ||
| private int $count, | ||
| private bool $identical = false, | ||
| ) { | ||
| } | ||
|
|
||
| public function evaluate(mixed $input): Result | ||
| { | ||
| $parameters = [ | ||
| 'containsValue' => $this->containsValue, | ||
| 'count' => $this->count, | ||
| ]; | ||
|
|
||
| $template = $this->count === 1 ? self::TEMPLATE_ONCE : self::TEMPLATE_TIMES; | ||
|
|
||
| if (is_array($input)) { | ||
| return Result::of( | ||
| $this->countArrayOccurrences($input) === $this->count, | ||
| $input, | ||
| $this, | ||
| $parameters, | ||
| $template, | ||
| ); | ||
| } | ||
|
|
||
| if (!is_scalar($input) || !is_scalar($this->containsValue)) { | ||
| return Result::failed($input, $this, $parameters, $template); | ||
| } | ||
|
|
||
| $needle = (string) $this->containsValue; | ||
|
|
||
| if ($needle === '') { | ||
| return Result::failed($input, $this, $parameters, $template); | ||
| } | ||
|
|
||
| return Result::of( | ||
| $this->countStringOccurrences((string) $input, $needle) === $this->count, | ||
| $input, | ||
| $this, | ||
| $parameters, | ||
| $template, | ||
| ); | ||
| } | ||
|
|
||
| /** @param array<mixed> $input */ | ||
| private function countArrayOccurrences(array $input): int | ||
| { | ||
| return array_reduce( | ||
| $input, | ||
| $this->identical ? function (int $carry, mixed $item): int { | ||
| return $carry + ($item === $this->containsValue ? 1 : 0); | ||
| } : function (int $carry, mixed $item): int { | ||
| return $carry + ($item == $this->containsValue ? 1 : 0); | ||
| }, | ||
| 0, | ||
| ); | ||
| } | ||
|
|
||
| private function countStringOccurrences(string $haystack, string $needle): int | ||
| { | ||
| if ($this->identical) { | ||
| return mb_substr_count($haystack, $needle); | ||
| } | ||
|
|
||
| return mb_substr_count(mb_strtolower($haystack), mb_strtolower($needle)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.