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

Move out setting of validation rules enricher from theme container #351

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ jobs:
['ubuntu-latest']
php: >-
['8.2']
bin-name: infection
secrets:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"yiisoft/widget": "^2.2"
},
"require-dev": {
"infection/infection": "^0.26.19",
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0.0",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.21"
},
Expand Down
5 changes: 4 additions & 1 deletion docs/guide/en/validation-rules-enrichment.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ final class MyTextField extends InputField
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
arogachev marked this conversation as resolved.
Show resolved Hide resolved
?? [];
}
}

Expand Down
5 changes: 2 additions & 3 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

## Mutation testing

The package tests are checked with [Infection](https://infection.github.io/) mutation framework with
[Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:
The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

```shell
./vendor/bin/roave-infection-static-analysis-plugin
./vendor/bin/infection
```

## Static analysis
Expand Down
6 changes: 4 additions & 2 deletions src/Field/Base/DateTimeInputField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -143,7 +142,10 @@ final public function disabled(bool $disabled = true): static
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
arogachev marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface EnrichFromValidationRulesInterface
* Enable/disable enrichment based on validation rules.
*/
public function enrichFromValidationRules(bool $enrich): self;

public function validationRulesEnricher(?ValidationRulesEnricherInterface $enricher): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

namespace Yiisoft\Form\Field\Base\EnrichFromValidationRules;

use Yiisoft\Form\ValidationRulesEnricherInterface;

trait EnrichFromValidationRulesTrait
{
private bool $enrichFromValidationRules = false;
private ?ValidationRulesEnricherInterface $validationRulesEnricher = null;

public function enrichFromValidationRules(bool $enrich = true): self
{
$new = clone $this;
$new->enrichFromValidationRules = $enrich;
return $new;
}

public function validationRulesEnricher(?ValidationRulesEnricherInterface $enricher): self
{
$new = clone $this;
$new->validationRulesEnricher = $enricher;
return $new;
}
}
6 changes: 4 additions & 2 deletions src/Field/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -201,7 +200,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

/**
Expand Down Expand Up @@ -154,7 +153,10 @@ public function addUncheckInputAttributes(array $attributes): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

/**
Expand Down Expand Up @@ -162,7 +161,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -192,7 +191,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -190,7 +189,10 @@ public function addOutputAttributes(array $attributes): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Yiisoft\Form\Field\Base\InputField;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Tag\Optgroup;
use Yiisoft\Html\Tag\Option;
use Yiisoft\Html\Tag\Select as SelectTag;
Expand Down Expand Up @@ -241,7 +240,10 @@
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand All @@ -262,7 +264,7 @@
&& !is_string($value)
&& !is_numeric($value)
&& $value !== null
&& (!is_object($value) || !method_exists($value, '__toString'))

Check warning on line 267 in src/Field/Select.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalNot": --- Original +++ New @@ @@ throw new InvalidArgumentException('Select field with multiple option requires iterable or null value.'); } } else { - if (!is_bool($value) && !is_string($value) && !is_numeric($value) && $value !== null && (!is_object($value) || !method_exists($value, '__toString'))) { + if (!is_bool($value) && !is_string($value) && !is_numeric($value) && $value !== null && (is_object($value) || !method_exists($value, '__toString'))) { throw new InvalidArgumentException('Non-multiple Select field requires a string, numeric, bool, Stringable or null value.'); } $value = $value === null ? [] : [$value];
) {
throw new InvalidArgumentException(
'Non-multiple Select field requires a string, numeric, bool, Stringable or null value.'
Expand Down
6 changes: 4 additions & 2 deletions src/Field/Telephone.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -187,7 +186,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -203,7 +202,10 @@ public function tabIndex(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -218,7 +217,10 @@ public function wrap(?string $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Field/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
use Yiisoft\Form\Theme\ThemeContainer;
use Yiisoft\Html\Html;

use function is_string;
Expand Down Expand Up @@ -187,7 +186,10 @@ public function size(?int $value): self
protected function beforeRender(): void
{
if ($this->enrichFromValidationRules) {
$this->enrichment = ThemeContainer::getValidationRulesEnrichment($this, $this->getInputData());
$this->enrichment = $this
->validationRulesEnricher
?->process($this, $this->getInputData()->getValidationRules())
?? [];
}
}

Expand Down
19 changes: 2 additions & 17 deletions src/Theme/ThemeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace Yiisoft\Form\Theme;

use Yiisoft\Form\Field\Base\BaseField;
use Yiisoft\Form\Field\Base\InputData\InputDataInterface;
use Yiisoft\Form\ValidationRulesEnricherInterface;

use function array_key_exists;

final class ThemeContainer
Expand All @@ -24,8 +20,6 @@ final class ThemeContainer
*/
private static array $themes = [];

private static ?ValidationRulesEnricherInterface $validationRulesEnricher = null;

/**
* @param array<string,array> $configs Array of configurations with {@see Theme::__construct()}
* arguments indexed by name. For example:
Expand Down Expand Up @@ -57,15 +51,11 @@ final class ThemeContainer
* ```
* @param string|null $defaultConfig Configuration name that will be used for create fields by default.
*/
public static function initialize(
array $configs = [],
?string $defaultConfig = null,
?ValidationRulesEnricherInterface $validationRulesEnricher = null,
): void {
public static function initialize(array $configs = [], ?string $defaultConfig = null): void
{
self::$configs = $configs;
self::$defaultConfig = $defaultConfig;
self::$themes = [];
self::$validationRulesEnricher = $validationRulesEnricher;
}

public static function getTheme(?string $name = null): ?Theme
Expand All @@ -84,9 +74,4 @@ public static function getTheme(?string $name = null): ?Theme

return self::$themes[$name];
}

public static function getValidationRulesEnrichment(BaseField $field, InputDataInterface $inputData): array
{
return self::$validationRulesEnricher?->process($field, $inputData->getValidationRules()) ?? [];
}
}
Loading
Loading