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\Argument\Token; 13 14use Prophecy\Exception\InvalidArgumentException; 15 16/** 17 * Callback-verified token. 18 * 19 * @author Konstantin Kudryashov <ever.zet@gmail.com> 20 */ 21class CallbackToken implements TokenInterface 22{ 23 private $callback; 24 25 /** 26 * Initializes token. 27 * 28 * @param callable $callback 29 * 30 * @throws \Prophecy\Exception\InvalidArgumentException 31 */ 32 public function __construct($callback) 33 { 34 if (!is_callable($callback)) { 35 throw new InvalidArgumentException(sprintf( 36 'Callable expected as an argument to CallbackToken, but got %s.', 37 gettype($callback) 38 )); 39 } 40 41 $this->callback = $callback; 42 } 43 44 /** 45 * Scores 7 if callback returns true, false otherwise. 46 * 47 * @param $argument 48 * 49 * @return bool|int 50 */ 51 public function scoreArgument($argument) 52 { 53 return call_user_func($this->callback, $argument) ? 7 : false; 54 } 55 56 /** 57 * Returns false. 58 * 59 * @return bool 60 */ 61 public function isLast() 62 { 63 return false; 64 } 65 66 /** 67 * Returns string representation for token. 68 * 69 * @return string 70 */ 71 public function __toString() 72 { 73 return 'callback()'; 74 } 75} 76