Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods ArrayView::match() and ArrayView::matchWith() added and tested. #28

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Exceptions/NotSupportedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* Error class for not supported errors.
*/
class NotSupportedError extends \Exception
class NotSupportedError extends \RuntimeException
{
}
75 changes: 67 additions & 8 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public static function toUnlinkedView($source, ?bool $readonly = null): ArrayVie
*/
public function toArray(): array;

/**
* Returns a subview of this view based on a selector or string slice.
*
* @param string|array<int|bool>|ArrayViewInterface<int|bool>|ArraySelectorInterface $selector The selector or
* string to filter the subview.
* @param bool|null $readonly Flag indicating if the subview should be read-only.
*
* @return ArrayViewInterface<T> A new view representing the subview of this view.
*
* @throws IndexError if the selector is IndexListSelector and some indexes are out of range.
* @throws SizeError if the selector is MaskSelector and size of the mask not equals to size of the view.
*/
public function subview($selector, bool $readonly = null): ArrayViewInterface;

/**
* Filters the elements in the view based on a predicate function.
*
Expand All @@ -80,22 +94,67 @@ public function filter(callable $predicate): ArrayViewInterface;
* @param callable(T, int): bool $predicate Function that returns a boolean value for each element.
*
* @return MaskSelectorInterface Boolean mask for selecting elements that satisfy the predicate.
*
* @see ArrayViewInterface::match() Full synonim.
*/
public function is(callable $predicate): MaskSelectorInterface;

/**
* Returns a subview of this view based on a selector or string slice.
* Checks if all elements in the view satisfy a given predicate function.
*
* @param string|array<int|bool>|ArrayViewInterface<int|bool>|ArraySelectorInterface $selector The selector or
* string to filter the subview.
* @param bool|null $readonly Flag indicating if the subview should be read-only.
* @param callable(T, int): bool $predicate Function that returns a boolean value for each element.
*
* @return ArrayViewInterface<T> A new view representing the subview of this view.
* @return MaskSelectorInterface Boolean mask for selecting elements that satisfy the predicate.
*
* @throws IndexError if the selector is IndexListSelector and some indexes are out of range.
* @throws SizeError if the selector is MaskSelector and size of the mask not equals to size of the view.
* @see ArrayViewInterface::is() Full synonim.
*/
public function subview($selector, bool $readonly = null): ArrayViewInterface;
public function match(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>|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;

/**
* Transforms each element of the array using the given callback function.
*
* The callback function receives two parameters: the current element of the array and its index.
*
* @param callable(T, int): T $mapper Function to transform each element.
*
* @return array<T> New array with transformed elements of this view.
*/
public function map(callable $mapper): array;

/**
* Transforms each pair of elements from the current array view and the provided data array using the given
* callback function.
*
* The callback function receives three parameters: the current element of the current array view,
* the corresponding element of the data array, and the index.
*
* @template U The type rhs of a binary operation.
*
* @param array<U>|ArrayViewInterface<U>|U $data The rhs values for a binary operation.
* @param callable(T, U, int): T $mapper Function to transform each pair of elements.
*
* @return array<mixed> New array with transformed elements of this view.
*
* @throws ValueError if the $data is not sequential array.
* @throws SizeError if size of $data not equals to size of the view.
*/
public function mapWith($data, callable $mapper): array;

/**
* Applies a transformation function to each element in the view.
Expand Down
2 changes: 1 addition & 1 deletion src/Structs/NormalizedSlice.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getStep(): int
*/
public function count(): int
{
return intval(ceil(abs((($this->end - $this->start) / $this->step))));
return \intval(\ceil(\abs((($this->end - $this->start) / $this->step))));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Structs/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function toSlice($s): Slice
}

if (!self::isSliceString($s)) {
$str = \is_scalar($s) ? "{$s}" : gettype($s);
$str = \is_scalar($s) ? "{$s}" : \gettype($s);
throw new ValueError("Invalid slice: \"{$str}\".");
}

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/ArrayViewAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function offsetExists($offset): bool
*
* $view['::2']; // [1, 3, 5]
* $view[[0, 2, 4]]; // [1, 3, 5]
* $view[[true, true, false, false, true]]; // [1, 3, 5]
* $view[[true, true, false, false, true]]; // [1, 2, 5]
* ```
*
* @param numeric|S $offset The offset to get the value at.
Expand Down
Loading
Loading