Skip to content

Commit

Permalink
Rename options to make them unambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Sep 10, 2017
1 parent 1d49eb6 commit cab998a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 78 deletions.
10 changes: 5 additions & 5 deletions src/SlugGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ public function generate(string $text, iterable $options = []): string

$options = $this->options->merge($options);

if ($options->getValid() === '') {
if ($options->getValidChars() === '') {
return '';
}

$text = \Normalizer::normalize($text, \Normalizer::FORM_C);
$text = $this->removeIgnored($text, $options->getIgnore());
$text = $this->transform($text, $options->getValid(), $options->getTransforms(), $options->getLocale());
$text = $this->removeIgnored($text, $options->getIgnore());
$text = $this->replaceWithDelimiter($text, $options->getValid(), $options->getDelimiter());
$text = $this->removeIgnored($text, $options->getIgnoreChars());
$text = $this->transform($text, $options->getValidChars(), $options->getTransforms(), $options->getLocale());
$text = $this->removeIgnored($text, $options->getIgnoreChars());
$text = $this->replaceWithDelimiter($text, $options->getValidChars(), $options->getDelimiter());

return $text;
}
Expand Down
40 changes: 20 additions & 20 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class SlugOptions implements \IteratorAggregate
/**
* @var string
*/
private $valid = 'a-z0-9';
private $validChars = 'a-z0-9';

/**
* @var string
*/
private $ignore = '\p{Mn}\p{Lm}';
private $ignoreChars = '\p{Mn}\p{Lm}';

/**
* @var string
Expand Down Expand Up @@ -125,53 +125,53 @@ public function getDelimiter(): string
}

/**
* @param string $valid Character range for allowed characters
* @param string $chars Character range for allowed characters
* in the form of a regular expression character set,
* e.g. `abc`, `a-z0-9` or `\p{Ll}\-_`
*
* @return static
*/
public function setValid(string $valid): self
public function setValidChars(string $chars): self
{
$this->assertCharacterClass($valid);
$this->assertCharacterClass($chars);

$this->valid = $valid;
$this->setOptions['valid'] = null;
$this->validChars = $chars;
$this->setOptions['validChars'] = null;

return $this;
}

/**
* @return string
*/
public function getValid(): string
public function getValidChars(): string
{
return $this->valid;
return $this->validChars;
}

/**
* @param string $ignore Range of characters that get ignored
* in the form of a regular expression character set,
* e.g. `abc`, `a-z0-9` or `\p{Ll}\-_`
* @param string $chars Range of characters that get ignored
* in the form of a regular expression character set,
* e.g. `abc`, `a-z0-9` or `\p{Ll}\-_`
*
* @return static
*/
public function setIgnore(string $ignore): self
public function setIgnoreChars(string $chars): self
{
$this->assertCharacterClass($ignore);
$this->assertCharacterClass($chars);

$this->ignore = $ignore;
$this->setOptions['ignore'] = null;
$this->ignoreChars = $chars;
$this->setOptions['ignoreChars'] = null;

return $this;
}

/**
* @return string
*/
public function getIgnore(): string
public function getIgnoreChars(): string
{
return $this->ignore;
return $this->ignoreChars;
}

