1<?php 2 3/* 4 * This file is part of the Prophecy. 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com> 6 * Marcello Duarte <marcello.duarte@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Prophecy\Promise; 13 14use Prophecy\Exception\InvalidArgumentException; 15use Prophecy\Prophecy\ObjectProphecy; 16use Prophecy\Prophecy\MethodProphecy; 17 18/** 19 * Return argument promise. 20 * 21 * @author Konstantin Kudryashov <ever.zet@gmail.com> 22 */ 23class ReturnArgumentPromise implements PromiseInterface 24{ 25 /** 26 * @var int 27 */ 28 private $index; 29 30 /** 31 * Initializes callback promise. 32 * 33 * @param int $index The zero-indexed number of the argument to return 34 * 35 * @throws \Prophecy\Exception\InvalidArgumentException 36 */ 37 public function __construct($index = 0) 38 { 39 if (!is_int($index) || $index < 0) { 40 throw new InvalidArgumentException(sprintf( 41 'Zero-based index expected as argument to ReturnArgumentPromise, but got %s.', 42 $index 43 )); 44 } 45 $this->index = $index; 46 } 47 48 /** 49 * Returns nth argument if has one, null otherwise. 50 * 51 * @param array $args 52 * @param ObjectProphecy $object 53 * @param MethodProphecy $method 54 * 55 * @return null|mixed 56 */ 57 public function execute(array $args, ObjectProphecy $object, MethodProphecy $method) 58 { 59 return count($args) > $this->index ? $args[$this->index] : null; 60 } 61} 62