Skip to content

Commit

Permalink
Add phpdoc and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Nov 9, 2022
1 parent 62729c0 commit 7def169
Showing 1 changed file with 61 additions and 23 deletions.
84 changes: 61 additions & 23 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use ipl\Html\Attributes;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Validator\DeferredInArrayValidator;

class SelectElement extends BaseFormElement
Expand All @@ -14,6 +15,7 @@ class SelectElement extends BaseFormElement
/** @var SelectOption[] */
protected $options = [];

/** @var array of SelectOption|HtmlElement */
protected $optionContent = [];

/** @var array Disabled values */
Expand Down Expand Up @@ -74,19 +76,38 @@ public function getValue()
return $value;
}

public function hasOption($value)
/**
* Whether an option with the specified value exists
*
* @param string $value
*
* @return bool
*/
public function hasOption(string $value): bool
{
return isset($this->options[$value]);
}

public function deselect()
/**
* Deselect all values
*
* @return $this
*/
public function deselect(): self
{
$this->setValue(null);

return $this;
}

public function disableOption($value)
/**
* Disable the option with specified value
*
* @param string $value
*
* @return $this
*/
public function disableOption(string $value): self
{
$this->valid = null;
$this->disabledOptions[] = $value;
Expand All @@ -98,7 +119,14 @@ public function disableOption($value)
return $this;
}

public function disableOptions($values)
/**
* Disable the options with specified values
*
* @param array $values
*
* @return $this
*/
public function disableOptions(array $values): self
{
foreach ($values as $value) {
$this->disableOption($value);
Expand All @@ -108,23 +136,25 @@ public function disableOptions($values)
}

/**
* @param $value
* Get the option with specified value
*
* @param string $value
*
* @return SelectOption|null
*/
public function getOption($value)
public function getOption(string $value): ?SelectOption
{
if ($this->hasOption($value)) {
return $this->options[$value];
} else {
return null;
}
return $this->options[$value] ?? null;
}

/**
* @param array $options
* Set the options from specified values
*
* @param array $options `value` => `label`
*
* @return $this
*/
public function setOptions(array $options)
public function setOptions(array $options): self
{
$this->options = [];
$this->optionContent = [];
Expand All @@ -144,7 +174,15 @@ public function addDefaultValidators()
);
}

protected function makeOption($value, $label)
/**
* Make the selectOption for the specified value and the label
*
* @param string $value Value of the option
* @param string|array $label Label of the option
*
* @return SelectOption|HtmlElement
*/
protected function makeOption(string $value, $label)
{
if (is_array($label)) {
$grp = Html::tag('optgroup', ['label' => $value]);
Expand All @@ -153,15 +191,15 @@ protected function makeOption($value, $label)
}

return $grp;
} else {
$option = new SelectOption($value, $label);
$option->getAttributes()->registerAttributeCallback('selected', function () use ($option) {
return $this->isSelectedOption($option->getValue());
});
$this->options[$value] = $option;

return $this->options[$value];
}

$option = new SelectOption($value, $label);
$option->getAttributes()->registerAttributeCallback('selected', function () use ($option) {
return $this->isSelectedOption($option->getValue());
});
$this->options[$value] = $option;

return $this->options[$value];
}

/**
Expand All @@ -171,7 +209,7 @@ protected function makeOption($value, $label)
*
* @return bool
*/
protected function isSelectedOption($optionValue)
protected function isSelectedOption($optionValue): bool
{
if ($this->isMultiSelect) {
return in_array($optionValue, $this->getValue(), ! is_int($optionValue));
Expand Down

0 comments on commit 7def169

Please sign in to comment.