Skip to content

Commit a0fffe9

Browse files
authored
feat(support): add wrap and unwrap to StringHelper (#693)
1 parent 9021c6e commit a0fffe9

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Tempest/Support/src/StringHelper.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,51 @@ public function prepend(string|Stringable ...$prepend): self
487487
return new self(implode('', $prepend) . $this->string);
488488
}
489489

490+
/**
491+
* Wraps the instance with the given string. If `$after` is specified, it will be appended instead of `$before`.
492+
*
493+
* ### Example
494+
* ```php
495+
* str('Scott')->wrap(before: 'Leon ', after: ' Kennedy'); // Leon Scott Kennedy
496+
* ```
497+
*/
498+
public function wrap(string|Stringable $before, string|Stringable $after = null): self
499+
{
500+
return new self($before . $this->string . ($after ??= $before));
501+
}
502+
503+
/**
504+
* Removes the specified `$before` and `$after` from the beginning and the end of the instance. If `$after` is null, `$before` is used instead.
505+
* Setting `$strict` to `false` will unwrap the instance even if both ends do not correspond to the specified `$before` and `$after`.
506+
*
507+
* ### Example
508+
* ```php
509+
* str('Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy', strict: false); // Scott
510+
* ```
511+
*/
512+
public function unwrap(string|Stringable $before, string|Stringable $after = null, bool $strict = true): self
513+
{
514+
$string = $this->string;
515+
516+
if ($string === '') {
517+
return $this;
518+
}
519+
520+
if ($after === null) {
521+
$after = $before;
522+
}
523+
524+
if (! $strict) {
525+
return (new self($string))->after($before)->beforeLast($after);
526+
}
527+
528+
if ($this->startsWith($before) && $this->endsWith($after)) {
529+
$string = (string) (new self($string))->after($before)->beforeLast($after);
530+
}
531+
532+
return new self($string);
533+
}
534+
490535
/**
491536
* Replaces all occurrences of the given `$search` with `$replace`.
492537
*/

src/Tempest/Support/tests/StringHelperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,23 @@ public function test_excerpt(): void
456456

457457
$this->assertSame([2 => 'b', 3 => 'c', 4 => 'd'], $content->excerpt(2, 4, asArray: true)->toArray());
458458
}
459+
460+
public function test_wrap(): void
461+
{
462+
$this->assertSame('Leon Scott Kennedy', str('Scott')->wrap(before: 'Leon ', after: ' Kennedy')->toString());
463+
$this->assertSame('"value"', str('value')->wrap('"')->toString());
464+
}
465+
466+
public function test_unwrap(): void
467+
{
468+
$this->assertSame('Scott', str('Leon Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy')->toString());
469+
$this->assertSame('value', str('"value"')->unwrap('"')->toString());
470+
$this->assertSame('"value"', str('"value"')->unwrap('`')->toString());
471+
$this->assertSame('[value', str('[value')->unwrap('[', ']')->toString());
472+
$this->assertEquals('some: "json"', str('{some: "json"}')->unwrap('{', '}')->toString());
473+
474+
$this->assertSame('value', str('[value')->unwrap('[', ']', strict: false)->toString());
475+
$this->assertSame('value', str('value]')->unwrap('[', ']', strict: false)->toString());
476+
$this->assertSame('Scott', str('Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy', strict: false)->toString());
477+
}
459478
}

0 commit comments

Comments
 (0)