Passed
Push — master ( 0a6b04...2ff664 )
by Smoren
01:53
created

SliceSelector::compatibleWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
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\Selectors;
6
7
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
8
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
9
use Smoren\ArrayView\Structs\Slice;
10
use Smoren\ArrayView\Views\ArraySliceView;
11
12
/**
13
 * Represents a slice selector that selects elements based on the provided slice parameters.
14
 */
15
class SliceSelector extends Slice implements ArraySelectorInterface
16
{
17
    /**
18
     * Creates a new SliceSelector instance with the provided slice parameters.
19
     *
20
     * @param Slice|string|array<int> $slice The slice instance or slice string defining the selection.
21
     */
22
    public function __construct($slice)
23
    {
24
        $s = Slice::toSlice($slice);
25
        parent::__construct($s->start, $s->end, $s->step);
26
    }
27
28
    /**
29
     * Selects elements from the source array based on the slice parameters.
30
     *
31
     * @template T The type of elements in the source array.
32
     *
33
     * @param ArrayViewInterface<T> $source The source array to select elements from.
34
     * @param bool|null $readonly Whether the selection should be read-only.
35
     *
36
     * @return ArraySliceView<T> The view containing the selected elements.
37
     *
38
     * {@inheritDoc}
39
     */
40
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface
41
    {
42
        return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
43
    }
44
45
    /**
46
     * Checks if the selector is compatible with the given view.
47
     *
48
     * @template T View elements type.
49
     *
50
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
51
     *
52
     * @return bool true if the element is compatible, false otherwise
53
     *
54
     * {@inheritDoc}
55
     */
56
    public function compatibleWith(ArrayViewInterface $view): bool
57
    {
58
        return true;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function getValue(): Slice
65
    {
66
        return $this;
67
    }
68
}
69