Skip to content
Open
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ipl/stdlib": ">=0.12.0",
"ipl/validator": ">=0.5.0",
"psr/http-message": "^1.1",
"guzzlehttp/psr7": "^2.5"
"guzzlehttp/psr7": "^2.8"
},
"require-dev": {
"ext-dom": "*",
Expand Down
8 changes: 4 additions & 4 deletions src/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @phpstan-import-type AttributeValue from Attribute
* @phpstan-type AttributesType array<string, AttributeValue>
*/
class Attributes implements ArrayAccess, IteratorAggregate

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Class ipl\Html\Attributes implements generic interface IteratorAggregate but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Class ipl\Html\Attributes implements generic interface ArrayAccess but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Class ipl\Html\Attributes implements generic interface IteratorAggregate but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Class ipl\Html\Attributes implements generic interface ArrayAccess but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Class ipl\Html\Attributes implements generic interface IteratorAggregate but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Class ipl\Html\Attributes implements generic interface ArrayAccess but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Class ipl\Html\Attributes implements generic interface IteratorAggregate but does not specify its types: TKey, TValue

Check failure on line 24 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Class ipl\Html\Attributes implements generic interface ArrayAccess but does not specify its types: TKey, TValue
{
/** @var Attribute[] */
protected $attributes = [];
Expand All @@ -38,9 +38,9 @@
/**
* Create new HTML attributes
*
* @param AttributesType $attributes
* @param ?AttributesType $attributes
*/
public function __construct(array $attributes = null)
public function __construct(?array $attributes = null)
{
if (empty($attributes)) {
return;
Expand All @@ -60,11 +60,11 @@
/**
* Create new HTML attributes
*
* @param AttributesType $attributes
* @param ?AttributesType $attributes
*
* @return static
*/
public static function create(array $attributes = null)
public static function create(?array $attributes = null)
{
return new static($attributes);
}
Expand Down Expand Up @@ -508,7 +508,7 @@
*
* @return Attribute[]|ArrayIterator
*/
public function getIterator(): Traversable

Check failure on line 511 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.2) / PHPStan 8.2

Method ipl\Html\Attributes::getIterator() return type with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 511 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.5) / PHPStan 8.5

Method ipl\Html\Attributes::getIterator() return type with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 511 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.3) / PHPStan 8.3

Method ipl\Html\Attributes::getIterator() return type with generic class ArrayIterator does not specify its types: TKey, TValue

Check failure on line 511 in src/Attributes.php

View workflow job for this annotation

GitHub Actions / PHP / Static analysis (8.4) / PHPStan 8.4

Method ipl\Html\Attributes::getIterator() return type with generic class ArrayIterator does not specify its types: TKey, TValue
{
return new ArrayIterator($this->attributes);
}
Expand Down
1 change: 0 additions & 1 deletion src/FormElement/BaseFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ protected function getValueOfNameAttribute()
$attributes = $this->getAttributes();

$callbacksProperty = new ReflectionProperty(get_class($attributes), 'callbacks');
$callbacksProperty->setAccessible(true);
$callbacks = $callbacksProperty->getValue($attributes);

if (isset($callbacks['name'])) {
Expand Down
11 changes: 8 additions & 3 deletions src/FormElement/RadioElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function setOptions(array $options): self
*
* @throws InvalidArgumentException If no option with the specified value exists
*/
public function getOption($value): RadioOption
public function getOption(string|int $value): RadioOption
{
if (! isset($this->options[$value])) {
throw new InvalidArgumentException(sprintf('There is no such option "%s"', $value));
Expand Down Expand Up @@ -127,10 +127,15 @@ protected function assemble()
'checked',
function () use ($option) {
$optionValue = $option->getValue();
$value = $this->getValue();

if ($optionValue === '' && $value === null) {
return true;
}

return ! is_int($optionValue)
? $this->getValue() === $optionValue
: $this->getValue() == $optionValue;
? $value === $optionValue
: $value == $optionValue;
}
)
->registerAttributeCallback(
Expand Down
12 changes: 6 additions & 6 deletions src/FormElement/RadioOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RadioOption
/** @var string The default label class */
public const LABEL_CLASS = 'radio-label';

/** @var string|int|null Value of the option */
/** @var string|int Value of the option */
protected $value;

/** @var string Label of the option */
Expand All @@ -27,12 +27,12 @@ class RadioOption
/**
* RadioOption constructor.
*
* @param string|int|null $value
* @param string|int $value
* @param string $label
*/
public function __construct($value, string $label)
public function __construct(string|int $value, string $label)
{
$this->value = $value === '' ? null : $value;
$this->value = $value;
$this->label = $label;
}

Expand Down Expand Up @@ -63,9 +63,9 @@ public function getLabel(): string
/**
* Get the value of the option
*
* @return string|int|null
* @return string|int
*/
public function getValue()
public function getValue(): string|int
{
return $this->value;
}
Expand Down
27 changes: 13 additions & 14 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ipl\Html\FormElement;

use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\Common\MultipleAttribute;
use ipl\Html\Html;
Expand Down Expand Up @@ -32,11 +31,11 @@ class SelectElement extends BaseFormElement
/**
* Get the option with specified value
*
* @param string|int|null $value
* @param string|int $value
*
* @return ?SelectOption
*/
public function getOption($value): ?SelectOption
public function getOption(string|int $value): ?SelectOption
{
return $this->options[$value] ?? null;
}
Expand Down Expand Up @@ -75,7 +74,7 @@ public function setDisabledOptions(array $disabledOptions): self
$option->setAttribute(
'disabled',
in_array($optionValue, $disabledOptions, ! is_int($optionValue))
|| ($optionValue === null && in_array('', $disabledOptions, true))
|| ($optionValue === '' && in_array(null, $disabledOptions, true))
);
}

Expand Down Expand Up @@ -119,12 +118,12 @@ public function getNameAttribute()
/**
* Make the selectOption for the specified value and the label
*
* @param string|int|null $value Value of the option
* @param string|int $value Value of the option
* @param string|array $label Label of the option
*
* @return SelectOption|HtmlElement
*/
protected function makeOption($value, $label)
protected function makeOption(string|int $value, string|array $label)
{
if (is_array($label)) {
$grp = Html::tag('optgroup', ['label' => $value]);
Expand All @@ -150,27 +149,23 @@ protected function makeOption($value, $label)
/**
* Get whether the given option is selected
*
* @param int|string|null $optionValue
* @param string|int $optionValue
*
* @return bool
*/
protected function isSelectedOption($optionValue): bool
protected function isSelectedOption(string|int $optionValue): bool
{
$value = $this->getValue();

if ($optionValue === '') {
$optionValue = null;
}

if ($this->isMultiple()) {
if (! is_array($value)) {
throw new UnexpectedValueException(
'Value must be an array when the `multiple` attribute is set to `true`'
);
}

return in_array($optionValue, $this->getValue(), ! is_int($optionValue))
|| ($optionValue === null && in_array('', $this->getValue(), true));
return in_array($optionValue, $value, ! is_int($optionValue))
|| ($optionValue === '' && in_array(null, $value, true));
}

if (is_array($value)) {
Expand All @@ -179,6 +174,10 @@ protected function isSelectedOption($optionValue): bool
);
}

if ($optionValue === '' && $value === null) {
return true;
}

return is_int($optionValue)
// The loose comparison is required because PHP casts
// numeric strings to integers if used as array keys
Expand Down
10 changes: 5 additions & 5 deletions src/FormElement/SelectOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SelectOption extends BaseHtmlElement
{
protected $tag = 'option';

/** @var string|int|null Value of the option */
/** @var string|int Value of the option */
protected $value;

/** @var string Label of the option */
Expand All @@ -17,12 +17,12 @@ class SelectOption extends BaseHtmlElement
/**
* SelectOption constructor.
*
* @param string|int|null $value
* @param string|int $value
* @param string $label
*/
public function __construct($value, string $label)
public function __construct(string|int $value, string $label)
{
$this->value = $value === '' ? null : $value;
$this->value = $value;
$this->label = $label;

$this->getAttributes()->registerAttributeCallback('value', [$this, 'getValueAttribute']);
Expand Down Expand Up @@ -55,7 +55,7 @@ public function getLabel(): string
/**
* Get the value of the option
*
* @return string|int|null
* @return string|int
*/
public function getValue()
{
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class HtmlElement extends BaseHtmlElement
* Create a new HTML element from the given tag, attributes and content
*
* @param string $tag The tag for the element
* @param Attributes $attributes The HTML attributes for the element
* @param ?Attributes $attributes The HTML attributes for the element
* @param ValidHtml ...$content The content of the element
*/
public function __construct($tag, Attributes $attributes = null, ValidHtml ...$content)
public function __construct($tag, ?Attributes $attributes = null, ValidHtml ...$content)
{
$this->tag = $tag;

Expand Down
4 changes: 2 additions & 2 deletions tests/DocumentationFormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testSelectElement()
$form->addElement('select', 'customer', [
'label' => 'Customer',
'options' => [
null => 'Please choose',
'' => 'Please choose',
'1' => 'The one',
'4' => 'Four',
'5' => 'Hi five',
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testSetValues()
$form->addElement('select', 'customer', [
'label' => 'Customer',
'options' => [
null => 'Please choose',
'' => 'Please choose',
'1' => 'The one',
'4' => 'Four',
'5' => 'Hi five',
Expand Down
2 changes: 1 addition & 1 deletion tests/FormElement/FieldsetElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class FieldsetElementTest extends TestCase
{
private const SELECT_OPTIONS_TO_TEST = [
null => 'Nothing',
'' => 'Nothing',
1 => 'One',
'two' => 2,
];
Expand Down
Loading
Loading