SliceSelector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
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
 *  ```php
16
 * $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
17
 * $view = ArrayView::toView($originalArray);
18
 *
19
 * $selector = new SliceSelector('::2');
20
 * print_r($view[$selector]); // [1, 3, 5, 7, 9]
21
 * print_r($view->subview($selector)->toArray()); // [1, 3, 5, 7, 9]
22
 *
23
 * $selector = new SliceSelector('1::2');
24
 * print_r($view[$selector]); // [2, 4, 6, 8, 10]
25
 * print_r($view->subview($selector)->toArray()); // [2, 4, 6, 8, 10]
26
 *
27
 * $selector = new SliceSelector('-3::-2');
28
 * print_r($view[$selector]); // [8, 6, 4, 2]
29
 * print_r($view->subview($selector)->toArray()); // [8, 6, 4, 2]
30
 *
31
 * $selector = new SliceSelector('1:4');
32
 * print_r($view[$selector]); // [2, 3, 4]
33
 * print_r($view->subview($selector)->toArray()); // [2, 3, 4]
34
 *
35
 * $selector = new SliceSelector('-2:0:-1');
36
 * print_r($view[$selector]); // [9, 8, 7, 6, 5, 4, 3, 2]
37
 * print_r($view->subview($selector)->toArray()); // [9, 8, 7, 6, 5, 4, 3, 2]
38
 * ```
39
 */
40
final class SliceSelector extends Slice implements ArraySelectorInterface
41
{
42
    /**
43
     * Creates a new SliceSelector instance with the provided slice parameters.
44
     *
45
     * @param Slice|string|array<int> $slice The slice instance or slice string defining the selection.
46
     */
47
    public function __construct($slice)
48
    {
49
        $s = Slice::toSlice($slice);
50
        parent::__construct($s->start, $s->end, $s->step);
51
    }
52
53
    /**
54
     * Selects elements from the source array based on the slice parameters.
55
     *
56
     * @template T The type of elements in the source array.
57
     *
58
     * @param ArrayViewInterface<T> $source The source array to select elements from.
59
     * @param bool|null $readonly Whether the selection should be read-only.
60
     *
61
     * @return ArraySliceView<T> The view containing the selected elements.
62
     *
63
     * {@inheritDoc}
64
     */
65
    public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface
66
    {
67
        return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
68
    }
69
70
    /**
71
     * Checks if the selector is compatible with the given view.
72
     *
73
     * @template T View elements type.
74
     *
75
     * @param ArrayViewInterface<T> $view the view to check compatibility with.
76
     *
77
     * @return bool true if the element is compatible, false otherwise
78
     *
79
     * {@inheritDoc}
80
     */
81
    public function compatibleWith(ArrayViewInterface $view): bool
82
    {
83
        return true;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function getValue(): Slice
90
    {
91
        return $this;
92
    }
93
}
94