Passed
Push — master ( 6865c5...ce3e3e )
by Ryosuke
02:28
created

RequestParamValidator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 0
dl 0
loc 57
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateStringable() 0 15 4
A validateParams() 0 11 3
A validateMultipartParams() 0 13 4
B isGenerator() 0 12 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 (false === $result = filter_var($value)) {
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 validateMultipartParams(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
            if (!$value instanceof \CURLFile) {
43 13
                $params[$key] = static::validateStringable($key, $value);
44
            }
45
        }
46 15
        return $params;
47
    }
48
49 18
    public static function isGenerator(callable $callable)
50 18
    {
51 18
        if (is_string($callable) && strpos($callable, '::')) {
52
            $callable = explode('::', $callable);
53 18
        } elseif (!$callable instanceof \Closure && is_object($callable)) {
54 1
            $callable = [$callable, '__invoke'];
55
        }
56 18
        $reflector = $callable instanceof \Closure || is_string($callable)
57 15
            ? new \ReflectionFunction($callable)
58 18
            : new \ReflectionMethod($callable[0], $callable[1]);
59 18
        return $reflector->isGenerator();
60
    }
61
}
62