Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Bug #40: Refactor tests to use `assertSame()` for rendering with `testRenderWithGlobalDefaultsAreApplied()` and `testRenderWithUserOverridesGlobalDefaults()` (@terabytesoftw)
- Enh #41: Add `InputImage` class for HTML `<input type="image">` element with attributes and rendering capabilities (@terabytesoftw)
- Bug #42: Fix messages in `assert()` methods and code style (@terabytesoftw)
- Bug #43: Update `ui-awesome/html-helper` to version `^0.7` and `ui-awesome/html-mixin` to version `^0.4` in `composer.json` and apply necessary changes to `src` and `tests` directories (@terabytesoftw)

## 0.3.0 March 31, 2024

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"require": {
"php": "^8.1",
"ext-mbstring": "*",
"ui-awesome/html-attribute": "dev-main as 0.5.2",
"ui-awesome/html-attribute": "^0.6@dev",
"ui-awesome/html-core": "^0.6@dev",
"ui-awesome/html-helper": "^0.6",
"ui-awesome/html-interop": "^0.3"
"ui-awesome/html-helper": "^0.7",
"ui-awesome/html-interop": "^0.3",
"ui-awesome/html-mixin": "^0.4"
},
"require-dev": {
"infection/infection": "^0.27|^0.32",
Expand Down
6 changes: 2 additions & 4 deletions src/Embedded/Attribute/HasElementtiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
/**
* Provides an immutable API for the HTML `elementtiming` attribute.
*
* @method static addAttribute(string|UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/elementtiming
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -35,6 +33,6 @@ trait HasElementtiming
*/
public function elementtiming(string|UnitEnum|null $value): static
{
return $this->addAttribute('elementtiming', $value);
return $this->setAttribute('elementtiming', $value);
}
}
6 changes: 2 additions & 4 deletions src/Embedded/Attribute/HasIsmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
/**
* Provides an immutable API for the HTML `ismap` attribute.
*
* @method static addAttribute(string|\UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/img#ismap
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -31,6 +29,6 @@ trait HasIsmap
*/
public function ismap(bool $value): static
{
return $this->addAttribute('ismap', $value);
return $this->setAttribute('ismap', $value);
}
}
6 changes: 2 additions & 4 deletions src/Form/Attribute/HasFormaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/**
* Provides an immutable API for the HTML `formaction` attribute.
*
* @method static addAttribute(string|UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/formaction
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -34,6 +32,6 @@ trait HasFormaction
*/
public function formaction(string|Stringable|UnitEnum|null $value): static
{
return $this->addAttribute('formaction', $value);
return $this->setAttribute('formaction', $value);
}
}
6 changes: 2 additions & 4 deletions src/Form/Attribute/HasFormenctype.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/**
* Provides an immutable API for the HTML `formenctype` attribute.
*
* @method static addAttribute(string|UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/formenctype
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -35,6 +33,6 @@ trait HasFormenctype
*/
public function formenctype(string|Stringable|UnitEnum|null $value): static
{
return $this->addAttribute('formenctype', $value);
return $this->setAttribute('formenctype', $value);
}
}
6 changes: 2 additions & 4 deletions src/Form/Attribute/HasFormmethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/**
* Provides an immutable API for the HTML `formmethod` attribute.
*
* @method static addAttribute(string|UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/formmethod
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -34,6 +32,6 @@ trait HasFormmethod
*/
public function formmethod(string|Stringable|UnitEnum|null $value): static
{
return $this->addAttribute('formmethod', $value);
return $this->setAttribute('formmethod', $value);
}
}
6 changes: 2 additions & 4 deletions src/Form/Attribute/HasFormnovalidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
/**
* Provides an immutable API for the HTML `formnovalidate` attribute.
*
* @method static addAttribute(string|\UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/formnovalidate
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -31,6 +29,6 @@ trait HasFormnovalidate
*/
public function formnovalidate(bool|null $value): static
{
return $this->addAttribute('formnovalidate', $value);
return $this->setAttribute('formnovalidate', $value);
}
}
6 changes: 2 additions & 4 deletions src/Form/Attribute/HasFormtarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/**
* Provides an immutable API for the HTML `formtarget` attribute.
*
* @method static addAttribute(string|UnitEnum $key, mixed $value) Adds an attribute and returns a new instance.
* {@see \UIAwesome\Html\Mixin\HasAttributes} for managing the underlying attributes array.
*
* @mixin \UIAwesome\Html\Mixin\HasAttributes
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/formtarget
*
* @copyright Copyright (C) 2026 Terabytesoftw.
Expand All @@ -35,6 +33,6 @@ trait HasFormtarget
*/
public function formtarget(string|Stringable|UnitEnum|null $value): static
{
return $this->addAttribute('formtarget', $value);
return $this->setAttribute('formtarget', $value);
}
}
115 changes: 115 additions & 0 deletions src/Form/Base/BaseChoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace UIAwesome\Html\Form\Base;

use UIAwesome\Html\Attribute\Form\HasRequired;
use UIAwesome\Html\Attribute\Global\{CanBeAutofocus, HasTabindex};
use UIAwesome\Html\Attribute\HasValue;
use UIAwesome\Html\Core\Element\BaseInput;
use UIAwesome\Html\Core\Html;
use UIAwesome\Html\Form\InputHidden;
use UIAwesome\Html\Form\Mixin\{CanBeEnclosedByLabel, CanBeUnchecked, HasCheckedState};
use UIAwesome\Html\Mixin\HasLabelCollection;
use UIAwesome\Html\Phrasing\Label;

use function array_key_exists;

abstract class BaseChoice extends BaseInput
{
use CanBeAutofocus;
use CanBeEnclosedByLabel;
use CanBeUnchecked;
use HasCheckedState;
use HasLabelCollection;
use HasRequired;
use HasTabindex;
use HasValue;

/**
* Returns the array of HTML attributes for the element.
*
* @return array Attributes array assigned to the element.
*
* @phpstan-return mixed[]
*/
public function getAttributes(): array
{
return $this->buildAttributes(parent::getAttributes());
}

/**
* Renders the `<input>` element with its attributes.
*
* @return string Rendered HTML for the `<input>` element.
*/
protected function run(): string
{
/** @var string|null $id */
$id = $this->getAttribute('id', null);

$unchecked = '';
$uncheckedValue = $this->getUncheckedValue();

if ($uncheckedValue !== null) {
/** @phpstan-var string $name */
$name = $this->getAttribute('name', '');

$unchecked = InputHidden::tag()
->id(null)
->name($name)
->value($uncheckedValue)
->render();
}

$label = $this->getLabel();

if ($this->isLabel() === false) {
return $this->buildElement(
'',
[
'{label}' => '',
'{unchecked}' => $unchecked,
],
);
}

$labelAttributes = $this->getLabelAttributes();
$labelTag = Label::tag()->attributes($labelAttributes);

if ($this->enclosedByLabel === false) {
if (array_key_exists('for', $labelAttributes) === false) {
$labelTag = $labelTag->for($id);
}

$labelTag = $labelTag->content($label);

return $this->buildElement(
'',
[
'{label}' => $labelTag,
'{unchecked}' => $unchecked,
],
);
}

$labelTag = $labelTag
->html(
"\n",
Html::element($this->getTag(), '', $this->getAttributes()),
"\n",
$label,
"\n",
);

return $this->buildElement(
'',
[
'{tag}' => $labelTag,
'{label}' => '',
'{unchecked}' => $unchecked,
],
);
}
}
Loading
Loading