Skip to content

Commit

Permalink
Attribute: Allow separator customization
Browse files Browse the repository at this point in the history
Some require a comma, e.g. the file input's accept attribute
  • Loading branch information
nilmerg committed Jan 23, 2023
1 parent 7d6fd94 commit 32e2332
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Attribute
/** @var string */
protected $name;

/** @var string The separator used if value is an array */
protected $separator = ' ';

/** @var string|array|bool|null */
protected $value;

Expand Down Expand Up @@ -154,6 +157,30 @@ protected function setName($name)
return $this;
}

/**
* Get the separator by which multiple values are concatenated with
*
* @return string
*/
public function getSeparator(): string
{
return $this->separator;
}

/**
* Set the separator to concatenate multiple values with
*
* @param string $separator
*
* @return $this
*/
public function setSeparator(string $separator): self
{
$this->separator = $separator;

return $this;
}

/**
* Get the value of the attribute
*
Expand Down Expand Up @@ -296,6 +323,6 @@ public function renderName()
*/
public function renderValue()
{
return static::escapeValue($this->value);
return static::escapeValue($this->value, $this->separator);
}
}
10 changes: 10 additions & 0 deletions tests/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public function testConstructorAcceptsArray()
);
}

public function testCustomSeparatorIsUsed()
{
$this->assertSame(
'accept="image/png, image/jpg"',
(new Attribute('accept', ['image/png', 'image/jpg']))
->setSeparator(', ')
->render()
);
}

public function testAttributeNameCanBeRetrieved()
{
$this->assertEquals(
Expand Down

0 comments on commit 32e2332

Please sign in to comment.