Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Prophecy.
|
Chris@0
|
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
6 * Marcello Duarte <marcello.duarte@gmail.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Prophecy\Argument\Token;
|
Chris@0
|
13
|
Chris@0
|
14 use SebastianBergmann\Comparator\ComparisonFailure;
|
Chris@0
|
15 use Prophecy\Comparator\Factory as ComparatorFactory;
|
Chris@0
|
16 use Prophecy\Util\StringUtil;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Exact value token.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class ExactValueToken implements TokenInterface
|
Chris@0
|
24 {
|
Chris@0
|
25 private $value;
|
Chris@0
|
26 private $string;
|
Chris@0
|
27 private $util;
|
Chris@0
|
28 private $comparatorFactory;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Initializes token.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param mixed $value
|
Chris@0
|
34 * @param StringUtil $util
|
Chris@0
|
35 * @param ComparatorFactory $comparatorFactory
|
Chris@0
|
36 */
|
Chris@0
|
37 public function __construct($value, StringUtil $util = null, ComparatorFactory $comparatorFactory = null)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->value = $value;
|
Chris@0
|
40 $this->util = $util ?: new StringUtil();
|
Chris@0
|
41
|
Chris@0
|
42 $this->comparatorFactory = $comparatorFactory ?: ComparatorFactory::getInstance();
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Scores 10 if argument matches preset value.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @param $argument
|
Chris@0
|
49 *
|
Chris@0
|
50 * @return bool|int
|
Chris@0
|
51 */
|
Chris@0
|
52 public function scoreArgument($argument)
|
Chris@0
|
53 {
|
Chris@0
|
54 if (is_object($argument) && is_object($this->value)) {
|
Chris@0
|
55 $comparator = $this->comparatorFactory->getComparatorFor(
|
Chris@0
|
56 $argument, $this->value
|
Chris@0
|
57 );
|
Chris@0
|
58
|
Chris@0
|
59 try {
|
Chris@0
|
60 $comparator->assertEquals($argument, $this->value);
|
Chris@0
|
61 return 10;
|
Chris@0
|
62 } catch (ComparisonFailure $failure) {}
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 // If either one is an object it should be castable to a string
|
Chris@0
|
66 if (is_object($argument) xor is_object($this->value)) {
|
Chris@0
|
67 if (is_object($argument) && !method_exists($argument, '__toString')) {
|
Chris@0
|
68 return false;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 if (is_object($this->value) && !method_exists($this->value, '__toString')) {
|
Chris@0
|
72 return false;
|
Chris@0
|
73 }
|
Chris@0
|
74 } elseif (is_numeric($argument) && is_numeric($this->value)) {
|
Chris@0
|
75 // noop
|
Chris@0
|
76 } elseif (gettype($argument) !== gettype($this->value)) {
|
Chris@0
|
77 return false;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 return $argument == $this->value ? 10 : false;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Returns preset value against which token checks arguments.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return mixed
|
Chris@0
|
87 */
|
Chris@0
|
88 public function getValue()
|
Chris@0
|
89 {
|
Chris@0
|
90 return $this->value;
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * Returns false.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return bool
|
Chris@0
|
97 */
|
Chris@0
|
98 public function isLast()
|
Chris@0
|
99 {
|
Chris@0
|
100 return false;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Returns string representation for token.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return string
|
Chris@0
|
107 */
|
Chris@0
|
108 public function __toString()
|
Chris@0
|
109 {
|
Chris@0
|
110 if (null === $this->string) {
|
Chris@0
|
111 $this->string = sprintf('exact(%s)', $this->util->stringify($this->value));
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 return $this->string;
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|