1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\ArrayView\Selectors; |
6
|
|
|
|
7
|
|
|
use Smoren\ArrayView\Exceptions\IndexError; |
8
|
|
|
use Smoren\ArrayView\Interfaces\ArrayViewInterface; |
9
|
|
|
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface; |
10
|
|
|
use Smoren\ArrayView\Views\ArrayIndexListView; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Represents an index list selector that selects elements based on the provided array of indexes. |
14
|
|
|
* |
15
|
|
|
* ```php |
16
|
|
|
* $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
17
|
|
|
* $view = ArrayView::toView($originalArray); |
18
|
|
|
* |
19
|
|
|
* $selector = new IndexListSelector([0, 2, 4]); |
20
|
|
|
* print_r($view[$selector]); // [1, 3, 5] |
21
|
|
|
* print_r($view->subview($selector)->toArray()); // [1, 3, 5] |
22
|
|
|
* ``` |
23
|
|
|
*/ |
24
|
|
|
final class IndexListSelector implements IndexListSelectorInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array<int> The array of indexes to select elements from. |
28
|
|
|
*/ |
29
|
|
|
private array $value; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a new IndexListSelector instance with the provided array of indexes. |
33
|
|
|
* |
34
|
|
|
* @param array<int>|ArrayViewInterface<int> $value The array of indexes or array view containing indexes. |
35
|
|
|
*/ |
36
|
|
|
public function __construct($value) |
37
|
|
|
{ |
38
|
|
|
$this->value = \is_array($value) ? $value : $value->toArray(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Selects elements from the source array based on the index list. |
43
|
|
|
* |
44
|
|
|
* @template T The type of elements in the source array view. |
45
|
|
|
* |
46
|
|
|
* @param ArrayViewInterface<T> $source The source array view to select elements from. |
47
|
|
|
* @param bool|null $readonly Whether the selection should be read-only. |
48
|
|
|
* |
49
|
|
|
* @return ArrayIndexListView<T> The view containing the selected elements. |
50
|
|
|
* |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
*/ |
53
|
|
|
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView |
54
|
|
|
{ |
55
|
|
|
if (!$this->compatibleWith($source)) { |
56
|
|
|
throw new IndexError('Some indexes are out of range.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks if the selector is compatible with the given view. |
64
|
|
|
* |
65
|
|
|
* @template T View elements type. |
66
|
|
|
* |
67
|
|
|
* @param ArrayViewInterface<T> $view the view to check compatibility with. |
68
|
|
|
* |
69
|
|
|
* @return bool true if the element is compatible, false otherwise |
70
|
|
|
* |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function compatibleWith(ArrayViewInterface $view): bool |
74
|
|
|
{ |
75
|
|
|
return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function getValue(): array |
82
|
|
|
{ |
83
|
|
|
return $this->value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.