Skip to content

Commit 0241ad4

Browse files
committed
Documentation updated.
1 parent 7ecb542 commit 0241ad4

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ $view = ArrayView::toView($originalArray);
5656
$view->subview(new MaskSelector([true, false, true, false, true])).toArray(); // [1, 3, 5]
5757
$view->subview(new IndexListSelector([1, 2, 4])).toArray(); // [2, 3, 5]
5858
$view->subview(new SliceSelector('::-1')).toArray(); // [5, 4, 3, 2, 1]
59+
60+
$view->subview([true, false, true, false, true]).toArray(); // [1, 3, 5]
61+
$view->subview([1, 2, 4]).toArray(); // [2, 3, 5]
5962
$view->subview('::-1').toArray(); // [5, 4, 3, 2, 1]
6063

6164
$view->subview(new MaskSelector([true, false, true, false, true])).apply(fn ($x) => x * 10);
@@ -75,6 +78,9 @@ $view = ArrayView::toView($originalArray);
7578
$view[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5]
7679
$view[new IndexListSelector([1, 2, 4])]; // [2, 3, 5]
7780
$view[new SliceSelector('::-1')]; // [5, 4, 3, 2, 1]
81+
82+
$view[[true, false, true, false, true]]; // [1, 3, 5]
83+
$view[[1, 2, 4]]; // [2, 3, 5]
7884
$view['::-1']; // [5, 4, 3, 2, 1]
7985

8086
$view[new MaskSelector([true, false, true, false, true])] = [10, 30, 50];
@@ -89,12 +95,21 @@ use Smoren\ArrayView\Selectors\SliceSelector;
8995
use Smoren\ArrayView\Views\ArrayView;
9096

9197
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
92-
9398
$subview = ArrayView::toView($originalArray)
94-
->subview('::2') // [1, 3, 5, 7, 9]
99+
->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9]
95100
->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
96101
->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
97-
->subview('1:'); // [5, 7]
102+
->subview(new SliceSelector('1:')); // [5, 7]
103+
104+
$subview[':'] = [55, 77];
105+
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
106+
107+
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
108+
$subview = ArrayView::toView($originalArray)
109+
->subview('::2') // [1, 3, 5, 7, 9]
110+
->subview([true, false, true, true, true]) // [1, 5, 7, 9]
111+
->subview([0, 1, 2]) // [1, 5, 7]
112+
->subview('1:'); // [5, 7]
98113

99114
$subview[':'] = [55, 77];
100115
print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

src/Views/ArrayView.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,29 @@ public function is(callable $predicate): MaskSelectorInterface
236236
/**
237237
* Returns a subview of this view based on a selector or string slice.
238238
*
239-
* ##### Example
239+
* ##### Example (using selector objects)
240240
* ```
241241
* $source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
242242
*
243243
* $subview = ArrayView::toView($source)
244244
* ->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9]
245245
* ->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
246246
* ->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
247-
* ->subview('1:'); // [5, 7]
247+
* ->subview(new SliceSelector('1:')); // [5, 7]
248+
*
249+
* $subview[':'] = [55, 77];
250+
* print_r($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]
251+
* ```
252+
*
253+
* ##### Example (using short objects)
254+
* ```
255+
* $source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
256+
*
257+
* $subview = ArrayView::toView($source)
258+
* ->subview('::2') // [1, 3, 5, 7, 9]
259+
* ->subview([true, false, true, true, true]) // [1, 5, 7, 9]
260+
* ->subview([0, 1, 2]) // [1, 5, 7]
261+
* ->subview('1:'); // [5, 7]
248262
*
249263
* $subview[':'] = [55, 77];
250264
* print_r($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10]

tests/unit/Examples/ExamplesTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public function testSubview()
4444
[5, 4, 3, 2, 1],
4545
$originalView->subview(new SliceSelector('::-1'))->toArray(),
4646
);
47+
48+
$this->assertSame(
49+
[1, 3, 5],
50+
$originalView->subview([true, false, true, false, true])->toArray(),
51+
);
52+
$this->assertSame(
53+
[2, 3, 5],
54+
$originalView->subview([1, 2, 4])->toArray(),
55+
);
4756
$this->assertSame(
4857
[5, 4, 3, 2, 1],
4958
$originalView->subview('::-1')->toArray(),
@@ -72,6 +81,15 @@ public function testSubarray()
7281
[5, 4, 3, 2, 1],
7382
$originalView[new SliceSelector('::-1')],
7483
);
84+
85+
$this->assertSame(
86+
[1, 3, 5],
87+
$originalView[[true, false, true, false, true]],
88+
);
89+
$this->assertSame(
90+
[2, 3, 5],
91+
$originalView[[1, 2, 4]],
92+
);
7593
$this->assertSame(
7694
[5, 4, 3, 2, 1],
7795
$originalView['::-1'],
@@ -87,10 +105,27 @@ public function testCombinedSubview()
87105
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
88106

89107
$subview = ArrayView::toView($originalArray)
90-
->subview('::2') // [1, 3, 5, 7, 9]
108+
->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9]
91109
->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9]
92-
->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
93-
->subview('1:'); // [5, 7]
110+
->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7]
111+
->subview(new SliceSelector('1:')); // [5, 7]
112+
113+
$this->assertSame([5, 7], $subview->toArray());
114+
$this->assertSame([5, 7], $subview[':']);
115+
116+
$subview[':'] = [55, 77];
117+
$this->assertSame([1, 2, 3, 4, 55, 6, 77, 8, 9, 10], $originalArray);
118+
}
119+
120+
public function testCombinedSubview2()
121+
{
122+
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
123+
124+
$subview = ArrayView::toView($originalArray)
125+
->subview('::2') // [1, 3, 5, 7, 9]
126+
->subview([true, false, true, true, true]) // [1, 5, 7, 9]
127+
->subview([0, 1, 2]) // [1, 5, 7]
128+
->subview('1:'); // [5, 7]
94129

95130
$this->assertSame([5, 7], $subview->toArray());
96131
$this->assertSame([5, 7], $subview[':']);

0 commit comments

Comments
 (0)