/**
Expand Down Expand Up @@ -302,8 +302,8 @@ private function assertOptionName(string $option): void
{
static $validOptions = [
'delimiter',
'valid',
'ignore',
'validChars',
'ignoreChars',
'locale',
'transforms',
'preTransforms',
Expand Down
38 changes: 19 additions & 19 deletions tests/SlugGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,49 @@ public function getGenerate(): array
'Ö Äpfel-Fuß',
'OE-Aepfel-Fuss',
//'OE-AEpfel-Fuss',
['valid' => 'a-zA-Z', 'locale' => 'de'],
['validChars' => 'a-zA-Z', 'locale' => 'de'],
],
[
'Ö Ä Ü ẞ ÖX ÄX ÜX ẞX Öx Äx Üx ẞx Öö Ää Üü ẞß',
'OE-AE-UE-SS-OEX-AEX-UEX-SSX-Oex-Aex-Uex-SSx-Oeoe-Aeae-Ueue-SSss',
//'OE-AE-UE-SS-OEX-AEX-UEX-SSX-OEx-AEx-UEx-SSx-Oeoe-Aeae-Ueue-SSss',
['valid' => 'a-zA-Z', 'locale' => 'de'],
['validChars' => 'a-zA-Z', 'locale' => 'de'],
],
[
"O\u{308} A\u{308} U\u{308} O\u{308}X A\u{308}X U\u{308}X O\u{308}x A\u{308}x U\u{308}x O\u{308}o\u{308} A\u{308}a\u{308} U\u{308}u\u{308}",
'OE-AE-UE-OEX-AEX-UEX-Oex-Aex-Uex-Oeoe-Aeae-Ueue',
//'OE-AE-UE-OEX-AEX-UEX-OEx-AEx-UEx-Oeoe-Aeae-Ueue',
['valid' => 'a-zA-Z', 'locale' => 'de'],
['validChars' => 'a-zA-Z', 'locale' => 'de'],
],
[
'Ö Äpfel-Fuß',
'ö-äpfel-fuß',
['valid' => 'a-zäöüß'],
['validChars' => 'a-zäöüß'],
],
[
'ö-äpfel-fuß',
'OE__AEPFEL__FUSS',
['valid' => 'A-Z', 'delimiter' => '__', 'locale' => 'de'],
['validChars' => 'A-Z', 'delimiter' => '__', 'locale' => 'de'],
],
['İNATÇI', 'inatci'],
[
'inatçı',
'INATCI',
['valid' => 'A-Z'],
['validChars' => 'A-Z'],
],
[
'İNATÇI',
'inatçı',
[
'valid' => 'a-pr-vyzçğıöşü', // Turkish alphabet
'validChars' => 'a-pr-vyzçğıöşü', // Turkish alphabet
'locale' => 'tr',
],
],
[
'inatçı',
'İNATÇI',
[
'valid' => 'A-PR-VYZÇĞİÖŞÜ', // Turkish alphabet
'validChars' => 'A-PR-VYZÇĞİÖŞÜ', // Turkish alphabet
'locale' => 'tr',
],
],
Expand All @@ -126,32 +126,32 @@ public function getGenerate(): array
[
'富士山',
'fù-shì-shān',
['valid' => '\p{Latin}'],
['validChars' => '\p{Latin}'],
],
[
'Exämle <!-- % {{BR}} --> <a href="http://example.com">',
'exämle-br-a-href-http-example-com',
['valid' => '\p{Ll}'],
['validChars' => '\p{Ll}'],
],
[
'Exämle <!-- % {{BR}} --> <a href="http://example.com">',
'EXÄMLE-BR-A-HREF-HTTP-EXAMPLE-COM',
['valid' => '\p{Lu}'],
['validChars' => '\p{Lu}'],
],
[
'Lj Nj Dz',
'lj-nj-dz',
['valid' => '\p{Ll}'],
['validChars' => '\p{Ll}'],
],
[
'Lj Nj Dz',
'LJ-NJ-DZ',
['valid' => '\p{Lu}'],
['validChars' => '\p{Lu}'],
],
[
'ABC',
'ac',
['ignore' => 'b'],
['ignoreChars' => 'b'],
],
[
'ABʹC',
Expand All @@ -160,17 +160,17 @@ public function getGenerate(): array
[
'ABʹC',
'ab-c',
['ignore' => ''],
['ignoreChars' => ''],
],
[
'Don’t they\'re',
'dont-theyre',
['ignore' => '\''],
['ignoreChars' => '\''],
],
[
'фильм',
'film',
['ignore' => '\p{Mn}\p{Lm}\pP'],
['ignoreChars' => '\p{Mn}\p{Lm}\pP'],
],
['Україна', 'ukraina'],
['0 1 9 ⑽ ⒒ ¼ Ⅻ', '0-1-9-10-11-1-4-xii'],
Expand All @@ -180,7 +180,7 @@ public function getGenerate(): array
'abc',
'1b3',
[
'valid' => 'b\d',
'validChars' => 'b\d',
'transforms' => ['a > 1; b > 1; c > 3;'],
],
],
Expand All @@ -207,7 +207,7 @@ public function getGenerate(): array
[
'-A B C-',
'',
['valid' => ''],
['validChars' => ''],
],
];
}
Expand Down
68 changes: 34 additions & 34 deletions tests/SlugOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function testMerge()
$options = (new SlugOptions)->merge(new SlugOptions(['delimiter' => 'x']));

$this->assertSame('x', $options->getDelimiter());
$this->assertSame('a-z0-9', $options->getValid());
$this->assertSame('a-z0-9', $options->getValidChars());

$options2 = $options->merge(new SlugOptions(['valid' => 'x']));
$options2 = $options->merge(new SlugOptions(['validChars' => 'x']));
$this->assertSame('x', $options->getDelimiter());
$this->assertSame('a-z0-9', $options->getValid());
$this->assertSame('a-z0-9', $options->getValidChars());
$this->assertSame('x', $options2->getDelimiter());
$this->assertSame('x', $options2->getValid());
$this->assertSame('x', $options2->getValidChars());
}

public function testGetIterator()
Expand All @@ -53,8 +53,8 @@ public function testGetIterator()
$options->setDelimiter('-');
$this->assertSame(['delimiter' => '-'], iterator_to_array($options));

$options->setValid('x');
$this->assertSame(['delimiter' => '-', 'valid' => 'x'], iterator_to_array($options));
$options->setValidChars('x');
$this->assertSame(['delimiter' => '-', 'validChars' => 'x'], iterator_to_array($options));
}

public function testSetDelimiter()
Expand All @@ -69,66 +69,66 @@ public function testSetDelimiter()
$this->assertSame('xx', $options->setDelimiter('xx')->getDelimiter());
}

public function testSetValid()
public function testSetValidChars()
{
$options = new SlugOptions;
$this->assertSame('a-z0-9', $options->getValid());

$options = new SlugOptions(['valid' => 'x']);
$this->assertSame('x', $options->getValid());
$this->assertSame('', $options->setValid('')->getValid());
$this->assertSame('a', $options->setValid('a')->getValid());
$this->assertSame('a-c', $options->setValid('a-c')->getValid());
$this->assertSame('\p{Ll}', $options->setValid('\p{Ll}')->getValid());
$this->assertSame('/', $options->setValid('/')->getValid());
$this->assertSame('\\\\', $options->setValid('\\\\')->getValid());
$this->assertSame('{}*?.\\]', $options->setValid('{}*?.\\]')->getValid());
$this->assertSame('a-z0-9', $options->getValidChars());

$options = new SlugOptions(['validChars' => 'x']);
$this->assertSame('x', $options->getValidChars());
$this->assertSame('', $options->setValidChars('')->getValidChars());
$this->assertSame('a', $options->setValidChars('a')->getValidChars());
$this->assertSame('a-c', $options->setValidChars('a-c')->getValidChars());
$this->assertSame('\p{Ll}', $options->setValidChars('\p{Ll}')->getValidChars());
$this->assertSame('/', $options->setValidChars('/')->getValidChars());
$this->assertSame('\\\\', $options->setValidChars('\\\\')->getValidChars());
$this->assertSame('{}*?.\\]', $options->setValidChars('{}*?.\\]')->getValidChars());
}

/**
* @dataProvider getInvalidCharacterClasses
*
* @param mixed $valid
*/
public function testSetValidThrows($valid)
public function testSetValidCharsThrows($valid)
{
$options = new SlugOptions;

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('("'.preg_quote($valid).'")');

$options->setValid($valid);
$options->setValidChars($valid);
}

