Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework\Constraint;
11
 
12
use function mb_stripos;
13
use function mb_strtolower;
14
use function sprintf;
15
use function strpos;
16
 
17
/**
18
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
19
 */
20
final class StringContains extends Constraint
21
{
22
    /**
23
     * @var string
24
     */
25
    private $string;
26
 
27
    /**
28
     * @var bool
29
     */
30
    private $ignoreCase;
31
 
32
    public function __construct(string $string, bool $ignoreCase = false)
33
    {
34
        $this->string     = $string;
35
        $this->ignoreCase = $ignoreCase;
36
    }
37
 
38
    /**
39
     * Returns a string representation of the constraint.
40
     */
41
    public function toString(): string
42
    {
43
        if ($this->ignoreCase) {
44
            $string = mb_strtolower($this->string, 'UTF-8');
45
        } else {
46
            $string = $this->string;
47
        }
48
 
49
        return sprintf(
50
            'contains "%s"',
51
            $string
52
        );
53
    }
54
 
55
    /**
56
     * Evaluates the constraint for parameter $other. Returns true if the
57
     * constraint is met, false otherwise.
58
     *
59
     * @param mixed $other value or object to evaluate
60
     */
61
    protected function matches($other): bool
62
    {
63
        if ('' === $this->string) {
64
            return true;
65
        }
66
 
67
        if ($this->ignoreCase) {
68
            /*
69
             * We must use the multi byte safe version so we can accurately compare non latin upper characters with
70
             * their lowercase equivalents.
71
             */
72
            return mb_stripos($other, $this->string, 0, 'UTF-8') !== false;
73
        }
74
 
75
        /*
76
         * Use the non multi byte safe functions to see if the string is contained in $other.
77
         *
78
         * This function is very fast and we don't care about the character position in the string.
79
         *
80
         * Additionally, we want this method to be binary safe so we can check if some binary data is in other binary
81
         * data.
82
         */
83
        return strpos($other, $this->string) !== false;
84
    }
85
}