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

RequestParamValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 45
ccs 28
cts 28
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateStringable() 0 15 4
A validateParams() 0 11 3
A validateMultipartParams() 0 13 4
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