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\Argument\Token;
14 use SebastianBergmann\Comparator\ComparisonFailure;
15 use Prophecy\Comparator\Factory as ComparatorFactory;
16 use Prophecy\Util\StringUtil;
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class ExactValueToken implements TokenInterface
28 private $comparatorFactory;
34 * @param StringUtil $util
35 * @param ComparatorFactory $comparatorFactory
37 public function __construct($value, StringUtil $util = null, ComparatorFactory $comparatorFactory = null)
39 $this->value = $value;
40 $this->util = $util ?: new StringUtil();
42 $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
46 * Scores 10 if argument matches preset value.
52 public function scoreArgument($argument)
54 if (is_object($argument) && is_object($this->value)) {
55 $comparator = $this->comparatorFactory->getComparatorFor(
56 $argument, $this->value
60 $comparator->assertEquals($argument, $this->value);
62 } catch (ComparisonFailure $failure) {}
65 // If either one is an object it should be castable to a string
66 if (is_object($argument) xor is_object($this->value)) {
67 if (is_object($argument) && !method_exists($argument, '__toString')) {
71 if (is_object($this->value) && !method_exists($this->value, '__toString')) {
74 } elseif (is_numeric($argument) && is_numeric($this->value)) {
76 } elseif (gettype($argument) !== gettype($this->value)) {
80 return $argument == $this->value ? 10 : false;
84 * Returns preset value against which token checks arguments.
88 public function getValue()
98 public function isLast()
104 * Returns string representation for token.
108 public function __toString()
110 if (null === $this->string) {
111 $this->string = sprintf('exact(%s)', $this->util->stringify($this->value));
114 return $this->string;