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 begins with a
13 * given prefix.
14 */
15class PHPUnit_Framework_Constraint_StringStartsWith extends PHPUnit_Framework_Constraint
16{
17    /**
18     * @var string
19     */
20    protected $prefix;
21
22    /**
23     * @param string $prefix
24     */
25    public function __construct($prefix)
26    {
27        parent::__construct();
28        $this->prefix = $prefix;
29    }
30
31    /**
32     * Evaluates the constraint for parameter $other. Returns true if the
33     * constraint is met, false otherwise.
34     *
35     * @param mixed $other Value or object to evaluate.
36     *
37     * @return bool
38     */
39    protected function matches($other)
40    {
41        return strpos($other, $this->prefix) === 0;
42    }
43
44    /**
45     * Returns a string representation of the constraint.
46     *
47     * @return string
48     */
49    public function toString()
50    {
51        return 'starts with "' . $this->prefix . '"';
52    }
53}
54