Skip to content

Commit 088218b

Browse files
committed
Refactor and new tests.
1 parent 0ec1659 commit 088218b

File tree

10 files changed

+184
-8
lines changed

10 files changed

+184
-8
lines changed

src/Interfaces/ArraySelectorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ interface ArraySelectorInterface
1515
* @return ArrayViewInterface<T>
1616
*/
1717
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;
18+
19+
/**
20+
* @return mixed
21+
*/
22+
public function getValue();
1823
}

src/Interfaces/ArrayViewInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function filter(callable $predicate): ArrayViewInterface;
3838

3939
/**
4040
* @param callable(T): bool $predicate
41-
* @return ArraySelectorInterface
41+
* @return MaskSelectorInterface
4242
*/
43-
public function is(callable $predicate): ArraySelectorInterface;
43+
public function is(callable $predicate): MaskSelectorInterface;
4444

4545
/**
4646
* @param ArraySelectorInterface|string $selector
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Interfaces;
6+
7+
interface IndexListSelectorInterface extends ArraySelectorInterface
8+
{
9+
/**
10+
* @return array<int>
11+
*/
12+
public function getValue(): array;
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Interfaces;
6+
7+
interface MaskSelectorInterface extends ArraySelectorInterface
8+
{
9+
/**
10+
* @return array<bool>
11+
*/
12+
public function getValue(): array;
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smoren\ArrayView\Interfaces;
6+
7+
use Smoren\ArrayView\Structs\Slice;
8+
9+
interface SliceSelectorInterface extends ArraySelectorInterface
10+
{
11+
/**
12+
* @return Slice
13+
*/
14+
public function getValue(): Slice;
15+
}

src/Selectors/IndexListSelector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Smoren\ArrayView\Selectors;
44

5-
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
65
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
6+
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface;
77
use Smoren\ArrayView\Views\ArrayIndexListView;
88

9-
final class IndexListSelector implements ArraySelectorInterface
9+
final class IndexListSelector implements IndexListSelectorInterface
1010
{
1111
/**
1212
* @var array<int>
@@ -35,4 +35,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
3535
{
3636
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
3737
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function getValue(): array
43+
{
44+
return $this->value;
45+
}
3846
}

src/Selectors/MaskSelector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Smoren\ArrayView\Selectors;
44

5-
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
65
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
6+
use Smoren\ArrayView\Interfaces\MaskSelectorInterface;
77
use Smoren\ArrayView\Views\ArrayMaskView;
88

9-
class MaskSelector implements ArraySelectorInterface
9+
class MaskSelector implements MaskSelectorInterface
1010
{
1111
/**
1212
* @var array<bool>
@@ -35,4 +35,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
3535
{
3636
return new ArrayMaskView($source, $this->value, $readonly ?? $source->isReadonly());
3737
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function getValue(): array
43+
{
44+
return $this->value;
45+
}
3846
}

src/Selectors/SliceSelector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
3232
{
3333
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
3434
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function getValue(): Slice
40+
{
41+
return $this;
42+
}
3543
}

src/Views/ArrayView.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Smoren\ArrayView\Exceptions\ValueError;
1313
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
1414
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
15+
use Smoren\ArrayView\Interfaces\MaskSelectorInterface;
1516
use Smoren\ArrayView\Selectors\MaskSelector;
1617
use Smoren\ArrayView\Selectors\SliceSelector;
1718
use Smoren\ArrayView\Structs\Slice;
@@ -100,9 +101,10 @@ public function filter(callable $predicate): ArrayViewInterface
100101
/**
101102
* {@inheritDoc}
102103
*/
103-
public function is(callable $predicate): ArraySelectorInterface
104+
public function is(callable $predicate): MaskSelectorInterface
104105
{
105-
return new MaskSelector(array_map($predicate, $this->toArray()));
106+
$data = $this->toArray();
107+
return new MaskSelector(array_map($predicate, $data, array_keys($data)));
106108
}
107109

108110
/**

tests/unit/ArrayView/WriteTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,39 @@ public function testApply(array $source, callable $viewGetter, callable $mapper,
143143
$this->assertSame($expected, $source);
144144
}
145145

146+
/**
147+
* @dataProvider dataProviderForApplyWith
148+
*/
149+
public function testApplyWith(array $source, callable $viewGetter, callable $mapper, array $arg, array $expected)
150+
{
151+
// Given
152+
$view = $viewGetter($source);
153+
154+
// When
155+
$view->applyWith($arg, $mapper);
156+
157+
// Then
158+
$this->assertSame($expected, $source);
159+
}
160+
161+
/**
162+
* @dataProvider dataProviderForIsAndFilter
163+
*/
164+
public function testIsAndFilter(array $source, callable $predicate, array $expectedMask, array $expectedArray)
165+
{
166+
// Given
167+
$view = ArrayView::toView($source);
168+
169+
// When
170+
$boolMask = $view->is($predicate);
171+
$filtered = $view->filter($predicate);
172+
173+
// Then
174+
$this->assertSame($expectedMask, $boolMask->getValue());
175+
$this->assertSame($expectedArray, $view->subview($boolMask)->toArray());
176+
$this->assertSame($expectedArray, $filtered->toArray());
177+
}
178+
146179
public function dataProviderForArrayWrite(): array
147180
{
148181
return [
@@ -326,4 +359,75 @@ public function dataProviderForApply(): array
326359
],
327360
];
328361
}
362+
363+
public function dataProviderForApplyWith(): array
364+
{
365+
return [
366+
[
367+
[],
368+
fn (array &$source) => ArrayView::toView($source),
369+
fn (int $lhs, int $rhs) => $lhs + $rhs,
370+
[],
371+
[],
372+
],
373+
[
374+
[1],
375+
fn (array &$source) => ArrayView::toView($source),
376+
fn (int $lhs, int $rhs) => $lhs + $rhs,
377+
[2],
378+
[3],
379+
],
380+
[
381+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
382+
fn (array &$source) => ArrayView::toView($source),
383+
fn (int $lhs, int $rhs) => $lhs + $rhs,
384+
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
385+
[11, 22, 33, 44, 55, 66, 77, 88, 99, 110],
386+
],
387+
[
388+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
389+
fn (array &$source) => ArrayView::toView($source),
390+
fn (int $lhs, int $rhs, int $index) => $index % 2 === 0 ? $lhs : $rhs,
391+
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
392+
[1, 20, 3, 40, 5, 60, 7, 80, 9, 100],
393+
],
394+
[
395+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
396+
fn (array &$source) => ArrayView::toView($source)->subview('::2'),
397+
fn (int $lhs, int $rhs) => $lhs * $rhs,
398+
[1, 2, 3, 4, 5],
399+
[1, 2, 6, 4, 15, 6, 28, 8, 45, 10],
400+
],
401+
];
402+
}
403+
404+
public function dataProviderForIsAndFilter(): array
405+
{
406+
return [
407+
[
408+
[],
409+
fn (int $x) => $x % 2 === 0,
410+
[],
411+
[],
412+
],
413+
[
414+
[1],
415+
fn (int $x) => $x % 2 === 0,
416+
[false],
417+
[],
418+
],
419+
[
420+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
421+
fn (int $x) => $x % 2 === 0,
422+
[false, true, false, true, false, true, false, true, false, true],
423+
[2, 4, 6, 8, 10],
424+
],
425+
[
426+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
427+
fn (int $_, int $i) => $i % 2 === 0,
428+
[true, false, true, false, true, false, true, false, true, false],
429+
[1, 3, 5, 7, 9],
430+
],
431+
];
432+
}
329433
}

0 commit comments

Comments
 (0)