ArrayStream::limit()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
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