ArrayStream   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 51
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A skip() 0 8 2
A __construct() 0 3 1
A toArray() 0 7 2
A limit() 0 8 3
1
<?php
2
3
namespace Bdf\Collection\Stream;
4
5
/**
6
 * Create a stream from a native PHP array
7
 */
8
final class ArrayStream extends \ArrayIterator implements StreamInterface
9
{
10
    use StreamTrait;
11
12
    /**
13
     * ArrayStream constructor.
14
     *
15
     * @param array $array The array data
16
     */
17 214
    public function __construct(array $array = [])
18
    {
19 214
        parent::__construct($array);
20 214
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 28
    public function toArray(bool $preserveKeys = true): array
26
    {
27 28
        $array = $this->getArrayCopy();
28
29 28
        return $preserveKeys
30 20
            ? $array
31 28
            : array_values($array)
32
        ;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function skip(int $count): StreamInterface
39
    {
40
        // out of bound : return an empty stream
41 1
        if ($count >= count($this)) {
42 1
            return EmptyStream::instance();
43
        }
44
45 1
        return new LimitStream($this, $count);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function limit(int $count, int $offset = 0): StreamInterface
52
    {
53
        // out of bound : return an empty stream
54 1
        if ($offset >= count($this) || $count === 0) {
55 1
            return EmptyStream::instance();
56
        }
57
58 1
        return new LimitStream($this, $offset, $count);
59
    }
60
}
61