From 1d49eb6309501b7ca0bc6da93eaa8953c26d2924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Fri, 11 Aug 2017 11:27:03 +0200 Subject: [PATCH] Validate option names --- src/SlugOptions.php | 24 ++++++++++++++++++++++++ tests/SlugOptionsTest.php | 8 ++++++++ 2 files changed, 32 insertions(+) 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']); + } }