Test Failed
Push — master ( 5aaffa...4ac754 )
by Solo
02:18
created

RequestParamValidator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 66
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validateStringable() 0 14 4
A validateParams() 0 10 3
A validateJsonParams() 0 9 3
A validateMultipartParams() 0 12 4
B isGenerator() 0 11 7
1
<?php
2
3
namespace mpyw\Cowitter\Helpers;
4
5
class RequestParamValidator
6
{
7 31
    public static function validateStringable($name, $value)
8 31
    {
9 31
        if ($value instanceof \CURLFile) {
10 2
            if (false === $value = @file_get_contents($value->getFilename())) {
11 1
                $error = error_get_last();
12 1
                throw new \RuntimeException($error['message']);
13
            }
14 1
            return base64_encode($value);
15
        }
16 30
        if (filter_var($value) === false) {
17 2
            $type = gettype($value);
18 2
            throw new \InvalidArgumentException("\"$name\" must be stringable, $type given.");
19
        }
20 28
        return (string)$value;
21
    }
22
23 45
    public static function validateParams(array $params)
24 45
    {
25 45
        foreach ($params as $key => $value) {
26 15
            if ($value === null) {
27 2
                unset($params[$key]);
28 2
                continue;
29
            }
30 15
            $params[$key] = static::validateStringable($key, $value);
31
        }
32 45
        return $params;
33
    }
34
    
35 15
    public static function validateJsonParams(array $params)
36 15
    {
37 15
        foreach ($params as $key => $value) {
38 13
            if ($value === null) {
39 1
                unset($params[$key]);
40 1
                continue;
41
            }
42 13
        }
43 13
        return $params;
44
    }
45
46 15
    public static function validateMultipartParams(array $params)
47
    {
48
        foreach ($params as $key => $value) {
49 18
            if ($value === null) {
50 18
                unset($params[$key]);
51 18
                continue;
52
            }
53 18
            if (!$value instanceof \CURLFile) {
54 1
                $params[$key] = static::validateStringable($key, $value);
55
            }
56 18
        }
57 15
        return $params;
58 18
    }
59 18
60
    public static function isGenerator(callable $callable)
61
    {
62
        if (is_string($callable) && strpos($callable, '::')) {
63
            $callable = explode('::', $callable);
64
        } elseif (!$callable instanceof \Closure && is_object($callable)) {
65
            $callable = [$callable, '__invoke'];
66
        }
67
        $reflector = $callable instanceof \Closure || is_string($callable)
68
            ? new \ReflectionFunction($callable)
69
            : new \ReflectionMethod($callable[0], $callable[1]);
70
        return $reflector->isGenerator();
71
    }
72
}
73