Completed
Push — master ( c915d2...2ad4b2 )
by Alexander
04:32
created

ConvertsToSnakeCase   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 134
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __isset() 0 4 1
A __get() 0 4 1
A getValidatorInstance() 0 6 1
A getConvertedParameters() 0 8 1
A castBooleans() 0 14 4
A castValueToBoolean() 0 8 3
A convertToSnakeCase() 0 10 3
A convertArrayToSnakeCase() 0 10 2
all() 0 1 ?
getInputSource() 0 1 ?
1
<?php
2
3
namespace Flugg\Responder\Traits;
4
5
/**
6
 * Use this trait in your base form request to convert all camel cased parameters to
7
 * snake case and boolean strings to PHP booleans when accessing the input from
8
 * the controller.
9
 *
10
 * @package Laravel Responder
11
 * @author  Alexander Tømmerås <[email protected]>
12
 * @license The MIT License
13
 */
14
trait ConvertsToSnakeCase
15
{
16
    /**
17
     * Check if an input element is set on the request.
18
     *
19
     * @param  string $key
20
     * @return bool
21
     */
22
    public function __isset( $key )
23
    {
24
        return parent::__isset( snake_case( $key ) );
25
    }
26
27
    /**
28
     * Get an input element from the request.
29
     *
30
     * @param  string $key
31
     * @return mixed
32
     */
33
    public function __get( $key )
34
    {
35
        return parent::__get( snake_case( $key ) );
36
    }
37
38
    /**
39
     * Get the validator instance for the request.
40
     *
41
     * @return \Illuminate\Contracts\Validation\Validator
42
     */
43
    protected function getValidatorInstance()
44
    {
45
        $this->getInputSource()->replace( $this->getConvertedParameters() );
46
47
        return parent::getValidatorInstance();
48
    }
49
50
    /**
51
     * Cast and convert parameters.
52
     *
53
     * @return array
54
     */
55
    protected function getConvertedParameters():array
56
    {
57
        $parameters = $this->all();
58
        $parameters = $this->castBooleans( $parameters );
59
        $parameters = $this->convertToSnakeCase( $parameters );
60
61
        return $parameters;
62
    }
63
64
    /**
65
     * Cast all string booleans to real boolean values.
66
     *
67
     * @param  mixed $input
68
     * @return array
69
     */
70
    protected function castBooleans( $input ):array
71
    {
72
        if ( isset( $this->convertToSnakeCase ) && ! $this->convertToSnakeCase ) {
73
            return;
74
        }
75
76
        $casted = [ ];
77
78
        foreach ( $input as $key => $value ) {
79
            $casted[ $key ] = $this->castValueToBoolean( $value );
80
        }
81
82
        return $casted;
83
    }
84
85
    /**
86
     * Cast a given value to a boolean if it is in fact a boolean.
87
     *
88
     * @param  mixed $value
89
     * @return mixed
90
     */
91
    protected function castValueToBoolean( $value ):array
92
    {
93
        if ( $value === 'true' || $value === 'false' ) {
94
            return filter_var( $value, FILTER_VALIDATE_BOOLEAN );
95
        }
96
97
        return $value;
98
    }
99
100
    /**
101
     * Convert a string or array to snake case.
102
     *
103
     * @param  mixed $input
104
     * @return mixed
105
     */
106
    protected function convertToSnakeCase( $input )
107
    {
108
        if ( is_null( $input ) ) {
109
            return null;
110
        } elseif ( is_array( $input ) ) {
111
            return $this->convertArrayToSnakeCase( $input );
112
        }
113
114
        return snake_case( $input );
115
    }
116
117
    /**
118
     * Convert all keys of an array to snake case.
119
     *
120
     * @param  array $input
121
     * @return array
122
     */
123
    protected function convertArrayToSnakeCase( array $input ):array
124
    {
125
        $converted = [ ];
126
127
        foreach ( $input as $key => $value ) {
128
            $converted[ snake_case( $key ) ] = $value;
129
        }
130
131
        return $converted;
132
    }
133
134
    /**
135
     * Get all of the input and files for the request.
136
     *
137
     * @return array
138
     */
139
    abstract public function all();
140
141
    /**
142
     * Get the input source for the request.
143
     *
144
     * @return \Symfony\Component\HttpFoundation\ParameterBag
145
     */
146
    abstract protected function getInputSource();
147
}