public function testSetIgnore()
public function testSetIgnoreChars()
{
$options = new SlugOptions;
$this->assertSame('\p{Mn}\p{Lm}', $options->getIgnore());

$options = new SlugOptions(['ignore' => 'x']);
$this->assertSame('x', $options->getIgnore());
$this->assertSame('', $options->setIgnore('')->getIgnore());
$this->assertSame('a', $options->setIgnore('a')->getIgnore());
$this->assertSame('a-c', $options->setIgnore('a-c')->getIgnore());
$this->assertSame('\p{Ll}', $options->setIgnore('\p{Ll}')->getIgnore());
$this->assertSame('/', $options->setIgnore('/')->getIgnore());
$this->assertSame('\\\\', $options->setIgnore('\\\\')->getIgnore());
$this->assertSame('{}*?.\\]', $options->setIgnore('{}*?.\\]')->getIgnore());
$this->assertSame('\p{Mn}\p{Lm}', $options->getIgnoreChars());

$options = new SlugOptions(['ignoreChars' => 'x']);
$this->assertSame('x', $options->getIgnoreChars());
$this->assertSame('', $options->setIgnoreChars('')->getIgnoreChars());
$this->assertSame('a', $options->setIgnoreChars('a')->getIgnoreChars());
$this->assertSame('a-c', $options->setIgnoreChars('a-c')->getIgnoreChars());
$this->assertSame('\p{Ll}', $options->setIgnoreChars('\p{Ll}')->getIgnoreChars());
$this->assertSame('/', $options->setIgnoreChars('/')->getIgnoreChars());
$this->assertSame('\\\\', $options->setIgnoreChars('\\\\')->getIgnoreChars());
$this->assertSame('{}*?.\\]', $options->setIgnoreChars('{}*?.\\]')->getIgnoreChars());
}

/**
* @dataProvider getInvalidCharacterClasses
*
* @param mixed $ignore
*/
public function testSetIgnoreThrows($ignore)
public function testSetIgnoreCharsThrows($ignore)
{
$options = new SlugOptions;

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessageRegExp('("'.preg_quote($ignore).'")');

$options->setIgnore($ignore);
$options->setIgnoreChars($ignore);
}

/**
Expand Down

0 comments on commit cab998a

Please sign in to comment.