Passed
Pull Request — master (#42)
by Théo
02:03
created

iterables_to_iterator()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use AppendIterator;
18
use ArrayIterator;
19
use Assert\Assertion;
20
use Iterator;
21
use IteratorAggregate;
22
use Phar;
23
use Symfony\Component\Filesystem\Filesystem;
24
use Webmozart\PathUtil\Path;
25
26
function canonicalize(string $path): string
27
{
28
    $lastChar = substr($path, -1);
29
30
    $canonical = Path::canonicalize($path);
31
32
    return '/' === $lastChar ? $canonical.$lastChar : $canonical;
33
}
34
35
function is_absolute(string $path): bool
36
{
37
    static $fileSystem;
38
39
    if (null === $fileSystem) {
40
        $fileSystem = new Filesystem();
41
    }
42
43
    return $fileSystem->isAbsolutePath($path);
44
}
45
46
/**
47
 * TODO: this function should be pushed down to the PHAR extension.
48
 */
49
function get_phar_compression_algorithms(): array
50
{
51
    static $algorithms = [
52
        'GZ' => Phar::GZ,
53
        'BZ2' => Phar::BZ2,
54
        'NONE' => Phar::NONE,
55
    ];
56
57
    return $algorithms;
58
}
59
60
function iterables_to_iterator(iterable ...$iterables): Iterator
61
{
62
    $iterator = new AppendIterator();
63
64
    foreach ($iterables as $iterable) {
65
        if (is_array($iterable)) {
66
            $iterator->append(new ArrayIterator($iterable));
67
        } elseif ($iterable instanceof IteratorAggregate) {
68
            $iterator->append($iterable->getIterator());
69
        } else {
70
            Assertion::isInstanceOf($iterable, Iterator::class);
71
72
            $iterator->append($iterable);
73
        }
74
    }
75
76
    return $iterator;
77
}
78
79
function formatted_filesize(string $path)
80
{
81
    Assertion::file($path);
82
83
    $size = filesize($path);
84
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
85
    $power = $size > 0 ? floor(log($size, 1024)) : 0;
86
87
    return sprintf(
88
        '%s%s',
89
        number_format(
90
            $size / (1024 ** $power),
91
            2,
92
            '.',
93
            ','
94
        ),
95
        $units[$power]
96
    );
97
}
98
99
function register_aliases(): void
100
{
101
    if (false === class_exists(\Herrera\Box\Compactor\Javascript::class, false)) {
102
        class_alias(\KevinGH\Box\Compactor\Javascript::class, \Herrera\Box\Compactor\Javascript::class);
103
    }
104
105
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
106
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
107
    }
108
109
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
110
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
111
    }
112
}
113