1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei\Validator;
10:
11: 12: 13: 14: 15:
16: class Contains extends BaseValidator
17: {
18: protected $notContainsMessage = '%name% must contains %search%';
19:
20: protected $negativeMessage = '%name% must not contains %search%';
21:
22: protected $search;
23:
24: protected $regex = false;
25:
26: 27: 28: 29: 30: 31: 32: 33:
34: public function __invoke($input, $search = null, $regex = false)
35: {
36: $search && $this->storeOption('search', (string)$search);
37: $this->storeOption('regex', $regex);
38:
39: return $this->isValid($input);
40: }
41:
42: 43: 44:
45: protected function doValidate($input)
46: {
47: if (!$this->isString($input)) {
48: $this->addError('notString');
49: return false;
50: }
51:
52: if (!$this->regex) {
53: if (strpos($input, $this->search) === false) {
54: $this->addError('notContains');
55: return false;
56: }
57: } else {
58: if (!preg_match($this->search, $input)) {
59: $this->addError('notContains');
60: return false;
61: }
62: }
63:
64: return true;
65: }
66: }
67: