Skip to content

Commit

Permalink
Validate option names
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Aug 11, 2017
1 parent dba79f4 commit 1d49eb6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class SlugOptions implements \IteratorAggregate
public function __construct(iterable $options = [])
{
foreach ($options as $option => $value) {
$this->assertOptionName($option);
$this->{'set'.ucfirst($option)}($value);
}
}
Expand Down Expand Up @@ -95,6 +96,7 @@ public function merge(iterable $options): self
$merged = clone $this;

foreach ($options as $option => $value) {
$this->assertOptionName($option);
$merged->{'set'.ucfirst($option)}($value);
}

Expand Down Expand Up @@ -291,6 +293,28 @@ public function setPostTransforms(iterable $transforms): self
return $this->setTransforms(array_merge($this->transforms, $transforms));
}

/**
* @param string $option
*
* @throws \InvalidArgumentException If it’s an invalid option name
*/
private function assertOptionName(string $option): void
{
static $validOptions = [
'delimiter',
'valid',
'ignore',
'locale',
'transforms',
'preTransforms',
'postTransforms',
];

if (!in_array($option, $validOptions, true)) {
throw new \InvalidArgumentException(sprintf('Unknown option "%s"', $option));
}
}

/**
* @param string $chars
*
Expand Down
8 changes: 8 additions & 0 deletions tests/SlugOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,12 @@ public function getAddTransformThrows()
[new \stdClass, \TypeError::class],
];
}

public function testUnknownOptionThrows()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('(unknown.*"foo")i');

new SlugOptions(['foo' => 'bar']);
}
}

0 comments on commit 1d49eb6

Please sign in to comment.