1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2015 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 provided
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class Required extends BaseValidator
17: {
18: protected $requiredMessage = '%name% is required';
19:
20: /**
21: * Whether the input is required or not
22: *
23: * @var bool
24: */
25: protected $required = true;
26:
27: /**
28: * The invalid variable values
29: *
30: * @var array
31: */
32: protected $invalid = array(null, '', false, array());
33:
34: /**
35: * {@inheritdoc}
36: */
37: public function __invoke($input, $required = null)
38: {
39: is_bool($required) && $this->storeOption('required', $required);
40:
41: return $this->isValid($input);
42: }
43:
44: /**
45: * {@inheritdoc}
46: */
47: protected function doValidate($input)
48: {
49: if ($this->required && $this->isInvalid($input)) {
50: $this->addError('required');
51: return false;
52: }
53: return true;
54: }
55:
56: /**
57: * Check if the input is in invalid variables
58: *
59: * @param mixed $input
60: * @return bool
61: */
62: public function isInvalid($input)
63: {
64: return in_array($input, $this->invalid, true);
65: }
66: }
67: