diff --git a/src/SlugOptions.php b/src/SlugOptions.php index 111d104..f159e65 100644 --- a/src/SlugOptions.php +++ b/src/SlugOptions.php @@ -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); } } @@ -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); } @@ -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 * diff --git a/tests/SlugOptionsTest.php b/tests/SlugOptionsTest.php index f824be5..e48ac7d 100644 --- a/tests/SlugOptionsTest.php +++ b/tests/SlugOptionsTest.php @@ -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']); + } }