Skip to content

Commit 7877f23

Browse files
committed
Selectors: compatibleWith() method added.
1 parent 66415d3 commit 7877f23

File tree

6 files changed

+138
-9
lines changed

6 files changed

+138
-9
lines changed

src/Interfaces/ArraySelectorInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ interface ArraySelectorInterface
2121
*/
2222
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;
2323

24+
/**
25+
* Checks if the selector is compatible with the given view.
26+
*
27+
* @template T View elements type.
28+
*
29+
* @param ArrayViewInterface<T> $view the view to check compatibility with.
30+
*
31+
* @return bool true if the element is compatible, false otherwise
32+
*/
33+
public function compatibleWith(ArrayViewInterface $view): bool;
34+
2435
/**
2536
* Return value of the selector.
2637
*

src/Selectors/IndexListSelector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
4545
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
4646
}
4747

48+
/**
49+
* Checks if the selector is compatible with the given view.
50+
*
51+
* @template T View elements type.
52+
*
53+
* @param ArrayViewInterface<T> $view the view to check compatibility with.
54+
*
55+
* @return bool true if the element is compatible, false otherwise
56+
*
57+
* {@inheritDoc}
58+
*/
59+
public function compatibleWith(ArrayViewInterface $view): bool
60+
{
61+
return \count($this->value) === 0 || \max($this->value) < \count($view) && \min($this->value) >= -\count($view);
62+
}
63+
4864
/**
4965
* {@inheritDoc}
5066
*/

src/Selectors/MaskSelector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
4545
return new ArrayMaskView($source, $this->value, $readonly ?? $source->isReadonly());
4646
}
4747

48+
/**
49+
* Checks if the selector is compatible with the given view.
50+
*
51+
* @template T View elements type.
52+
*
53+
* @param ArrayViewInterface<T> $view the view to check compatibility with.
54+
*
55+
* @return bool true if the element is compatible, false otherwise
56+
*
57+
* {@inheritDoc}
58+
*/
59+
public function compatibleWith(ArrayViewInterface $view): bool
60+
{
61+
return \count($this->value) === \count($view);
62+
}
63+
4864
/**
4965
* {@inheritDoc}
5066
*/

src/Selectors/SliceSelector.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
4242
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
4343
}
4444

45+
/**
46+
* Checks if the selector is compatible with the given view.
47+
*
48+
* @template T View elements type.
49+
*
50+
* @param ArrayViewInterface<T> $view the view to check compatibility with.
51+
*
52+
* @return bool true if the element is compatible, false otherwise
53+
*
54+
* {@inheritDoc}
55+
*/
56+
public function compatibleWith(ArrayViewInterface $view): bool
57+
{
58+
return true;
59+
}
60+
4561
/**
4662
* {@inheritDoc}
4763
*/

src/Traits/ArrayViewAccessTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function offsetExists($offset): bool
4242
}
4343

4444
if ($offset instanceof ArraySelectorInterface) {
45-
return true;
45+
return $offset->compatibleWith($this);
4646
}
4747

4848
return false;

tests/unit/ArrayIndexListView/IssetTest.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace Smoren\ArrayView\Tests\Unit\ArrayIndexListView;
44

5+
use Smoren\ArrayView\Exceptions\IndexError;
56
use Smoren\ArrayView\Selectors\IndexListSelector;
6-
use Smoren\ArrayView\Selectors\MaskSelector;
7-
use Smoren\ArrayView\Views\ArrayIndexListView;
87
use Smoren\ArrayView\Views\ArrayView;
98

109
class IssetTest extends \Codeception\Test\Unit
1110
{
1211
/**
13-
* @dataProvider dataProviderForIssetTrue
12+
* @dataProvider dataProviderForIssetSingleTrue
1413
*/
15-
public function testIssetTrue(array $source, array $indexes)
14+
public function testIssetSingleTrue(array $source, array $indexes)
1615
{
1716
$view = ArrayView::toView($source);
1817
$subview = $view->subview(new IndexListSelector($indexes));
@@ -28,9 +27,9 @@ public function testIssetTrue(array $source, array $indexes)
2827
}
2928

3029
/**
31-
* @dataProvider dataProviderForIssetFalse
30+
* @dataProvider dataProviderForIssetSingleFalse
3231
*/
33-
public function testIssetFalse(array $source, array $indexes, array $expected)
32+
public function testIssetSingleFalse(array $source, array $indexes, array $expected)
3433
{
3534
$view = ArrayView::toView($source);
3635
$subview = $view->subview(new IndexListSelector($indexes));
@@ -40,7 +39,36 @@ public function testIssetFalse(array $source, array $indexes, array $expected)
4039
}
4140
}
4241

43-
public function dataProviderForIssetTrue(): array
42+
/**
43+
* @dataProvider dataProviderForIssetSelectorTrue
44+
*/
45+
public function testIssetSelectorTrue(array $source, array $indexes)
46+
{
47+
$view = ArrayView::toView($source);
48+
49+
$this->assertTrue(isset($view[new IndexListSelector($indexes)]));
50+
51+
$subview = $view->subview(new IndexListSelector($indexes));
52+
$this->assertSame(\count($indexes), \count($subview));
53+
54+
$subview = $view[new IndexListSelector($indexes)];
55+
$this->assertSame(\count($indexes), \count($subview));
56+
}
57+
58+
/**
59+
* @dataProvider dataProviderForIssetSelectorFalse
60+
*/
61+
public function testIssetSelectorFalse(array $source, array $indexes)
62+
{
63+
$view = ArrayView::toView($source);
64+
65+
$this->assertFalse(isset($view[new IndexListSelector($indexes)]));
66+
67+
$this->expectException(IndexError::class);
68+
$_ = $view[new IndexListSelector($indexes)];
69+
}
70+
71+
public function dataProviderForIssetSingleTrue(): array
4472
{
4573
return [
4674
[[1], [0]],
@@ -57,7 +85,7 @@ public function dataProviderForIssetTrue(): array
5785
];
5886
}
5987

60-
public static function dataProviderForIssetFalse(): array
88+
public function dataProviderForIssetSingleFalse(): array
6189
{
6290
return [
6391
[[], [], [-2, -1, 0, 1, 2]],
@@ -76,4 +104,46 @@ public static function dataProviderForIssetFalse(): array
76104
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 5, 5, 3], [-8, -7, -6, 5, 6, 7]],
77105
];
78106
}
107+
108+
public function dataProviderForIssetSelectorTrue(): array
109+
{
110+
return [
111+
[[1], []],
112+
[[1], [0]],
113+
[[1], [-1]],
114+
[[1], [0, 0]],
115+
[[1], [0, 0, 0]],
116+
[[1, 2], []],
117+
[[1, 2], [0]],
118+
[[1, 2], [-1, -2]],
119+
[[1, 2], [1]],
120+
[[1, 2], [0, 1]],
121+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], []],
122+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, 7]],
123+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [7, 5, 3, 1]],
124+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 3, 7]],
125+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 7, 8]],
126+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], []],
127+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 5, 5, 3]],
128+
];
129+
}
130+
131+
public function dataProviderForIssetSelectorFalse(): array
132+
{
133+
return [
134+
[[1], [0, 1]],
135+
[[1], [1, -1, -2]],
136+
[[1], [0, 1, 0, -1, -2]],
137+
[[1], [1, -1]],
138+
[[1], [0, 0, -2]],
139+
[[1, 2], [2]],
140+
[[1, 2], [1, 2]],
141+
[[1, 2], [0, 1, 2]],
142+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 3, 5, -10]],
143+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [9, 5, 3, 1]],
144+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 10, 9, 7]],
145+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [-10, 1, 7, 10]],
146+
[[1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 1, 50, 5, 3]],
147+
];
148+
}
79149
}

0 commit comments

Comments
 (0)