Cast   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C apply() 0 29 15
1
<?php
2
/**
3
 * File copied from Waavi/Sanitizer https://github.com/waavi/sanitizer
4
 * Sanitization functionality to be customized within this project before a 1.0 release.
5
 */
6
7
namespace PerfectOblivion\Valid\Sanitizer\Filters;
8
9
use InvalidArgumentException;
10
use Illuminate\Support\Collection;
11
use PerfectOblivion\Valid\Sanitizer\Contracts\Filter;
12
13
class Cast implements Filter
14
{
15
    /**
16
     * Cast the value to the given type.
17
     *
18
     * @param  mixed  $value
19
     *
20
     * @return mixed
21
     */
22
    public function apply($value, $options = [])
23
    {
24
        $type = isset($options[0]) ? $options[0] : null;
25
26
        switch ($type) {
27
            case 'int':
28
            case 'integer':
29
                return (int) $value;
30
            case 'real':
31
            case 'float':
32
            case 'double':
33
                return (float) $value;
34
            case 'string':
35
                return (string) $value;
36
            case 'bool':
37
            case 'boolean':
38
                return (bool) $value;
39
            case 'object':
40
                return is_array($value) ? (object) $value : json_decode($value, false);
41
            case 'array':
42
                return json_decode($value, true);
43
            case 'collection':
44
                $array = is_array($value) ? $value : json_decode($value, true);
45
46
                return new Collection($array);
47
            default:
48
                throw new InvalidArgumentException("Wrong Sanitizer casting format: {$type}.");
49
        }
50
    }
51
}
52