Passed
Push — master ( bd10da...d5e00b )
by Irfaq
14:58
created

ParamBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 15
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 18 5
A wrap() 0 7 3
1
<?php
2
3
namespace Transmission\HttpClient\Message;
4
5
/**
6
 * ParamBuilder
7
 */
8
final class ParamBuilder
9
{
10
    /**
11
     * Sanitize and build params.
12
     *
13
     * @param array $params
14
     *
15
     * @return string
16
     */
17
    public static function build($params)
18
    {
19
        return collect($params)
20
            ->reject(function ($value) {
21
                return blank($value);
22
            })->transform(function ($value, $key) {
23
                if ('ids' === $key) {
24
                    return $value !== 'recently-active' ? static::wrap($value) : $value;
25
                }
26
27
                if (is_string($value)) { // Encode if it's not UTF-8
28
                    if (mb_detect_encoding($value, 'auto') !== 'UTF-8') {
29
                        return mb_convert_encoding($value, 'UTF-8');
30
                    }
31
                }
32
33
                return $value;
34
            })->toArray();
35
    }
36
37
    /**
38
     * If the given value is not an array and not null, wrap it in one.
39
     * And typehint all values to integer. Primarily used for ids.
40
     *
41
     * @param $value
42
     *
43
     * @return array
44
     */
45
    protected static function wrap($value)
46
    {
47
        if (is_null($value)) {
48
            return [];
49
        }
50
51
        return !is_array($value) ? [(int)$value] : array_map('intval', $value);
52
    }
53
}
54