4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Promise;
14 use Prophecy\Exception\InvalidArgumentException;
15 use Prophecy\Prophecy\ObjectProphecy;
16 use Prophecy\Prophecy\MethodProphecy;
19 * Return argument promise.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class ReturnArgumentPromise implements PromiseInterface
31 * Initializes callback promise.
33 * @param int $index The zero-indexed number of the argument to return
35 * @throws \Prophecy\Exception\InvalidArgumentException
37 public function __construct($index = 0)
39 if (!is_int($index) || $index < 0) {
40 throw new InvalidArgumentException(sprintf(
41 'Zero-based index expected as argument to ReturnArgumentPromise, but got %s.',
45 $this->index = $index;
49 * Returns nth argument if has one, null otherwise.
52 * @param ObjectProphecy $object
53 * @param MethodProphecy $method
57 public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
59 return count($args) > $this->index ? $args[$this->index] : null;