Skip to content

Commit

Permalink
ArrayView::matchWith() method added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 17, 2024
1 parent fcaaa70 commit 60e7cb4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ public function filter(callable $predicate): ArrayViewInterface;
*/
public function is(callable $predicate): MaskSelectorInterface;

/**
* Compares the elements of the current ArrayView instance with another array or ArrayView
* using the provided comparator function.
*
* @template U The type of the elements in the array for comparison with.
*
* @param array<U>|ArrayViewInterface<U> $data The array or ArrayView to compare to.
* @param callable(T, U, int): bool $comparator Function that determines the comparison logic between the elements.
*
* @return MaskSelectorInterface A MaskSelector instance representing the results of the element comparisons.
*
* @throws ValueError if the $data is not sequential array.
* @throws SizeError if size of $data not equals to size of the view.
*/
public function matchWith($data, callable $comparator): MaskSelectorInterface;

/**
* Returns a subview of this view based on a selector or string slice.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Views/ArrayView.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ public function is(callable $predicate): MaskSelectorInterface
return new MaskSelector(array_map($predicate, $data, array_keys($data)));
}

/**
* Compares the elements of the current ArrayView instance with another array or ArrayView
* using the provided comparator function.
*
* @template U The type of the elements in the array for comparison with.
*
* @param array<U>|ArrayViewInterface<U> $data The array or ArrayView to compare to.
* @param callable(T, U, int): bool $comparator Function that determines the comparison logic between the elements.
*
* @return MaskSelectorInterface A MaskSelector instance representing the results of the element comparisons.
*
* @throws ValueError if the $data is not sequential array.
* @throws SizeError if size of $data not equals to size of the view.
*/
public function matchWith($data, callable $comparator): MaskSelectorInterface
{
$data = $data instanceof ArrayViewInterface ? $data->toArray() : $data;
return new MaskSelector(array_map($comparator, $this->toArray(), $data, array_keys($data)));
}

/**
* Returns a subview of this view based on a selector or string slice.
*
Expand Down

0 comments on commit 60e7cb4

Please sign in to comment.