|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Smoren\ArrayView\Selectors; |
|
6
|
|
|
|
|
7
|
|
|
use Smoren\ArrayView\Interfaces\ArrayViewInterface; |
|
8
|
|
|
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface; |
|
9
|
|
|
use Smoren\ArrayView\Views\ArrayIndexListView; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Represents an index list selector that selects elements based on the provided array of indexes. |
|
13
|
|
|
*/ |
|
14
|
|
|
final class IndexListSelector implements IndexListSelectorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array<int> The array of indexes to select elements from. |
|
18
|
|
|
*/ |
|
19
|
|
|
private array $value; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Creates a new IndexListSelector instance with the provided array of indexes. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array<int>|ArrayViewInterface<int> $value The array of indexes or array view containing indexes. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($value) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->value = \is_array($value) ? $value : $value->toArray(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Selects elements from the source array based on the index list. |
|
33
|
|
|
* |
|
34
|
|
|
* @template T The type of elements in the source array view. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ArrayViewInterface<T> $source The source array view to select elements from. |
|
37
|
|
|
* @param bool|null $readonly Whether the selection should be read-only. |
|
38
|
|
|
* |
|
39
|
|
|
* @return ArrayIndexListView<T> The view containing the selected elements. |
|
40
|
|
|
* |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayIndexListView |
|
44
|
|
|
{ |
|
45
|
|
|
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Checks if the selector is compatible with the given view. |
|
50
|
|
|
* |
|
51
|
|
|
* @template T View elements type. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ArrayViewInterface<T> $view the view to check compatibility with. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool true if the element is compatible, false otherwise |
|
56
|
|
|
* |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function compatibleWith(ArrayViewInterface $view): bool |
|
60
|
|
|
{ |
|
61
|
|
|
return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getValue(): array |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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.