Skip to content

Commit

Permalink
Documentation updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 15, 2024
1 parent 7ecb542 commit 0241ad4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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]
Expand Down
18 changes: 16 additions & 2 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,29 @@ 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];
*
* $subview = ArrayView::toView($source)
* ->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]
Expand Down
41 changes: 38 additions & 3 deletions tests/unit/Examples/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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'],
Expand All @@ -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[':']);
Expand Down

0 comments on commit 0241ad4

Please sign in to comment.