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\Prediction; 13 14use Prophecy\Call\Call; 15use Prophecy\Prophecy\ObjectProphecy; 16use Prophecy\Prophecy\MethodProphecy; 17use Prophecy\Exception\InvalidArgumentException; 18use Closure; 19 20/** 21 * Callback prediction. 22 * 23 * @author Konstantin Kudryashov <ever.zet@gmail.com> 24 */ 25class CallbackPrediction implements PredictionInterface 26{ 27 private $callback; 28 29 /** 30 * Initializes callback prediction. 31 * 32 * @param callable $callback Custom callback 33 * 34 * @throws \Prophecy\Exception\InvalidArgumentException 35 */ 36 public function __construct($callback) 37 { 38 if (!is_callable($callback)) { 39 throw new InvalidArgumentException(sprintf( 40 'Callable expected as an argument to CallbackPrediction, but got %s.', 41 gettype($callback) 42 )); 43 } 44 45 $this->callback = $callback; 46 } 47 48 /** 49 * Executes preset callback. 50 * 51 * @param Call[] $calls 52 * @param ObjectProphecy $object 53 * @param MethodProphecy $method 54 */ 55 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method) 56 { 57 $callback = $this->callback; 58 59 if ($callback instanceof Closure && method_exists('Closure', 'bind')) { 60 $callback = Closure::bind($callback, $object); 61 } 62 63 call_user_func($callback, $calls, $object, $method); 64 } 65} 66