1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2013 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei\Validator;
10:
11: /**
12: * Check if the input is equals to (==) the specified value
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class EqualTo extends BaseValidator
17: {
18: protected $invalidMessage = '%name% must be equals %value%';
19:
20: protected $negativeMessage = '%name% must not be equals %value%';
21:
22: /**
23: * The value to be compared
24: *
25: * @var mixed
26: */
27: protected $value;
28:
29: /**
30: * {@inheritdoc}
31: */
32: public function __invoke($input, $value = null)
33: {
34: // Sets $this->equals only when the second argument provided
35: func_num_args() > 1 && $this->storeOption('value', $value);
36:
37: return $this->isValid($input);
38: }
39:
40: /**
41: * {@inheritdoc}
42: */
43: protected function doValidate($input)
44: {
45: if (!$this->doCompare($input)) {
46: $this->addError('invalid');
47: return false;
48: }
49: return true;
50: }
51:
52: /**
53: * Compare input and option value
54: *
55: * @param mixed $input
56: * @return bool
57: */
58: protected function doCompare($input)
59: {
60: return $input == $this->value;
61: }
62: }
63: