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\MaskSelectorInterface; |
9
|
|
|
use Smoren\ArrayView\Views\ArrayMaskView; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents a mask selector that selects elements based on the provided array of boolean mask values. |
13
|
|
|
*/ |
14
|
|
|
class MaskSelector implements MaskSelectorInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array<bool> The array of boolean mask values to select elements based on. |
18
|
|
|
*/ |
19
|
|
|
private $value; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Creates a new MaskSelector instance with the provided array of boolean mask values. |
23
|
|
|
* |
24
|
|
|
* @param array<bool>|ArrayViewInterface<bool> $value The array or array view of boolean mask values. |
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 mask values. |
33
|
|
|
* |
34
|
|
|
* @template T The type of elements in the source array view. |
35
|
|
|
* |
36
|
|
|
* @param ArrayViewInterface<T> $source The source array to select elements from. |
37
|
|
|
* @param bool|null $readonly Whether the selection should be read-only. |
38
|
|
|
* |
39
|
|
|
* @return ArrayMaskView<T> The view containing the selected elements. |
40
|
|
|
* |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayMaskView |
44
|
|
|
{ |
45
|
|
|
return new ArrayMaskView($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) === \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.