Skip to content

Commit

Permalink
Merge pull request #247 from gsteel/multi-checkbox-value-options
Browse files Browse the repository at this point in the history
Improve type inference for MultiCheckbox Value Options
  • Loading branch information
Slamdunk authored Nov 22, 2023
2 parents dc899f6 + 81aadca commit 2ec86da
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Element/MultiCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@

use const E_USER_DEPRECATED;

/**
* @psalm-type ValueOptionSpec = array<string, string>|list<array{
* label: non-empty-string,
* value: non-empty-string,
* selected?: bool,
* disabled?: bool,
* attributes?: array<string, scalar|null>,
* label_attributes?: array<string, scalar|null>,
* }>
*/
class MultiCheckbox extends Checkbox
{
/** @var array<string, scalar|null> */
Expand All @@ -32,18 +42,19 @@ class MultiCheckbox extends Checkbox
/** @var null|string */
protected $uncheckedValue;

/** @var array */
/** @var ValueOptionSpec */
protected $valueOptions = [];

/**
* @return array
* @return ValueOptionSpec
*/
public function getValueOptions(): array
{
return $this->valueOptions;
}

/**
* @param ValueOptionSpec $options
* @return $this
*/
public function setValueOptions(array $options)
Expand All @@ -61,6 +72,11 @@ public function setValueOptions(array $options)
}

/**
* Unset a value option
*
* This method will only unset a value option when the element was created with a simple array of key-value pairs
* for value options, for example ['value1' => 'label1', 'value2' => 'label2']
*
* @return $this
*/
public function unsetValueOption(string $key)
Expand Down
75 changes: 75 additions & 0 deletions test/StaticAnalysis/MultiCheckboxValueOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\StaticAnalysis;

use Laminas\Form\Element\MultiCheckbox;

/** @psalm-suppress UnusedClass, UnusedMethod */
final class MultiCheckboxValueOptions
{
public function checkValueOptionsVariations(): void
{
$element = new MultiCheckbox('foo');
$element->setValueOptions([
'a' => 'A',
'b' => 'B',
]);

$element = new MultiCheckbox('foo');
$element->setValueOptions([
[
'label' => 'Foo',
'value' => 'Bar',
],
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => false,
],
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => true,
'selected' => true,
],
[
'label' => 'Foo',
'value' => 'Bar',
'attributes' => [
'readonly' => true,
'something' => 'else',
],
],
[
'label' => 'Foo',
'value' => 'Bar',
'label_attributes' => [
'inert' => true,
'class' => 'baz',
],
],
]);

$element = new MultiCheckbox('foo');
$element->setValueOptions([
'foo' => 'bar',
[
'label' => 'Foo',
'value' => 'Bar',
'disabled' => true,
'selected' => true,
'attributes' => [
'readonly' => true,
'something' => 'else',
],
'label_attributes' => [
'inert' => true,
'class' => 'baz',
],
],
'bing' => 'bong',
]);
}
}
6 changes: 6 additions & 0 deletions test/View/Helper/FormMultiCheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function testUsesOptionsAttributeToGenerateCheckBoxes(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -120,6 +122,8 @@ public function testGenerateCheckBoxesAndHiddenElement(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -157,6 +161,8 @@ public function testAllowsSpecifyingLabelPosition(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('<label>%s<', $label), $markup);
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/View/Helper/FormRadioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function testUsesOptionsAttributeToGenerateRadioOptions(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -117,6 +119,8 @@ public function testGenerateRadioOptionsAndHiddenElement(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('>%s</label>', $label), $markup);
self::assertStringContainsString(sprintf('value="%s"', $value), $markup);
}
Expand Down Expand Up @@ -154,6 +158,8 @@ public function testAllowsSpecifyingLabelPosition(): void
self::assertEquals(3, substr_count($markup, '<label'));

foreach ($options as $value => $label) {
self::assertIsString($value);
self::assertIsString($label);
self::assertStringContainsString(sprintf('<label>%s<', $label), $markup);
}
}
Expand Down

0 comments on commit 2ec86da

Please sign in to comment.