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 evaluates against a specified closure.
13 */
14class PHPUnit_Framework_Constraint_Callback extends PHPUnit_Framework_Constraint
15{
16    private $callback;
17
18    /**
19     * @param callable $callback
20     *
21     * @throws PHPUnit_Framework_Exception
22     */
23    public function __construct($callback)
24    {
25        if (!is_callable($callback)) {
26            throw PHPUnit_Util_InvalidArgumentHelper::factory(
27                1,
28                'callable'
29            );
30        }
31
32        parent::__construct();
33
34        $this->callback = $callback;
35    }
36
37    /**
38     * Evaluates the constraint for parameter $value. Returns true if the
39     * constraint is met, false otherwise.
40     *
41     * @param mixed $other Value or object to evaluate.
42     *
43     * @return bool
44     */
45    protected function matches($other)
46    {
47        return call_user_func($this->callback, $other);
48    }
49
50    /**
51     * Returns a string representation of the constraint.
52     *
53     * @return string
54     */
55    public function toString()
56    {
57        return 'is accepted by specified callback';
58    }
59}
60