-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringEndsWith.php
47 lines (38 loc) · 981 Bytes
/
StringEndsWith.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
<?php
declare(strict_types=1);
namespace PHPUnitExtraConstraints\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use function is_string;
use function strlen;
use function substr_compare;
/**
* Constraint that asserts that a string ends with another string.
*/
final class StringEndsWith extends Constraint
{
/** @var string */
private $needle;
public function __construct(string $needle)
{
$this->needle = $needle;
}
/**
* @inheritDoc
*/
protected function matches($other): bool
{
return is_string($other)
&& self::endsWith($other, $this->needle);
}
private static function endsWith(string $haystack, string $needle): bool
{
return '' === $needle || ('' !== $haystack && 0 === substr_compare($haystack, $needle, -strlen($needle)));
}
/**
* @inheritDoc
*/
public function toString(): string
{
return 'ends with ' . $this->needle;
}
}