Skip to content

Commit

Permalink
Merge pull request #11 from vendeka-nl/deprecate-unwrap-method
Browse files Browse the repository at this point in the history
Deprecate unwrap method.
  • Loading branch information
royvanv authored Feb 16, 2024
2 parents da3c1b6 + ff63e10 commit acc12c1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
26 changes: 23 additions & 3 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ public static function toWords(mixed $text): Words
return new Words($text);
}

/**
* Unclose (unwrap) a text with a prefix and a (different) suffix. If the suffix is `null` the prefix is also used as the suffix.
*
* @param string $text
* @param string|iterable $before
* @param string|iterable $after
* @return string
*/
public static function unclose(string $text, string|iterable $before, string|iterable $after = null): string
{
return self::unsuffix(self::unprefix($text, $before), $after ?? $before);
}

/**
* Remove a prefix if it is present.
*
Expand Down Expand Up @@ -259,22 +272,27 @@ public static function unsuffix(string $text, string|iterable $cap): string
}

/**
* Unwrap a text with a prefix and a (different) suffix. If the suffix is empty the prefix is also used as the suffix.
* Unwrap a text with a prefix and a (different) suffix. If the suffix is `null` the prefix is also used as the suffix.
*
* @deprecated 3.1.1 No longer to be used in Laravel v10.42 or above, because `Illuminate\Support\Str::unwrap()` overrides this method. Use the `unclose()` method instead.
* @see Vendeka\Text\Text::unclose()
*
* @param string $text
* @param string|iterable $before
* @param string|iterable $after
* @return string
*
* @codeCoverageIgnore
*/
public static function unwrap(string $text, string|iterable $before, string|iterable $after = null): string
{
return self::unsuffix(self::unprefix($text, $before), $after ?? $before);
return self::unclose($text, $before, $after);
}

/**
* Wrap a text with a prefix and a (different) suffix. If the suffix is empty the prefix is also used as the suffix.
*
* @deprecated 3.0.2 No longer to be used in Laravel v9.31 or above, because `Str::wrap()` overrides this method. Use `Str::enclose()` instead.
* @deprecated 3.0.2 No longer to be used in Laravel v9.31 or above, because `Illuminate\Support\Str::wrap()` overrides this method. Use the `enclose()` method instead.
* @see Vendeka\Text\Text::enclose()
*
* @param string|iterable $before
Expand Down Expand Up @@ -308,6 +326,7 @@ private static function bootStrMacros(): void
Str::macro('sentence', fn (string $text, string $cap = '.'): string => Text::sentence($text, $cap));
Str::macro('toParagraphs', fn (string $text): Paragraphs => Text::toParagraphs($text));
Str::macro('toWords', fn (mixed $text): Words => Text::toWords($text));
Str::macro('unclose', fn (string $text, $before, $after = null): string => Text::unclose($text, $before, $after));
Str::macro('unprefix', fn (string $text, $lead): string => Text::unprefix($text, $lead));
Str::macro('unsuffix', fn (string $text, $cap): string => Text::unsuffix($text, $cap));
Str::macro('unwrap', fn (string $text, $before, $after = null): string => Text::unwrap($text, $before, $after));
Expand All @@ -331,6 +350,7 @@ private static function bootStringableMacros(): void
Stringable::macro('sentence', fn (string $cap = '.'): Stringable => new Stringable(Text::sentence($this->value, $cap)));
Stringable::macro('toParagraphs', fn (): Paragraphs => Text::toParagraphs($this->value));
Stringable::macro('toWords', fn (): Words => Text::toWords($this->value));
Stringable::macro('unclose', fn ($before, $after = null): Stringable => new Stringable(Text::unclose($this->value, $before, $after)));
Stringable::macro('unprefix', fn ($lead): Stringable => new Stringable(Text::unprefix($this->value, $lead)));
Stringable::macro('unsuffix', fn ($cap): Stringable => new Stringable(Text::unsuffix($this->value, $cap)));
Stringable::macro('unwrap', fn ($before, $after = null): Stringable => new Stringable(Text::unwrap($this->value, $before, $after)));
Expand Down
46 changes: 23 additions & 23 deletions tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ public function testToParagraphsMethod(): void
$this->assertInstanceOf(Paragraphs::class, Str::of('Paragraph?')->toParagraphs());
}

public function testUncloseMethod(): void
{
$this->assertIsString(Str::unclose('Instance', '/'));
$this->assertInstanceOf(Stringable::class, Str::of('Instance')->unclose('/'));

// String
$this->assertEquals('path', Str::unclose('path', '/'));
$this->assertEquals('path', Str::unclose('path/', '/'));
$this->assertEquals('path', Str::unclose('/path', '/'));
$this->assertEquals('path', Str::unclose('/path/', '/'));
$this->assertEquals('path', Str::unclose('path', '/x/', '/y/'));
$this->assertEquals('path', Str::unclose('/x/path', '/x/', '/y/'));
$this->assertEquals('path', Str::unclose('path/y/', '/x/', '/y/'));
$this->assertEquals('path', Str::unclose('/x/path/y/', '/x/', '/y/'));

// Array
$this->assertEquals('path', Str::unclose('/path/', ['/']));
$this->assertEquals('path', Str::unclose('/path/', ['/', '\\']));
$this->assertEquals('path', Str::unclose('x/path/x', ['/', 'x', '/']));
$this->assertEquals('path', Str::unclose('x/path/x', ['x', '/', 'q', 'x']));
$this->assertEquals('path', Str::unclose('/x/path/y/', ['/x/', '/a/'], ['/y/', '/z/']));
}

public function testUnprefixMethod(): void
{
$this->assertIsString(Str::unprefix('Instance', 'In'));
Expand Down Expand Up @@ -152,27 +175,4 @@ public function testUnsufffixMethod(): void
$this->assertEquals('path', Str::unsuffix('path/secret/', ['/', '/secret']));
$this->assertEquals('path', Str::unsuffix('path/secret/', ['/', '/top-secret', '/secret']));
}

public function testUnwrapMethod(): void
{
$this->assertIsString(Str::unwrap('Instance', '/'));
$this->assertInstanceOf(Stringable::class, Str::of('Instance')->unwrap('/'));

// String
$this->assertEquals('path', Str::unwrap('path', '/'));
$this->assertEquals('path', Str::unwrap('path/', '/'));
$this->assertEquals('path', Str::unwrap('/path', '/'));
$this->assertEquals('path', Str::unwrap('/path/', '/'));
$this->assertEquals('path', Str::unwrap('path', '/x/', '/y/'));
$this->assertEquals('path', Str::unwrap('/x/path', '/x/', '/y/'));
$this->assertEquals('path', Str::unwrap('path/y/', '/x/', '/y/'));
$this->assertEquals('path', Str::unwrap('/x/path/y/', '/x/', '/y/'));

// Array
$this->assertEquals('path', Str::unwrap('/path/', ['/']));
$this->assertEquals('path', Str::unwrap('/path/', ['/', '\\']));
$this->assertEquals('path', Str::unwrap('x/path/x', ['/', 'x', '/']));
$this->assertEquals('path', Str::unwrap('x/path/x', ['x', '/', 'q', 'x']));
$this->assertEquals('path', Str::unwrap('/x/path/y/', ['/x/', '/a/'], ['/y/', '/z/']));
}
}

0 comments on commit acc12c1

Please sign in to comment.