From 0241ad4e7a2aeb507f5ff1218203ebbcb437d4cb Mon Sep 17 00:00:00 2001 From: Smoren Date: Fri, 15 Mar 2024 19:35:08 +0300 Subject: [PATCH] Documentation updated. --- README.md | 21 ++++++++++++-- src/Views/ArrayView.php | 18 ++++++++++-- tests/unit/Examples/ExamplesTest.php | 41 ++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 248375a..356e85e 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ $view = ArrayView::toView($originalArray); $view->subview(new MaskSelector([true, false, true, false, true])).toArray(); // [1, 3, 5] $view->subview(new IndexListSelector([1, 2, 4])).toArray(); // [2, 3, 5] $view->subview(new SliceSelector('::-1')).toArray(); // [5, 4, 3, 2, 1] + +$view->subview([true, false, true, false, true]).toArray(); // [1, 3, 5] +$view->subview([1, 2, 4]).toArray(); // [2, 3, 5] $view->subview('::-1').toArray(); // [5, 4, 3, 2, 1] $view->subview(new MaskSelector([true, false, true, false, true])).apply(fn ($x) => x * 10); @@ -75,6 +78,9 @@ $view = ArrayView::toView($originalArray); $view[new MaskSelector([true, false, true, false, true])]; // [1, 3, 5] $view[new IndexListSelector([1, 2, 4])]; // [2, 3, 5] $view[new SliceSelector('::-1')]; // [5, 4, 3, 2, 1] + +$view[[true, false, true, false, true]]; // [1, 3, 5] +$view[[1, 2, 4]]; // [2, 3, 5] $view['::-1']; // [5, 4, 3, 2, 1] $view[new MaskSelector([true, false, true, false, true])] = [10, 30, 50]; @@ -89,12 +95,21 @@ use Smoren\ArrayView\Selectors\SliceSelector; use Smoren\ArrayView\Views\ArrayView; $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - $subview = ArrayView::toView($originalArray) - ->subview('::2') // [1, 3, 5, 7, 9] + ->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9] ->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9] ->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7] - ->subview('1:'); // [5, 7] + ->subview(new SliceSelector('1:')); // [5, 7] + +$subview[':'] = [55, 77]; +print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10] + +$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +$subview = ArrayView::toView($originalArray) + ->subview('::2') // [1, 3, 5, 7, 9] + ->subview([true, false, true, true, true]) // [1, 5, 7, 9] + ->subview([0, 1, 2]) // [1, 5, 7] + ->subview('1:'); // [5, 7] $subview[':'] = [55, 77]; print_r($originalArray); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10] diff --git a/src/Views/ArrayView.php b/src/Views/ArrayView.php index 642b138..38da822 100644 --- a/src/Views/ArrayView.php +++ b/src/Views/ArrayView.php @@ -236,7 +236,7 @@ public function is(callable $predicate): MaskSelectorInterface /** * Returns a subview of this view based on a selector or string slice. * - * ##### Example + * ##### Example (using selector objects) * ``` * $source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; * @@ -244,7 +244,21 @@ public function is(callable $predicate): MaskSelectorInterface * ->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9] * ->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9] * ->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7] - * ->subview('1:'); // [5, 7] + * ->subview(new SliceSelector('1:')); // [5, 7] + * + * $subview[':'] = [55, 77]; + * print_r($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10] + * ``` + * + * ##### Example (using short objects) + * ``` + * $source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + * + * $subview = ArrayView::toView($source) + * ->subview('::2') // [1, 3, 5, 7, 9] + * ->subview([true, false, true, true, true]) // [1, 5, 7, 9] + * ->subview([0, 1, 2]) // [1, 5, 7] + * ->subview('1:'); // [5, 7] * * $subview[':'] = [55, 77]; * print_r($source); // [1, 2, 3, 4, 55, 6, 77, 8, 9, 10] diff --git a/tests/unit/Examples/ExamplesTest.php b/tests/unit/Examples/ExamplesTest.php index 0ae6cbe..6646cd5 100644 --- a/tests/unit/Examples/ExamplesTest.php +++ b/tests/unit/Examples/ExamplesTest.php @@ -44,6 +44,15 @@ public function testSubview() [5, 4, 3, 2, 1], $originalView->subview(new SliceSelector('::-1'))->toArray(), ); + + $this->assertSame( + [1, 3, 5], + $originalView->subview([true, false, true, false, true])->toArray(), + ); + $this->assertSame( + [2, 3, 5], + $originalView->subview([1, 2, 4])->toArray(), + ); $this->assertSame( [5, 4, 3, 2, 1], $originalView->subview('::-1')->toArray(), @@ -72,6 +81,15 @@ public function testSubarray() [5, 4, 3, 2, 1], $originalView[new SliceSelector('::-1')], ); + + $this->assertSame( + [1, 3, 5], + $originalView[[true, false, true, false, true]], + ); + $this->assertSame( + [2, 3, 5], + $originalView[[1, 2, 4]], + ); $this->assertSame( [5, 4, 3, 2, 1], $originalView['::-1'], @@ -87,10 +105,27 @@ public function testCombinedSubview() $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; $subview = ArrayView::toView($originalArray) - ->subview('::2') // [1, 3, 5, 7, 9] + ->subview(new SliceSelector('::2')) // [1, 3, 5, 7, 9] ->subview(new MaskSelector([true, false, true, true, true])) // [1, 5, 7, 9] - ->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7] - ->subview('1:'); // [5, 7] + ->subview(new IndexListSelector([0, 1, 2])) // [1, 5, 7] + ->subview(new SliceSelector('1:')); // [5, 7] + + $this->assertSame([5, 7], $subview->toArray()); + $this->assertSame([5, 7], $subview[':']); + + $subview[':'] = [55, 77]; + $this->assertSame([1, 2, 3, 4, 55, 6, 77, 8, 9, 10], $originalArray); + } + + public function testCombinedSubview2() + { + $originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + + $subview = ArrayView::toView($originalArray) + ->subview('::2') // [1, 3, 5, 7, 9] + ->subview([true, false, true, true, true]) // [1, 5, 7, 9] + ->subview([0, 1, 2]) // [1, 5, 7] + ->subview('1:'); // [5, 7] $this->assertSame([5, 7], $subview->toArray()); $this->assertSame([5, 7], $subview[':']);