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
46 changes: 24 additions & 22 deletions src/Forms/EmailInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ class EmailInput implements Stringable
private string $id;

/**
* @var string[]
* @var list<string>
*/
private array $wrapperProperties = [];

/**
* @var string[]
* @var list<string>
*/
private array $labelProperties = [];

/**
* @var string[]
* @var list<string>
*/
private array $inputProperties = [];

/**
* @var array<string|Stringable>
* @var list<string|Stringable>
*/
private array $content = [];

Expand Down Expand Up @@ -58,53 +58,62 @@ public function id(string $id): self

public function wrapperProps(string ...$properties): self
{
$this->wrapperProperties = $properties;
$this->wrapperProperties = array_values($properties);
return $this;
}

public function labelProps(string ...$properties): self
{
$this->labelProperties = $properties;
$this->labelProperties = array_values($properties);
return $this;
}

public function inputProps(string ...$properties): self
{
$this->inputProperties = $properties;
$this->inputProperties = array_values($properties);
return $this;
}

public function content(string|Stringable ...$content): self
{
$this->content = $content;
$this->content = array_values($content);
return $this;
}

private function warningMessage(string $warningId): string|PropertyInterface
private function warningMessage(): string
{
if ($this->warningMessage === '') {
return '';
}

if (is_string($this->warningMessage)) {
return Element::p($this->warningMessage)->props(
'id ' . $warningId
return (string) Element::p($this->warningMessage)->props(
'id ' . $this->id . '-warning'
);
}

$warning = clone $this->warningMessage;

return $warning->prop('id ' . $warningId);
return (string) $warning->prop('id ' . $this->id . '-warning');
}

public function __toString(): string
/**
* @return list<string>
*/
private function inputProperties(bool $noWarning): array
{
$inputProperties = $this->warningMessage === ''
return ($noWarning)
? $this->inputProperties
: ControlProperties::withWarning(
array_values($this->inputProperties),
$this->inputProperties,
$this->id . '-warning'
);
}

public function __toString(): string
{
$warning = self::warningMessage();
$inputProperties = self::inputProperties($warning === '');

$input = Element::input()->omitEndTag()->props(...[
...$inputProperties,
Expand All @@ -116,13 +125,6 @@ public function __toString(): string
'autocorrect off'
]);

$warning = '';
if ($this->warningMessage !== '') {
$warningId = $this->id . '-warning';

$warning = (string) self::warningMessage($warningId);
}

return (string) Element::div(
Element::label($this->label)->props(
'for ' . $this->id,
Expand Down
119 changes: 75 additions & 44 deletions src/Forms/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@

class Password implements Stringable
{
// phpcs:disable
private string $warningId {
get => $this->name . '-warning';
}
// phpcs:enable
private string $id;

/**
* @var list<string>
*/
private array $wrapperProperties = [];

/**
* @var string[]
* @var list<string>
*/
private array $labelProperties = [];

/**
* @var string[]
* @var list<string>
*/
private array $inputProperties = [];

/**
* @var array<string|Stringable>
*/
private array $content = [];

private PasswordAutocomplete $autocomplete = PasswordAutocomplete::CurrentPassword;

Expand All @@ -44,81 +49,107 @@ final private function __construct(
private readonly string|Stringable $name,
private readonly string|PropertyInterface $warningMessage = ''
) {
$this->id = (string) $name;
}

public function labelProps(string ...$properties): self
public function id(string $id): self
{
$this->labelProperties = $properties;
$this->id = $id;
return $this;
}

public function inputProps(string ...$properties): self
public function wrapperProps(string ...$properties): self
{
$this->inputProperties = $properties;
$this->wrapperProperties = array_values($properties);
return $this;
}

public function forNewPassword(): self
public function labelProps(string ...$properties): self
{
$this->autocomplete = PasswordAutocomplete::NewPassword;
$this->labelProperties = array_values($properties);
return $this;
}

public function autocompleteOff(): self
public function inputProps(string ...$properties): self
{
$this->autocomplete = PasswordAutocomplete::Off;
$this->inputProperties = array_values($properties);
return $this;
}

public function autocompleteOn(): self
public function content(string|Stringable ...$content): self
{
$this->autocomplete = PasswordAutocomplete::On;
$this->content = array_values($content);
return $this;
}

private function warningMessage(): string|PropertyInterface
private function warningMessage(): string
{
if ($this->warningMessage === '') {
return '';
}

if (is_string($this->warningMessage)) {
return Element::p($this->warningMessage)->props(
'id ' . $this->warningId
return (string) Element::p($this->warningMessage)->props(
'id ' . $this->id . '-warning'
);
}

return $this->warningMessage->prop('id ' . $this->warningId);
$warning = clone $this->warningMessage;

return (string) $warning->prop('id ' . $this->id . '-warning');
}

/**
* @return list<string>
*/
private function inputProperties(bool $noWarning): array
{
return ($noWarning)
? $this->inputProperties
: ControlProperties::withWarning(
$this->inputProperties,
$this->id . '-warning'
);
}

public function forNewPassword(): self
{
$this->autocomplete = PasswordAutocomplete::NewPassword;
return $this;
}

public function autocompleteOff(): self
{
$this->autocomplete = PasswordAutocomplete::Off;
return $this;
}

public function autocompleteOn(): self
{
$this->autocomplete = PasswordAutocomplete::On;
return $this;
}

public function __toString(): string
{
$elements = [];
$elements[] = Element::label($this->label)->props(
'for ' . $this->name,
...$this->labelProperties
);
$warning = self::warningMessage();
$inputProperties = self::inputProperties($warning === '');

$input = Element::input()->omitEndTag()->props(
$input = Element::input()->omitEndTag()->props(...[
...$inputProperties,
'type password',
'id ' . $this->name,
'id ' . $this->id,
'name ' . $this->name,
'autocomplete ' . $this->autocomplete->value,
...$this->inputProperties
);

$warningMessage = self::warningMessage();
$hasWarningMessage = ($warningMessage !== '');
if ($hasWarningMessage) {
$input = $input->prop('aria-invalid true');
$input = $input->prop('aria-describedby ' . $this->warningId);
}
$elements[] = $input;

if ($hasWarningMessage) {
$elements[] = $warningMessage;
}

return implode('', array_map('strval', $elements));
]);

return (string) Element::div(
Element::label($this->label)->props(...[
...$this->labelProperties,
'for ' . $this->id
]),
$input,
...[...$this->content, $warning]
)->props(...$this->wrapperProperties);
}
}
Loading
Loading