-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCast.php
51 lines (47 loc) · 1.37 KB
/
Cast.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Mawuekom\RequestSanitizer\Sanitizers;
use Illuminate\Support\Collection;
use Mawuekom\RequestSanitizer\Contracts\SanitizerContract;
/**
* Casts a variable into the given type.
*
* Class Cast
*
* @package Mawuekom\RequestSanitizer\Sanitizers
*/
class Cast implements SanitizerContract
{
/**
* Sanitize an input and return it.
*
* @param $input
* @return string
*/
public function sanitize($input)
{
$type = isset($options[0]) ? $options[0] : null;
switch ($type) {
case 'int':
case 'integer':
return (int) $input;
case 'real':
case 'float':
case 'double':
return (float) $input;
case 'string':
return (string) $input;
case 'bool':
case 'boolean':
return (bool) $input;
case 'object':
return is_array($input) ? (object) $input : json_decode($input, false);
case 'array':
return json_decode($input, true);
case 'collection':
$array = is_array($input) ? $input : json_decode($input, true);
return new Collection($array);
default:
throw new \InvalidArgumentException("Wrong Sanitizer casting format: {$type}.");
}
}
}