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 */
20class PHPUnit_Framework_Constraint_PCREMatch extends PHPUnit_Framework_Constraint
21{
22    /**
23     * @var string
24     */
25    protected $pattern;
26
27    /**
28     * @param string $pattern
29     */
30    public function __construct($pattern)
31    {
32        parent::__construct();
33        $this->pattern = $pattern;
34    }
35
36    /**
37     * Evaluates the constraint for parameter $other. Returns true if the
38     * constraint is met, false otherwise.
39     *
40     * @param mixed $other Value or object to evaluate.
41     *
42     * @return bool
43     */
44    protected function matches($other)
45    {
46        return preg_match($this->pattern, $other) > 0;
47    }
48
49    /**
50     * Returns a string representation of the constraint.
51     *
52     * @return string
53     */
54    public function toString()
55    {
56        return sprintf(
57            'matches PCRE pattern "%s"',
58            $this->pattern
59        );
60    }
61}
62