Skip to content

Commit

Permalink
Adds Str::take() and Stringable->take() (#344)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Sep 20, 2023
1 parent e62db9c commit d619291
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions output/Hyperf/Stringable/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ public static function swap(array $map, $subject)
{
}

/**
* Take the first or last {$limit} characters of a string.
*
* @param string $string
*/
public static function take($string, int $limit): string
{
}

/**
* Transliterate a string to its closest ASCII representation.
*
Expand Down
9 changes: 9 additions & 0 deletions output/Hyperf/Stringable/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ public function swap(array $map)
{
}

/**
* Take the first or last {$limit} characters.
*
* @return static
*/
public function take(int $limit)
{
}

/**
* Determine if the string matches the given pattern.
*
Expand Down
11 changes: 11 additions & 0 deletions src/StrMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ public function swap()
return fn (array $map, $subject) => str_replace(array_keys($map), array_values($map), $subject);
}

public static function take()
{
return function ($string, int $limit) {
if ($limit < 0) {
return static::substr($string, $limit);
}

return static::substr($string, 0, $limit);
};
}

public function transliterate()
{
return fn ($string, $unknown = '?', $strict = false) => ASCII::to_transliterate($string, $unknown, $strict);
Expand Down
11 changes: 11 additions & 0 deletions src/StringableMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ public function swap()
return fn (array $map) => new static(strtr($this->value, $map));
}

/**
* Take the first or last {$limit} characters.
*
* @return static
*/
public function take()
{
/* @phpstan-ignore-next-line */
return fn (int $limit) => new static(Str::take($this->value, $limit));
}

public function test()
{
return fn ($pattern) => $this->match($pattern)->isNotEmpty();
Expand Down

0 comments on commit d619291

Please sign in to comment.