|
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\Interfaces\PipeSelectorInterface; |
|
10
|
|
|
use Smoren\ArrayView\Views\ArrayView; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Represents a selector that applies a series of selectors sequentially to a source array view. |
|
14
|
|
|
* |
|
15
|
|
|
* ##### Example |
|
16
|
|
|
* ```php |
|
17
|
|
|
* $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
18
|
|
|
* $selector = new PipeSelector([ |
|
19
|
|
|
* new SliceSelector('::2'), |
|
20
|
|
|
* new MaskSelector([true, false, true, true, true]), |
|
21
|
|
|
* new IndexListSelector([0, 1, 2]), |
|
22
|
|
|
* new SliceSelector('1:'), |
|
23
|
|
|
* ]); |
|
24
|
|
|
* |
|
25
|
|
|
* $view = ArrayView::toView($originalArray); |
|
26
|
|
|
* $subview = $view->subview($selector); |
|
27
|
|
|
* print_r($subview[':']); // [5, 7] |
|
28
|
|
|
* |
|
29
|
|
|
* $subview[':'] = [55, 77]; |
|
30
|
|
|
* print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10] |
|
31
|
|
|
* ``` |
|
32
|
|
|
*/ |
|
33
|
|
|
class PipeSelector implements PipeSelectorInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array<ArraySelectorInterface> An array of selectors to be applied sequentially. |
|
37
|
|
|
*/ |
|
38
|
|
|
private array $selectors; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates a new PipeSelector instance with the provided selectors array. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array<ArraySelectorInterface> $selectors An array of selectors to be assigned to the PipeSelector. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(array $selectors) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->selectors = $selectors; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Applies the series of selectors to the given source array view. |
|
52
|
|
|
* |
|
53
|
|
|
* @template T The type of elements in the source array view. |
|
54
|
|
|
* |
|
55
|
|
|
* @param ArrayViewInterface<T> $source The source array view to select from. |
|
56
|
|
|
* @param bool|null $readonly Optional parameter to specify if the view should be read-only. |
|
57
|
|
|
* |
|
58
|
|
|
* @return ArrayViewInterface<T> The resulting array view after applying all selectors. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$view = ArrayView::toView($source, $readonly); |
|
63
|
|
|
foreach ($this->selectors as $selector) { |
|
64
|
|
|
$view = $selector->select($view, $readonly); |
|
65
|
|
|
} |
|
66
|
|
|
/** @var ArrayViewInterface<T> $view */ |
|
67
|
|
|
return $view; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if the series of selectors are compatible with the given array view. |
|
72
|
|
|
* |
|
73
|
|
|
* @template T The type of elements in the source array view. |
|
74
|
|
|
* |
|
75
|
|
|
* @param ArrayViewInterface<T> $view The array view to check compatibility with. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool True if all selectors are compatible with the array view, false otherwise. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function compatibleWith(ArrayViewInterface $view): bool |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->selectors as $selector) { |
|
82
|
|
|
if (!$selector->compatibleWith($view)) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
$view = $selector->select($view); |
|
86
|
|
|
} |
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the array of selectors assigned to the PipeSelector. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array<ArraySelectorInterface> The array of selectors. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getValue(): array |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->selectors; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|