Passed
Push — master ( 9fff06...1ba6fb )
by Smoren
02:33 queued 16s
created

ArraySliceView::convertIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\ArrayView\Views;
6
7
use Smoren\ArrayView\Exceptions\ReadonlyError;
8
use Smoren\ArrayView\Exceptions\ValueError;
9
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
10
use Smoren\ArrayView\Structs\NormalizedSlice;
11
use Smoren\ArrayView\Structs\Slice;
12
13
/**
14
 * Class representing a slice-based view of an array or another ArrayView
15
 * for accessing elements within a specified slice range.
16
 *
17
 * @template T
18
 *
19
 * @extends ArrayView<T>
20
 */
21
class ArraySliceView extends ArrayView
22
{
23
    /**
24
     * @var NormalizedSlice The normalized slice range defining the view within the source array or ArrayView.
25
     */
26
    protected NormalizedSlice $slice;
27
28
    /**
29
     * Constructs a new ArraySliceView instance with the specified source array or ArrayView and slice range.
30
     *
31
     * @param Array<T>|ArrayViewInterface<T> $source The source array or ArrayView to create a view from.
32
     * @param Slice $slice The slice range specifying the subset of elements to include in the view.
33
     * @param bool|null $readonly Optional flag to indicate whether the view should be readonly.
34
     *
35
     * @throws ValueError if the array is not sequential.
36
     * @throws ReadonlyError if the source is readonly and trying to create a non-readonly view.
37
     */
38
    public function __construct(&$source, Slice $slice, ?bool $readonly = null)
39
    {
40
        parent::__construct($source, $readonly);
41
        $this->slice = $slice->normalize(\count($source));
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function count(): int
48
    {
49
        return \count($this->slice);
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    protected function convertIndex(int $i): int
56
    {
57
        return $this->slice->convertIndex($i);
58
    }
59
}
60