Completed
Push — master ( 46f0a1...360314 )
by Ryosuke
03:06
created

RequestParamValidator::validateParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace mpyw\Cowitter\Helpers;
4
5
class RequestParamValidator
6
{
7 5
    public static function validateStringable($name, $value)
8 5
    {
9 5
        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 4
        if (false === $result = filter_var($value)) {
17 2
            $type = gettype($value);
18 2
            throw new \InvalidArgumentException("\"$name\" must be stringable, $type given.");
19
        }
20 2
        return (string)$value;
21
    }
22
23 1
    public static function validateParams(array $params)
24 1
    {
25 1
        foreach ($params as $key => $value) {
26 1
            if ($value === null) {
27 1
                unset($params[$key]);
28 1
                continue;
29
            }
30 1
            $params[$key] = static::validateStringable($key, $value);
31
        }
32 1
        return $params;
33
    }
34
35 1
    public static function validateMultipartParams(array $params)
36 1
    {
37 1
        foreach ($params as $key => $value) {
38 1
            if ($value === null) {
39 1
                unset($params[$key]);
40 1
                continue;
41
            }
42 1
            if (!$value instanceof \CURLFile) {
43 1
                $params[$key] = static::validateStringable($key, $value);
44
            }
45
        }
46 1
        return $params;
47
    }
48
49
}
50