Skip to content

Commit

Permalink
Adds start and end string replacement helpers (#317)
Browse files Browse the repository at this point in the history
* Adds start and end string replacement helpers

* Fix phpstan

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Aug 24, 2023
1 parent e2347a8 commit 3a07643
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
24 changes: 24 additions & 0 deletions output/Hyperf/Stringable/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ public static function password($length = 32, $letters = true, $numbers = true,
{
}

/**
* Replace the first occurrence of the given value if it appears at the start of the string.
*
* @param string $search
* @param string $replace
* @param string $subject
* @return string
*/
public static function replaceStart($search, $replace, $subject)
{
}

/**
* Replace the last occurrence of a given value if it appears at the end of the string.
*
* @param string $search
* @param string $replace
* @param string $subject
* @return string
*/
public static function replaceEnd($search, $replace, $subject)
{
}

/**
* Reverse the given string.
*
Expand Down
22 changes: 22 additions & 0 deletions output/Hyperf/Stringable/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ public function newLine($count = 1)
{
}

/**
* Replace the first occurrence of the given value if it appears at the start of the string.
*
* @param string $search
* @param string $replace
* @return static
*/
public function replaceStart($search, $replace)
{
}

/**
* Replace the last occurrence of a given value if it appears at the end of the string.
*
* @param string $search
* @param string $replace
* @return static
*/
public function replaceEnd($search, $replace)
{
}

/**
* Reverse the string.
*
Expand Down
34 changes: 34 additions & 0 deletions src/StrMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,40 @@ public function password()
->implode('');
}

public static function replaceStart()
{
return function ($search, $replace, $subject) {
$search = (string) $search;

if ($search === '') {
return $subject;
}

if (static::startsWith($subject, $search)) {
return static::replaceFirst($search, $replace, $subject);
}

return $subject;
};
}

public static function replaceEnd()
{
return function ($search, $replace, $subject) {
$search = (string) $search;

if ($search === '') {
return $subject;
}

if (static::endsWith($subject, $search)) {
return static::replaceLast($search, $replace, $subject);
}

return $subject;
};
}

public function reverse()
{
return fn ($value) => implode(array_reverse(mb_str_split($value)));
Expand Down
12 changes: 12 additions & 0 deletions src/StringableMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public function newLine()
return fn ($count = 1) => $this->append(str_repeat(PHP_EOL, $count));
}

public function replaceStart()
{
/* @phpstan-ignore-next-line */
return fn ($search, $replace) => new static(Str::replaceStart($search, $replace, $this->value));
}

public function replaceEnd()
{
/* @phpstan-ignore-next-line */
return fn ($search, $replace) => new static(Str::replaceEnd($search, $replace, $this->value));
}

public function reverse()
{
/* @phpstan-ignore-next-line */
Expand Down

0 comments on commit 3a07643

Please sign in to comment.