Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / PCREMatch.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 matches
13  * a regular expression.
14  *
15  * Checks a given value using the Perl Compatible Regular Expression extension
16  * in PHP. The pattern is matched by executing preg_match().
17  *
18  * The pattern string passed in the constructor.
19  *
20  * @since Class available since Release 3.0.0
21  */
22 class PHPUnit_Framework_Constraint_PCREMatch extends PHPUnit_Framework_Constraint
23 {
24     /**
25      * @var string
26      */
27     protected $pattern;
28
29     /**
30      * @param string $pattern
31      */
32     public function __construct($pattern)
33     {
34         parent::__construct();
35         $this->pattern = $pattern;
36     }
37
38     /**
39      * Evaluates the constraint for parameter $other. Returns true if the
40      * constraint is met, false otherwise.
41      *
42      * @param mixed $other Value or object to evaluate.
43      *
44      * @return bool
45      */
46     protected function matches($other)
47     {
48         return preg_match($this->pattern, $other) > 0;
49     }
50
51     /**
52      * Returns a string representation of the constraint.
53      *
54      * @return string
55      */
56     public function toString()
57     {
58         return sprintf(
59             'matches PCRE pattern "%s"',
60             $this->pattern
61         );
62     }
63 }