ArrayViewOperationsTrait
Trait providing methods for operation methods of ArrayView.
Tags
Table of Contents
Methods
- apply() : ArrayView<string|int, T>
- Applies a transformation function to each element in the view.
- applyWith() : ArrayView<string|int, T>
- Sets new values for the elements in the view.
- filter() : ArrayMaskView<string|int, T>
- Filters the elements in the view based on a predicate function.
- is() : MaskSelector
- Checks if all elements in the view satisfy a given predicate function.
- map() : array<string|int, T>
- Transforms each element of the array using the given callback function.
- mapWith() : array<string|int, mixed>
- Transforms each pair of elements from the current array view and the provided data array using the given callback function.
- match() : MaskSelector
- Checks if all elements in the view satisfy a given predicate function.
- matchWith() : MaskSelectorInterface
- Compares the elements of the current ArrayView instance with another array or ArrayView using the provided comparator function.
Methods
apply()
Applies a transformation function to each element in the view.
public
apply(callable(T, int): T $mapper) : ArrayView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]
$subview->apply(fn ($x) => $x * 10);
$subview->toArray(); // [10, 30, 50, 70, 90]
$source; // [10, 2, 30, 4, 50, 6, 70, 8, 90, 10]
Parameters
- $mapper : callable(T, int): T
-
Function to transform each element.
Return values
ArrayView<string|int, T> —this view.
applyWith()
Sets new values for the elements in the view.
public
applyWith(array<string|int, U>|ArrayViewInterface<string|int, U> $data, callable(T, U, int): T $mapper) : ArrayView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]
$data = [9, 27, 45, 63, 81];
$subview->applyWith($data, fn ($lhs, $rhs) => $lhs + $rhs);
$subview->toArray(); // [10, 30, 50, 70, 90]
$source; // [10, 2, 30, 4, 50, 6, 70, 8, 90, 10]
Parameters
- $data : array<string|int, U>|ArrayViewInterface<string|int, U>
- $mapper : callable(T, U, int): T
Tags
Return values
ArrayView<string|int, T> —this view.
filter()
Filters the elements in the view based on a predicate function.
public
filter(callable(T, int): bool $predicate) : ArrayMaskView<string|int, T>
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);
$filtered = $view->filter(fn ($x) => $x % 2 === 0);
$filtered->toArray(); // [2, 4, 6]
$filtered[':'] = [20, 40, 60];
$filtered->toArray(); // [20, 40, 60]
$source; // [1, 20, 3, 40, 5, 60]
Parameters
- $predicate : callable(T, int): bool
-
Function that returns a boolean value for each element.
Return values
ArrayMaskView<string|int, T> —A new view with elements that satisfy the predicate.
is()
Checks if all elements in the view satisfy a given predicate function.
public
is(callable(T, int): bool $predicate) : MaskSelector
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);
$mask = $view->is(fn ($x) => $x % 2 === 0);
$mask->getValue(); // [false, true, false, true, false, true]
$view->subview($mask)->toArray(); // [2, 4, 6]
$view[$mask]; // [2, 4, 6]
$view[$mask] = [20, 40, 60];
$source; // [1, 20, 3, 40, 5, 60]
Parameters
- $predicate : callable(T, int): bool
-
Function that returns a boolean value for each element.
Tags
Return values
MaskSelector —Boolean mask for selecting elements that satisfy the predicate.
map()
Transforms each element of the array using the given callback function.
public
map(callable(T, int): T $mapper) : array<string|int, T>
The callback function receives two parameters: the current element of the array and its index.
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]
$subview->map(fn ($x) => $x * 10); // [10, 30, 50, 70, 90]
Parameters
- $mapper : callable(T, int): T
-
Function to transform each element.
Return values
array<string|int, T> —New array with transformed elements of this view.
mapWith()
Transforms each pair of elements from the current array view and the provided data array using the given callback function.
public
mapWith(array<string|int, U>|ArrayViewInterface<string|int, U>|U $data, callable(T, U, int): T $mapper) : array<string|int, mixed>
The callback function receives three parameters: the current element of the current array view, the corresponding element of the data array, and the index.
Example
$source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$subview = ArrayView::toView($source)->subview('::2'); // [1, 3, 5, 7, 9]
$data = [9, 27, 45, 63, 81];
$subview->mapWith($data, fn ($lhs, $rhs) => $lhs + $rhs); // [10, 30, 50, 70, 90]
Parameters
- $data : array<string|int, U>|ArrayViewInterface<string|int, U>|U
-
The rhs values for a binary operation.
- $mapper : callable(T, U, int): T
-
Function to transform each pair of elements.
Tags
Return values
array<string|int, mixed> —New array with transformed elements of this view.
match()
Checks if all elements in the view satisfy a given predicate function.
public
match(callable(T, int): bool $predicate) : MaskSelector
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);
$mask = $view->match(fn ($x) => $x % 2 === 0);
$mask->getValue(); // [false, true, false, true, false, true]
$view->subview($mask)->toArray(); // [2, 4, 6]
$view[$mask]; // [2, 4, 6]
$view[$mask] = [20, 40, 60];
$source; // [1, 20, 3, 40, 5, 60]
Parameters
- $predicate : callable(T, int): bool
-
Function that returns a boolean value for each element.
Tags
Return values
MaskSelector —Boolean mask for selecting elements that satisfy the predicate.
matchWith()
Compares the elements of the current ArrayView instance with another array or ArrayView using the provided comparator function.
public
matchWith(array<string|int, U>|ArrayViewInterface<string|int, U>|U $data, callable(T, U, int): bool $comparator) : MaskSelectorInterface
Example
$source = [1, 2, 3, 4, 5, 6];
$view = ArrayView::toView($source);
$data = [6, 5, 4, 3, 2, 1];
$mask = $view->matchWith($data, fn ($lhs, $rhs) => $lhs > $rhs);
$mask->getValue(); // [false, false, false, true, true, true]
$view->subview($mask)->toArray(); // [4, 5, 6]
$view[$mask]; // [4, 5, 6]
$view[$mask] = [40, 50, 60];
$source; // [1, 2, 3, 40, 50, 60]
Parameters
- $data : array<string|int, U>|ArrayViewInterface<string|int, U>|U
-
The array or ArrayView to compare to.
- $comparator : callable(T, U, int): bool
-
Function that determines the comparison logic between the elements.
Tags
Return values
MaskSelectorInterface —A MaskSelector instance representing the results of the element comparisons.