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

ParamBuilder::wrap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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