Completed
Push — master ( 66c45a...b9f89d )
by Alex
22s queued 18s
created

iterable_to_array()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 3
eloc 7
c 3
b 1
f 1
nc 3
nop 1
dl 0
loc 11
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) {
55
            return [];
56
        }
57
        if (is_array($it)) {
58
            return $it;
59
        }
60
        $ret = [];
61
        array_push($ret, ...$it);
62
        return $ret;
63
    }
64
}
65
if (!function_exists('iterable_to_traversable')) {
66
    function iterable_to_traversable(?iterable $it): Traversable
67
    {
68
        if (null === $it) {
69
            $it = [];
70
        }
71
        yield from $it;
72
    }
73
}
74