InteractsWithRequest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 47.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 17
c 1
b 0
f 0
dl 0
loc 73
ccs 9
cts 19
cp 0.4737
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueryParams() 0 5 1
A except() 0 7 2
A exists() 0 11 4
A only() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Noitran\Repositories\Requests;
6
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Trait InteractsWithRequest.
11
 */
12
trait InteractsWithRequest
13
{
14
    /**
15
     * Get a subset containing the provided keys with values from the input data.
16
     *
17
     * @param $keys
18
     * @param array $input
19
     *
20 14
     * @return array
21
     */
22 14
    public function only($keys, array $input = []): array
23
    {
24 14
        $keys = \is_array($keys) ? $keys : \func_get_args();
25
26 14
        $output = [];
27 14
28
        foreach ($keys as $key) {
29
            Arr::set($output, $key, data_get($input, $key));
30 14
        }
31
32
        return $output;
33
    }
34
35
    /**
36
     * Get all of the input except for a specified array of items.
37
     *
38
     * @param $keys
39
     * @param array $input
40
     *
41
     * @return array
42
     */
43
    public function except($keys, array $input = []): array
44
    {
45
        $keys = \is_array($keys) ? $keys : \func_get_args();
46
47
        Arr::forget($input, $keys);
48
49
        return $input;
50
    }
51
52
    /**
53
     * Determine if the input array contains a given input item key.
54
     *
55
     * @param $key
56
     * @param array $input
57
     *
58
     * @return bool
59
     */
60
    public function exists($key, array $input = []): bool
61
    {
62
        $keys = \is_array($key) ? $key : \func_get_args();
63
64
        foreach ($keys as $value) {
65
            if (! Arr::has($input, $value)) {
66
                return false;
67
            }
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * Returns array of query param keys.
75
     *
76
     * @param array $queryFilters
77
     *
78 14
     * @return array
79
     */
80
    public function getQueryParams(array $queryFilters): array
81 14
    {
82 14
        return array_map(function ($param) {
83
            return $param['queryParameter'];
84
        }, $queryFilters);
85
    }
86
}
87