SliceSelector
extends Slice
in package
implements
ArraySelectorInterface
Represents a slice selector that selects elements based on the provided slice parameters.
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$view = ArrayView::toView($originalArray);
$selector = new SliceSelector('::2');
print_r($view[$selector]); // [1, 3, 5, 7, 9]
print_r($view->subview($selector)->toArray()); // [1, 3, 5, 7, 9]
$selector = new SliceSelector('1::2');
print_r($view[$selector]); // [2, 4, 6, 8, 10]
print_r($view->subview($selector)->toArray()); // [2, 4, 6, 8, 10]
$selector = new SliceSelector('-3::-2');
print_r($view[$selector]); // [8, 6, 4, 2]
print_r($view->subview($selector)->toArray()); // [8, 6, 4, 2]
$selector = new SliceSelector('1:4');
print_r($view[$selector]); // [2, 3, 4]
print_r($view->subview($selector)->toArray()); // [2, 3, 4]
$selector = new SliceSelector('-2:0:-1');
print_r($view[$selector]); // [9, 8, 7, 6, 5, 4, 3, 2]
print_r($view->subview($selector)->toArray()); // [9, 8, 7, 6, 5, 4, 3, 2]
Table of Contents
Interfaces
- ArraySelectorInterface
- Interface for selecting elements from an array view.
Methods
- __construct() : mixed
- Creates a new SliceSelector instance with the provided slice parameters.
- compatibleWith() : bool
- Checks if the selector is compatible with the given view.
- getEnd() : int|null
- Getter for the stop index of the normalized slice.
- getStart() : int|null
- Getter for the start index of the normalized slice.
- getStep() : int|null
- Getter for the step of the normalized slice.
- getValue() : mixed
- Return value of the selector.
- isSlice() : bool
- Checks if the provided value is a Slice instance or a valid slice string.
- isSliceArray() : bool
- Checks if the provided value is a valid slice array.
- isSliceString() : bool
- Checks if the provided value is a valid slice string.
- normalize() : NormalizedSlice
- Normalizes the slice parameters based on the container length.
- select() : ArraySliceView<string|int, T>
- Selects elements from the source array based on the slice parameters.
- toSlice() : Slice
- Converts a slice string or Slice object into a Slice instance.
- toString() : string
- Returns the string representation of the Slice.
Methods
__construct()
Creates a new SliceSelector instance with the provided slice parameters.
public
__construct(Slice|string|array<string|int, int> $slice) : mixed
Parameters
- $slice : Slice|string|array<string|int, int>
-
The slice instance or slice string defining the selection.
compatibleWith()
Checks if the selector is compatible with the given view.
public
compatibleWith(ArrayViewInterface<string|int, T> $view) : bool
Parameters
- $view : ArrayViewInterface<string|int, T>
-
the view to check compatibility with.
Tags
Return values
bool —true if the element is compatible, false otherwise
getEnd()
Getter for the stop index of the normalized slice.
public
getEnd() : int|null
Return values
int|nullgetStart()
Getter for the start index of the normalized slice.
public
getStart() : int|null
Return values
int|nullgetStep()
Getter for the step of the normalized slice.
public
getStep() : int|null
Return values
int|nullgetValue()
Return value of the selector.
public
getValue() : mixed
isSlice()
Checks if the provided value is a Slice instance or a valid slice string.
public
static isSlice(mixed $s) : bool
Parameters
- $s : mixed
-
The value to check.
Return values
bool —True if the value is a Slice instance or a valid slice string, false otherwise.
isSliceArray()
Checks if the provided value is a valid slice array.
public
static isSliceArray(mixed $s) : bool
Parameters
- $s : mixed
-
The value to check.
Return values
bool —True if the value is a valid slice array, false otherwise.
isSliceString()
Checks if the provided value is a valid slice string.
public
static isSliceString(mixed $s) : bool
Parameters
- $s : mixed
-
The value to check.
Return values
bool —True if the value is a valid slice string, false otherwise.
normalize()
Normalizes the slice parameters based on the container length.
public
normalize(int $containerSize) : NormalizedSlice
Parameters
- $containerSize : int
-
The length of the container or array.
Tags
Return values
NormalizedSlice —The normalized slice parameters.
select()
Selects elements from the source array based on the slice parameters.
public
select(ArrayViewInterface<string|int, T> $source[, bool|null $readonly = null ]) : ArraySliceView<string|int, T>
Parameters
- $source : ArrayViewInterface<string|int, T>
-
The source array to select elements from.
- $readonly : bool|null = null
-
Whether the selection should be read-only.
Tags
Return values
ArraySliceView<string|int, T> —The view containing the selected elements.
toSlice()
Converts a slice string or Slice object into a Slice instance.
public
static toSlice(string|Slice|array<string|int, int> $s) : Slice
Parameters
- $s : string|Slice|array<string|int, int>
-
The slice string/array or Slice object to convert.
Tags
Return values
Slice —The converted Slice instance.
toString()
Returns the string representation of the Slice.
public
toString() : string
Return values
string —The string representation of the Slice.