1<?php
2
3/*
4 * This file is part of Respect/Validation.
5 *
6 * (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
7 *
8 * For the full copyright and license information, please view the "LICENSE.md"
9 * file that was distributed with this source code.
10 */
11
12namespace Respect\Validation\Rules;
13
14class Contains extends AbstractRule
15{
16    public $containsValue;
17    public $identical;
18
19    public function __construct($containsValue, $identical = false)
20    {
21        $this->containsValue = $containsValue;
22        $this->identical = $identical;
23    }
24
25    public function validate($input)
26    {
27        if ($this->identical) {
28            return $this->validateIdentical($input);
29        }
30
31        return $this->validateEquals($input);
32    }
33
34    protected function validateEquals($input)
35    {
36        if (is_array($input)) {
37            return in_array($this->containsValue, $input);
38        }
39
40        return false !== mb_stripos($input, $this->containsValue, 0, mb_detect_encoding($input));
41    }
42
43    protected function validateIdentical($input)
44    {
45        if (is_array($input)) {
46            return in_array($this->containsValue, $input, true);
47        }
48
49        return false !== mb_strpos($input, $this->containsValue, 0, mb_detect_encoding($input));
50    }
51}
52