summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/validator/src/InArrayValidator.php
blob: f8c18ef2fcc51cee94df850e48a015c68a4e2915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace ipl\Validator;

use ipl\I18n\Translation;

/**
 * Validate if specific single or multiple values exist in an array
 */
class InArrayValidator extends BaseValidator
{
    use Translation;

    /** @var array The array */
    protected $haystack;

    /** @var bool Whether the types of the needle in the haystack should also match */
    protected $strict = false;

    /**
     * Create a new InArray validator
     *
     * **Optional options:**
     *
     * * `haystack`: (`array`) The array
     * * `strict`: (`bool`) Whether the types of the needle in the haystack should also match, default `false`
     *
     * @param array $options
     */
    public function __construct(array $options = [])
    {
        if (isset($options['haystack'])) {
            $this->setHaystack($options['haystack']);
        }

        $this->setStrict($options['strict'] ?? false);
    }

    /**
     * Get the haystack
     *
     * @return array
     */
    public function getHaystack(): array
    {
        return $this->haystack ?? [];
    }

    /**
     * Set the haystack
     *
     * @param array $haystack
     *
     * @return $this
     */
    public function setHaystack(array $haystack): self
    {
        $this->haystack = $haystack;

        return $this;
    }

    /**
     * Get whether the types of the needle in the haystack should also match
     *
     * @return bool
     */
    public function isStrict(): bool
    {
        return $this->strict;
    }

    /**
     * Set whether the types of the needle in the haystack should also match
     *
     * @param bool $strict
     *
     * @return $this
     */
    public function setStrict(bool $strict = true): self
    {
        $this->strict = $strict;

        return $this;
    }

    public function isValid($value)
    {
        // Multiple isValid() calls must not stack validation messages
        $this->clearMessages();

        $notInArray = $this->findInvalid((array) $value);

        if (empty($notInArray)) {
            return true;
        }

        $this->addMessage(sprintf(
            $this->translatePlural(
                "%s was not found in the haystack",
                "%s were not found in the haystack",
                count($notInArray)
            ),
            implode(', ', $notInArray)
        ));

        return false;
    }

    /**
     * Get the values from the specified array that are not present in the haystack
     *
     * @param array $values
     *
     * @return array Values not found in the haystack
     */
    protected function findInvalid(array $values = []): array
    {
        $notInArray = [];
        foreach ($values as $val) {
            if (! in_array($val, $this->getHaystack(), $this->isStrict())) {
                $notInArray[] = $val;
            }
        }

        return $notInArray;
    }
}