Passed
Push — master ( e7b285...ac21bd )
by noitran
02:40
created

InteractsWithRequest::exists()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 10
cc 4
nc 6
nop 2
crap 20
1
<?php
2
3
namespace Noitran\Repositories\Requests;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Trait InteractsWithRequest
9
 */
10
trait InteractsWithRequest
11
{
12
    /**
13
     * Get a subset containing the provided keys with values from the input data.
14
     *
15
     * @param $keys
16
     * @param array $input
17
     *
18
     * @return array
19
     */
20
    public function only($keys, array $input = []): array
21
    {
22
        $keys = \is_array($keys) ? $keys : \func_get_args();
23
24
        $output = [];
25
26
        foreach ($keys as $key) {
27
            Arr::set($output, $key, data_get($input, $key));
28
        }
29
30
        return $output;
31
    }
32
33
    /**
34
     * Get all of the input except for a specified array of items.
35
     *
36
     * @param $keys
37
     * @param array $input
38
     *
39
     * @return array
40
     */
41
    public function except($keys, array $input = []): array
42
    {
43
        $keys = \is_array($keys) ? $keys : \func_get_args();
44
45
        Arr::forget($input, $keys);
46
47
        return $input;
48
    }
49
50
    /**
51
     * Determine if the input array contains a given input item key.
52
     *
53
     * @param $key
54
     * @param array $input
55
     *
56
     * @return bool
57
     */
58
    public function exists($key, array $input = []): bool
59
    {
60
        $keys = \is_array($key) ? $key : \func_get_args();
61
62
        foreach ($keys as $value) {
63
            if (! Arr::has($input, $value)) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
}
71