ee02bf4e0d4167893cd1e4e526886e3b5f75e054
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / StringContains.php
1 <?php
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
11 /**
12  * Constraint that asserts that the string it is evaluated for contains
13  * a given string.
14  *
15  * Uses strpos() to find the position of the string in the input, if not found
16  * the evaluation fails.
17  *
18  * The sub-string is passed in the constructor.
19  *
20  * @since Class available since Release 3.0.0
21  */
22 class PHPUnit_Framework_Constraint_StringContains extends PHPUnit_Framework_Constraint
23 {
24     /**
25      * @var string
26      */
27     protected $string;
28
29     /**
30      * @var bool
31      */
32     protected $ignoreCase;
33
34     /**
35      * @param string $string
36      * @param bool   $ignoreCase
37      */
38     public function __construct($string, $ignoreCase = false)
39     {
40         parent::__construct();
41
42         $this->string     = $string;
43         $this->ignoreCase = $ignoreCase;
44     }
45
46     /**
47      * Evaluates the constraint for parameter $other. Returns true if the
48      * constraint is met, false otherwise.
49      *
50      * @param mixed $other Value or object to evaluate.
51      *
52      * @return bool
53      */
54     protected function matches($other)
55     {
56         if ($this->ignoreCase) {
57             return stripos($other, $this->string) !== false;
58         } else {
59             return strpos($other, $this->string) !== false;
60         }
61     }
62
63     /**
64      * Returns a string representation of the constraint.
65      *
66      * @return string
67      */
68     public function toString()
69     {
70         if ($this->ignoreCase) {
71             $string = strtolower($this->string);
72         } else {
73             $string = $this->string;
74         }
75
76         return sprintf(
77             'contains "%s"',
78             $string
79         );
80     }
81 }