1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei\Validator;
10:
11: 12: 13: 14: 15:
16: class In extends BaseValidator
17: {
18: protected $notInMessage = '%name% must be in %array%';
19:
20: protected $negativeMessage = '%name% must not be in %array%';
21:
22: protected $strict = false;
23:
24: protected $array = array();
25:
26: 27: 28:
29: public function __invoke($input, $array = array(), $strict = null)
30: {
31: if ($array) {
32: if ($array instanceof \ArrayObject) {
33: $array = $array->getArrayCopy();
34: } elseif (!is_array($array)) {
35: throw new \InvalidArgumentException(sprintf(
36: 'Expected argument of type array or \ArrayObject, "%s" given',
37: is_object($array) ? get_class($array) : gettype($array)
38: ));
39: }
40: $this->storeOption('array', $array);
41: }
42:
43: is_bool($strict) && $this->storeOption('strict', $strict);
44:
45: return $this->isValid($input);
46: }
47:
48: 49: 50:
51: protected function doValidate($input)
52: {
53: if (!in_array($input, $this->array, $this->strict)) {
54: $this->addError('notIn');
55: return false;
56: }
57:
58: return true;
59: }
60: }
61: