Skip to content

Commit

Permalink
Refactor and new tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 11, 2024
1 parent 0ec1659 commit 088218b
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/Interfaces/ArraySelectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ interface ArraySelectorInterface
* @return ArrayViewInterface<T>
*/
public function select(ArrayViewInterface $source, ?bool $readonly = null): ArrayViewInterface;

/**
* @return mixed
*/
public function getValue();
}
4 changes: 2 additions & 2 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function filter(callable $predicate): ArrayViewInterface;

/**
* @param callable(T): bool $predicate
* @return ArraySelectorInterface
* @return MaskSelectorInterface
*/
public function is(callable $predicate): ArraySelectorInterface;
public function is(callable $predicate): MaskSelectorInterface;

/**
* @param ArraySelectorInterface|string $selector
Expand Down
13 changes: 13 additions & 0 deletions src/Interfaces/IndexListSelectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Interfaces;

interface IndexListSelectorInterface extends ArraySelectorInterface
{
/**
* @return array<int>
*/
public function getValue(): array;
}
13 changes: 13 additions & 0 deletions src/Interfaces/MaskSelectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Interfaces;

interface MaskSelectorInterface extends ArraySelectorInterface
{
/**
* @return array<bool>
*/
public function getValue(): array;
}
15 changes: 15 additions & 0 deletions src/Interfaces/SliceSelectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Interfaces;

use Smoren\ArrayView\Structs\Slice;

interface SliceSelectorInterface extends ArraySelectorInterface
{
/**
* @return Slice
*/
public function getValue(): Slice;
}
12 changes: 10 additions & 2 deletions src/Selectors/IndexListSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Smoren\ArrayView\Selectors;

use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
use Smoren\ArrayView\Interfaces\IndexListSelectorInterface;
use Smoren\ArrayView\Views\ArrayIndexListView;

final class IndexListSelector implements ArraySelectorInterface
final class IndexListSelector implements IndexListSelectorInterface
{
/**
* @var array<int>
Expand Down Expand Up @@ -35,4 +35,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
{
return new ArrayIndexListView($source, $this->value, $readonly ?? $source->isReadonly());
}

/**
* {@inheritDoc}
*/
public function getValue(): array
{
return $this->value;
}
}
12 changes: 10 additions & 2 deletions src/Selectors/MaskSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Smoren\ArrayView\Selectors;

use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
use Smoren\ArrayView\Interfaces\MaskSelectorInterface;
use Smoren\ArrayView\Views\ArrayMaskView;

class MaskSelector implements ArraySelectorInterface
class MaskSelector implements MaskSelectorInterface
{
/**
* @var array<bool>
Expand Down Expand Up @@ -35,4 +35,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
{
return new ArrayMaskView($source, $this->value, $readonly ?? $source->isReadonly());
}

/**
* {@inheritDoc}
*/
public function getValue(): array
{
return $this->value;
}
}
8 changes: 8 additions & 0 deletions src/Selectors/SliceSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function select(ArrayViewInterface $source, ?bool $readonly = null): Arra
{
return new ArraySliceView($source, $this, $readonly ?? $source->isReadonly());
}

/**
* {@inheritDoc}
*/
public function getValue(): Slice
{
return $this;
}
}
6 changes: 4 additions & 2 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Smoren\ArrayView\Exceptions\ValueError;
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
use Smoren\ArrayView\Interfaces\MaskSelectorInterface;
use Smoren\ArrayView\Selectors\MaskSelector;
use Smoren\ArrayView\Selectors\SliceSelector;
use Smoren\ArrayView\Structs\Slice;
Expand Down Expand Up @@ -100,9 +101,10 @@ public function filter(callable $predicate): ArrayViewInterface
/**
* {@inheritDoc}
*/
public function is(callable $predicate): ArraySelectorInterface
public function is(callable $predicate): MaskSelectorInterface
{
return new MaskSelector(array_map($predicate, $this->toArray()));
$data = $this->toArray();
return new MaskSelector(array_map($predicate, $data, array_keys($data)));
}

/**
Expand Down
104 changes: 104 additions & 0 deletions tests/unit/ArrayView/WriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,39 @@ public function testApply(array $source, callable $viewGetter, callable $mapper,
$this->assertSame($expected, $source);
}

/**
* @dataProvider dataProviderForApplyWith
*/
public function testApplyWith(array $source, callable $viewGetter, callable $mapper, array $arg, array $expected)
{
// Given
$view = $viewGetter($source);

// When
$view->applyWith($arg, $mapper);

// Then
$this->assertSame($expected, $source);
}

/**
* @dataProvider dataProviderForIsAndFilter
*/
public function testIsAndFilter(array $source, callable $predicate, array $expectedMask, array $expectedArray)
{
// Given
$view = ArrayView::toView($source);

// When
$boolMask = $view->is($predicate);
$filtered = $view->filter($predicate);

// Then
$this->assertSame($expectedMask, $boolMask->getValue());
$this->assertSame($expectedArray, $view->subview($boolMask)->toArray());
$this->assertSame($expectedArray, $filtered->toArray());
}

public function dataProviderForArrayWrite(): array
{
return [
Expand Down Expand Up @@ -326,4 +359,75 @@ public function dataProviderForApply(): array
],
];
}

public function dataProviderForApplyWith(): array
{
return [
[
[],
fn (array &$source) => ArrayView::toView($source),
fn (int $lhs, int $rhs) => $lhs + $rhs,
[],
[],
],
[
[1],
fn (array &$source) => ArrayView::toView($source),
fn (int $lhs, int $rhs) => $lhs + $rhs,
[2],
[3],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source),
fn (int $lhs, int $rhs) => $lhs + $rhs,
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
[11, 22, 33, 44, 55, 66, 77, 88, 99, 110],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source),
fn (int $lhs, int $rhs, int $index) => $index % 2 === 0 ? $lhs : $rhs,
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
[1, 20, 3, 40, 5, 60, 7, 80, 9, 100],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (array &$source) => ArrayView::toView($source)->subview('::2'),
fn (int $lhs, int $rhs) => $lhs * $rhs,
[1, 2, 3, 4, 5],
[1, 2, 6, 4, 15, 6, 28, 8, 45, 10],
],
];
}

public function dataProviderForIsAndFilter(): array
{
return [
[
[],
fn (int $x) => $x % 2 === 0,
[],
[],
],
[
[1],
fn (int $x) => $x % 2 === 0,
[false],
[],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (int $x) => $x % 2 === 0,
[false, true, false, true, false, true, false, true, false, true],
[2, 4, 6, 8, 10],
],
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
fn (int $_, int $i) => $i % 2 === 0,
[true, false, true, false, true, false, true, false, true, false],
[1, 3, 5, 7, 9],
],
];
}
}

0 comments on commit 088218b

Please sign in to comment.