Input::pick()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
namespace AdvancedLearning\InputValidator;
4
5
/**
6
 * Provides functions for handling input data
7
 */
8
class Input
9
{
10
    /**
11
     * Key/value array of data.
12
     *
13
     * @var array
14
     */
15
    protected $data;
16
17
    /**
18
     * Set the data.
19
     *
20
     * @param array $data Array of key/value pairs.
21
     * @return $this
22
     */
23
    public function setData(array $data)
24
    {
25
        $this->data = $data;
26
        return $this;
27
    }
28
29
    /**
30
     * Gets the data.
31
     *
32
     * @return array
33
     */
34
    public function getData()
35
    {
36
        return $this->data;
37
    }
38
39
    /**
40
     * Pick only the keys wanted in a key/value array.
41
     *
42
     * @param array $data         Key/value pairs.
43
     * @param array $fieldsToPick The keys to keep in the array.
44
     *
45
     * @return Input
46
     */
47
    public static function pick(array $data, array $fieldsToPick)
48
    {
49
        $input = new Input();
50
        $data = array_intersect_key($data, array_combine($fieldsToPick, $fieldsToPick));
51
        return $input->setData($data);
52
    }
53
54
    /**
55
     * Validate the input data.
56
     *
57
     * @param InputValidator $validator Validator to use to validate data.
58
     * @return static
59
     */
60
    public function validate(InputValidator $validator)
61
    {
62
        $validator->valid($this->data);
63
        return $this;
64
    }
65
}
66