blob: 6279f37ba4fb1b8bd333b703b3dd0da826d886ec (
plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework\Constraint;
use function mb_stripos;
use function mb_strtolower;
use function sprintf;
use function strpos;
/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final class StringContains extends Constraint
{
/**
* @var string
*/
private $string;
/**
* @var bool
*/
private $ignoreCase;
public function __construct(string $string, bool $ignoreCase = false)
{
$this->string = $string;
$this->ignoreCase = $ignoreCase;
}
/**
* Returns a string representation of the constraint.
*/
public function toString(): string
{
if ($this->ignoreCase) {
$string = mb_strtolower($this->string, 'UTF-8');
} else {
$string = $this->string;
}
return sprintf(
'contains "%s"',
$string
);
}
/**
* Evaluates the constraint for parameter $other. Returns true if the
* constraint is met, false otherwise.
*
* @param mixed $other value or object to evaluate
*/
protected function matches($other): bool
{
if ('' === $this->string) {
return true;
}
if ($this->ignoreCase) {
/*
* We must use the multi byte safe version so we can accurately compare non latin upper characters with
* their lowercase equivalents.
*/
return mb_stripos($other, $this->string, 0, 'UTF-8') !== false;
}
/*
* Use the non multi byte safe functions to see if the string is contained in $other.
*
* This function is very fast and we don't care about the character position in the string.
*
* Additionally, we want this method to be binary safe so we can check if some binary data is in other binary
* data.
*/
return strpos($other, $this->string) !== false;
}
}
|