for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\Html\Elements;
use Spatie\Html\Selectable;
use Spatie\Html\BaseElement;
class Select extends BaseElement
{
/** @var string */
protected $tag = 'select';
/** @var array */
protected $options = [];
protected $value = '';
/**
* @param string $name
*
* @return static
*/
public function name(?string $name)
return $this->attribute('name', $name);
}
* @param iterable $options
public function options(iterable $options)
return $this->addChildren($options, function ($text, $value) {
return Option::create()
->value($value)
->text($text)
->selectedIf($value === $this->value);
});
public function placeholder(string $text)
return $this->prependChild(
Option::create()
->selectedIf(! $this->hasSelection())
);
* @param string $value
public function value(?string $value)
$element = clone $this;
$element->value = $value;
$element->applyValueToOptions();
return $element;
protected function hasSelection(): bool
return $this->children->contains->hasAttribute('selected');
protected function applyValueToOptions()
$this->children = $this->children->map(function ($child) {
if ($child instanceof Selectable) {
return $child->selectedIf($this->value === $child->getAttribute('value'));
return $child;