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

Allow pass null argument to methods Tag::class(), Tag::replaceClass(), BooleanInputTag::label() and BooleanInputTag::sideLabel() #78

Merged
merged 9 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- New #74: Add classes for tags `Em`, `Strong`, `B` and `I` (vjik)
- New #75: Add methods `as()` and `preload()` to the `Link` tag (vjik)
- New #78: Allow pass `null` argument to methods `Tag::class()` and `Tag::replaceClass()` (vjik)

## 1.2.0 May 04, 2021

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"yiisoft/json": "^1.0"
},
"require-dev": {
"maglnet/composer-require-checker": "^3.2",
"maglnet/composer-require-checker": "^3.3",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.7",
"roave/infection-static-analysis-plugin": "^1.8",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.7"
"vimeo/psalm": "^4.8"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 4 additions & 1 deletion src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,10 @@ public static function renderTagAttributes(array $attributes): string
if (empty($value)) {
continue;
}
/** @psalm-var array<string, string> $value */
/**
* @psalm-suppress UnnecessaryVarAnnotation
* @psalm-var array<string, string> $value
*/
$html .= " $name=\"" . self::encodeAttribute(self::cssStyleFromArray($value)) . '"';
} else {
$html .= " $name='" . Json::htmlEncode($value) . "'";
Expand Down
15 changes: 9 additions & 6 deletions src/Tag/Base/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,32 @@ final public function id(?string $id): self
/**
* Add one or more CSS classes to the tag.
*
* @param string ...$class One or many CSS classes.
* @param string|null ...$class One or many CSS classes.
*
* @return static
*/
final public function class(string ...$class): self
final public function class(?string ...$class): self
{
$new = clone $this;
/** @psalm-suppress MixedArgumentTypeCoercion */
Html::addCssClass($new->attributes, $class);
Html::addCssClass(
$new->attributes,
array_filter($class, static fn ($c) => $c !== null),
);
return $new;
}

/**
* Replace current tag CSS classes with a new set of classes.
*
* @param string ...$class One or many CSS classes.
* @param string|null ...$class One or many CSS classes.
*
* @return static
*/
final public function replaceClass(string ...$class): self
final public function replaceClass(?string ...$class): self
{
$new = clone $this;
$new->attributes['class'] = $class;
$new->attributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;
}

Expand Down
23 changes: 22 additions & 1 deletion tests/common/Tag/Base/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function dataAttributes(): array
'<test checked disabled required="yes">',
['checked' => true, 'disabled' => true, 'hidden' => false, 'required' => 'yes'],
],
['<test class="">', ['class' => '']],
['<test class="red">', ['class' => 'red']],
['<test class="first second">', ['class' => ['first', 'second']]],
['<test>', ['class' => []]],
['<test style="width: 100px; height: 200px;">', ['style' => ['width' => '100px', 'height' => '200px']]],
Expand Down Expand Up @@ -141,10 +143,29 @@ public function testClass(string $expected, array $class): void
$this->assertSame($expected, (string)TestTag::tag()->class('main')->class(...$class));
}

public function dataNewClass(): array
{
return [
['<test>', null],
['<test class="">', ''],
['<test class="red">', 'red'],
];
}

/**
* @dataProvider dataNewClass
*/
public function testNewClass(string $expected, ?string $class): void
{
$this->assertSame($expected, (string)TestTag::tag()->class($class));
}

public function dataReplaceClass(): array
{
return [
['<test>', []],
['<test>', [null]],
['<test class="">', ['']],
['<test class="main">', ['main']],
['<test class="main bold">', ['main bold']],
['<test class="main bold">', ['main', 'bold']],
Expand All @@ -158,7 +179,7 @@ public function dataReplaceClass(): array
*/
public function testReplaceClass(string $expected, array $class): void
{
$this->assertSame($expected, (string)TestTag::tag()->replaceClass(...$class));
$this->assertSame($expected, (string)TestTag::tag()->class('red')->replaceClass(...$class));
}

public function testImmutability(): void
Expand Down