ValidatedInput::except()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation;
13
14
use ArrayIterator;
15
use BlitzPHP\Utilities\Iterable\Arr;
16
use BlitzPHP\Utilities\Iterable\Collection;
17
use Dimtrovich\Validation\Contracts\ValidatedData;
18
use stdClass;
19
use Traversable;
20
21
class ValidatedInput implements ValidatedData
22
{
23
    /**
24
     * Create a new validated input container.
25
     *
26
     * @param array $input The underlying input.
27
     */
28
    public function __construct(protected array $input)
29
    {
30
    }
31
32
    /**
33
     * Determine if the validated input has one or more keys.
34
     */
35
    public function has(mixed $keys): bool
36
    {
37
        $keys = is_array($keys) ? $keys : func_get_args();
38
39
        foreach ($keys as $key) {
40
            if (! Arr::has($this->input, $key)) {
41
                return false;
42
            }
43
        }
44
45
        return true;
46
    }
47
48
    /**
49
     * Determine if the validated input is missing one or more keys.
50
     */
51
    public function missing(mixed $keys): bool
52
    {
53
        $keys = is_array($keys) ? $keys : func_get_args();
54
55
        return ! $this->has($keys);
56
    }
57
58
    /**
59
     * Get a subset containing the provided keys with values from the input data.
60
     */
61
    public function only(mixed $keys): array
62
    {
63
        $results = [];
64
65
        $input = $this->input;
66
67
        $placeholder = new stdClass();
68
69
        foreach (is_array($keys) ? $keys : func_get_args() as $key) {
70
            $value = Arr::dataGet($input, $key, $placeholder);
71
72
            if ($value !== $placeholder) {
73
                Arr::set($results, $key, $value);
74
            }
75
        }
76
77
        return $results;
78
    }
79
80
    /**
81
     * Get all of the input except for a specified array of items.
82
     */
83
    public function except(mixed $keys): array
84
    {
85
        $keys = is_array($keys) ? $keys : func_get_args();
86
87
        $results = $this->input;
88
89
        Arr::forget($results, $keys);
90
91
        return $results;
92
    }
93
94
    /**
95
     * Merge the validated input with the given array of additional data.
96
     */
97
    public function merge(array $items)
98
    {
99
        return new self(array_merge($this->input, $items));
100
    }
101
102
    /**
103
     * Get the input as a collection.
104
     */
105
    public function collect(): Collection
106
    {
107
        return new Collection($this->input);
108
    }
109
110
    /**
111
     * Get the raw, underlying input array.
112
     */
113
    public function all(): array
114
    {
115
        return $this->input;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function toArray(): array
122
    {
123
        return $this->all();
124
    }
125
126
    /**
127
     * Recuperation dynamique des donnees.
128
     */
129
    public function __get(string $name): mixed
130
    {
131
        return $this->input[$name] ?? null;
132
    }
133
134
    /**
135
     * Definition dynamique des donnnees
136
     *
137
     * @return mixed
138
     */
139
    public function __set(string $name, mixed $value)
140
    {
141
        $this->input[$name] = $value;
142
    }
143
144
    /**
145
     * Determine si une cle existe parmis les donnees.
146
     */
147
    public function __isset(string $name): bool
148
    {
149
        return isset($this->input[$name]);
150
    }
151
152
    /**
153
     * Retire une donnees
154
     */
155
    public function __unset(string $name)
156
    {
157
        unset($this->input[$name]);
158
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163
    public function offsetExists($key): bool
164
    {
165
        return isset($this->input[$key]);
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    public function offsetGet($key): mixed
172
    {
173
        return $this->input[$key];
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179
    public function offsetSet($key, $value): void
180
    {
181
        if (null === $key) {
182
            $this->input[] = $value;
183
        } else {
184
            $this->input[$key] = $value;
185
        }
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191
    public function offsetUnset($key): void
192
    {
193
        unset($this->input[$key]);
194
    }
195
196
    /**
197
     * {@inheritDoc}
198
     */
199
    public function getIterator(): Traversable
200
    {
201
        return new ArrayIterator($this->input);
202
    }
203
}
204