-
Notifications
You must be signed in to change notification settings - Fork 0
/
After.php
68 lines (58 loc) · 1.81 KB
/
After.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace ExeQue\Remix\Mutate\String;
/**
* Retrieves the remainder of a string after a given substring.
*
* @author Morten Harders <mmh@harders-it.dk>
*/
class After extends StringMutator
{
private string $search;
private bool $last;
/**
* @param string $search The substring to search for.
* @param bool $last Whether to search for the last occurrence of the substring (default: false).
*/
public function __construct(string $search, bool $last = false)
{
$this->search = $search;
$this->last = $last;
}
/**
* @param string $search The substring to search for.
* @param bool $last Whether to search for the last occurrence of the substring (default: false).
*/
public static function make(string $search, bool $last = false): self
{
return new self($search, $last);
}
/**
* Retrieves the part of a string after the first occurrence of a given substring.
*
* @param string $search The substring to search for.
*/
public static function first(string $search): self
{
return new self($search, false);
}
/**
* Retrieves the part of a string after the last occurrence of a given substring.
*
* @param string $search The substring to search for.
*/
public static function last(string $search): self
{
return new self($search, true);
}
protected function mutateString(string $value): string
{
if (! str_contains($value, $this->search)) {
return $value;
}
if ($this->last) {
return mb_substr($value, mb_strrpos($value, $this->search) + mb_strlen($this->search));
}
return mb_substr($value, mb_strpos($value, $this->search) + mb_strlen($this->search));
}
}