Documentation

SliceSelector extends Slice
in package
implements ArraySelectorInterface

FinalYes

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 Slice instance with optional start, end, and step values.
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 Slice instance with optional start, end, and step values.

public __construct([int|null $start = null ][, int|null $end = null ][, int|null $step = null ]) : mixed
Parameters
$start : int|null = null

The start index of the slice range.

$end : int|null = null

The end index of the slice range.

$step : int|null = null

The step size for selecting elements in the slice range.

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
template

T View elements type.

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|null

getStart()

Getter for the start index of the normalized slice.

public getStart() : int|null
Return values
int|null

getStep()

Getter for the step of the normalized slice.

public getStep() : int|null
Return values
int|null

getValue()

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
throws
IndexError

if the step value is 0.

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
template

T The type of elements in the source array.

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
throws
ValueError

if the slice representation is invalid.

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.


        
On this page

Search results