Passed
Pull Request — master (#45)
by Christopher
03:22
created

iterable_to_array()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 2
b 0
f 1
nc 3
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
if (!function_exists('mb_ord')) {
5
    function mb_ord($s, $encoding = null)
6
    {
7
        $getEncoding = function ($encoding) {
8
            if (null === $encoding) {
9
                return 'UTF-8';
10
            }
11
12
            if ('UTF-8' === $encoding) {
13
                return 'UTF-8';
14
            }
15
16
            $encoding = strtoupper($encoding);
17
18
            if ('8BIT' === $encoding || 'BINARY' === $encoding) {
19
                return 'CP850';
20
            }
21
22
            if ('UTF8' === $encoding) {
23
                return 'UTF-8';
24
            }
25
26
            return $encoding;
27
        };
28
29
        if ('UTF-8' !== $encoding = $getEncoding($encoding)) {
30
            $s = mb_convert_encoding($s, 'UTF-8', $encoding);
31
        }
32
33
        if (1 === \strlen($s)) {
34
            return \ord($s);
35
        }
36
37
        $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
38
        if (0xF0 <= $code) {
39
            return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
40
        }
41
        if (0xE0 <= $code) {
42
            return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
43
        }
44
        if (0xC0 <= $code) {
45
            return (($code - 0xC0) << 6) + $s[2] - 0x80;
46
        }
47
48
        return $code;
49
    }
50
}
51
if(!function_exists('iterable_to_array')) {
52
    function iterable_to_array(?iterable $it): array
53
    {
54
        if(null === $it) return [];
55
        if (is_array($it)) return $it;
56
        $ret = [];
57
        array_push($ret, ...$it);
58
        return $ret;
59
    }
60
}
61
if(!function_exists('iterable_to_traversable')) {
62
63
    function iterable_to_traversable(?iterable $it): Traversable
64
    {
65
        if(null === $it) $it = [];
66
        yield from $it;
67
    }
68
}