class Vector extends BaseVector

A generic vector implementation to accept any kind of value.

Vector offers specialized methods to convert from and to int/string.

This class is intended to be used in every case a more specialized vector implementation doesn't exist or isn't needed, ie every time an array is needed, to contains ordered values, without string keys.

Traits

Constants

CB_ZERO_ARG

Properties

protected array $items from  BaseVector

Methods

int
count()

No description

array
toArray()

No description

mixed
first()

No description

mixed
firstOr(mixed $default)

No description

bool
any(callable $callable)

Determines if at least an element of the collection satisfies a condition.

bool
all(callable $callable)

Determines if all elements of the collection satisfies a condition.

static BaseCollection
from(iterable $items)

Create a new collection instance from the specified iterable.

bool
isEmpty()

Determine if a collection is empty.

__construct(iterable $items = [])

No description

mixed
get(int $key)

No description

mixed
getOr(int $key, mixed $defaultValue)

No description

set(int $key, mixed $value)

No description

unset(int $key)

No description

bool
contains(mixed $value)

No description

clear()

No description

push(mixed $item)

No description

append(iterable $iterable)

Append all elements of the specified iterable to the current vector.

update(iterable $iterable)

Append all elements of the specified iterable to the current vector.

replace(iterable $iterable, int $offset = 0, int $len = 0)

Replace a part of the vector by the specified iterable.

map(callable $callable)

Create a new vector by applying a callback on each element of the vector.

filter(callable $callable)

Create a new vector by filtering each element of the vector according the return value of the specified callback function.

mapKeys(callable $callable)

No description

mapToHashMap(callable $callable)

Allows to map each vector elements into key/value pairs and collect them into a HashMap.

flatMap(callable $callable)

No description

filterKeys(callable $callable)

No description

chunk(int $length)

No description

slice(int $offset, int $length)

No description

implode(string $delimiter)

No description

bigrams()

No description

trigrams()

No description

ngrams(int $n)

No description

bool
offsetExists(mixed $offset)

No description

mixed
offsetGet(mixed $offset)

No description

void
offsetSet(mixed $offset, mixed $value)

No description

void
offsetUnset(mixed $offset)

No description

getIterator()

Retrieve an iterator on the elements of the vector.

static Vector
explode(string $delimiter, string $string, int $limit = PHP_INT_MAX)

Constructs a new instance of a vector by exploding a string according a specified delimiter.

static Vector
range(int $start, int $end, int $step = 1)

No description

toIntegers()

No description

Details

in WithCollection at line 12
abstract int count()

No description

Return Value

int

in WithCollection at line 13
abstract array toArray()

No description

Return Value

array

in WithCollection at line 15
mixed first()

No description

Return Value

mixed

in WithCollection at line 23
mixed firstOr(mixed $default)

No description

Parameters

mixed $default

Return Value

mixed

in WithCollection at line 45
bool any(callable $callable)

Determines if at least an element of the collection satisfies a condition.

The execution of callbacks stop after a callable returned true.

Parameters

callable $callable

A method returning a boolean with key and value or only value as arguments.

Return Value

bool

True if callback is true for at least one of the elements

Exceptions

ReflectionException

in WithCollection at line 78
bool all(callable $callable)

Determines if all elements of the collection satisfies a condition.

The execution of callbacks stop after a callable returned false.

Parameters

callable $callable

A method returning a boolean with key and value or only value as arguments.

Return Value

bool

True if callback is true for all the elements

Exceptions

ReflectionException

in BaseVector at line 45
static BaseCollection from(iterable $items)

Create a new collection instance from the specified iterable.

Parameters

iterable $items

The elements to put in the new collection

Return Value

BaseCollection

The collection with the elements from the iterable

in BaseVector at line 95
bool isEmpty()

Determine if a collection is empty.

Return Value

bool

true if the collection has 0 element; if not, false.

in BaseVector at line 34
__construct(iterable $items = [])

No description

Parameters

iterable $items

in BaseVector at line 53
mixed get(int $key)

No description

Parameters

int $key

Return Value

mixed

in BaseVector at line 61
mixed getOr(int $key, mixed $defaultValue)

No description

Parameters

int $key
mixed $defaultValue

Return Value

mixed

in BaseVector at line 65
BaseVector set(int $key, mixed $value)

No description

Parameters

int $key
mixed $value

Return Value

BaseVector

in BaseVector at line 71
BaseVector unset(int $key)

No description

Parameters

int $key

Return Value

BaseVector

in BaseVector at line 77
bool contains(mixed $value)

No description

Parameters

mixed $value

Return Value

bool

in BaseVector at line 99
BaseVector clear()

No description

Return Value

BaseVector

in BaseVector at line 105
BaseVector push(mixed $item)

No description

Parameters

mixed $item

Return Value

BaseVector

in BaseVector at line 122
BaseVector append(iterable $iterable)

Append all elements of the specified iterable to the current vector.

If a value already exists, the value is still added as a duplicate.

This method returns the instance itself, so you can use it in fluent pattern.

Parameters

iterable $iterable

Return Value

BaseVector

See also

BaseVector::update when you need to only add unique values

in BaseVector at line 140
BaseVector update(iterable $iterable)

Append all elements of the specified iterable to the current vector.

If a value already exists, it is skipped.

This method returns the instance itself, so you can use it in fluent pattern.

Parameters

iterable $iterable

Return Value

BaseVector

See also

BaseVector::append when you need to always add everything

in BaseVector at line 159
BaseVector replace(iterable $iterable, int $offset = 0, int $len = 0)

Replace a part of the vector by the specified iterable.

This method returns the instance itself, so you can use it in fluent pattern.

Parameters

iterable $iterable
int $offset

Allow to replace a part inside the vector by an iterable with keys starting at 0, by adding the specified offset

int $len

The maximum amount of elements to read. If 0, the read isn't bounded

Return Value

BaseVector

in BaseVector at line 198
BaseVector map(callable $callable)

Create a new vector by applying a callback on each element of the vector.

Parameters

callable $callable

The callback function to run for each element.

Return Value

BaseVector

A new map with the mapped elements.

in BaseVector at line 226
BaseVector filter(callable $callable)

Create a new vector by filtering each element of the vector according the return value of the specified callback function.

Example:

$words = new Vector(["foo", "bar", "quux", "xizzy"]);

$threeLettersWords = $words->filter(
    fn(string $word) => strlen($word) === 3
);

// The new vector contains "foo" and "bar".

Parameters

callable $callable

A callback function, which returns a boolean. When true, the element is kept. When false, the element is discarded.

Return Value

BaseVector

A new map with filtered elements

Exceptions

ReflectionException
InvalidArgumentException

in BaseVector at line 242
BaseVector mapKeys(callable $callable)

No description

Parameters

callable $callable

Return Value

BaseVector

in BaseVector at line 259
HashMap mapToHashMap(callable $callable)

Allows to map each vector elements into key/value pairs and collect them into a HashMap.

Parameters

callable $callable

A method to return [$key, $value] array

Return Value

HashMap

in BaseVector at line 282
BaseVector flatMap(callable $callable)

No description

Parameters

callable $callable

Return Value

BaseVector

in BaseVector at line 298
BaseVector filterKeys(callable $callable)

No description

Parameters

callable $callable

Return Value

BaseVector

in BaseVector at line 304
Vector chunk(int $length)

No description

Parameters

int $length

Return Value

Vector

in BaseVector at line 308
BaseVector slice(int $offset, int $length)

No description

Parameters

int $offset
int $length

Return Value

BaseVector

in BaseVector at line 313
OmniString implode(string $delimiter)

No description

Parameters

string $delimiter

Return Value

OmniString

in BaseVector at line 317
Vector bigrams()

No description

Return Value

Vector

in BaseVector at line 321
Vector trigrams()

No description

Return Value

Vector

in BaseVector at line 325
Vector ngrams(int $n)

No description

Parameters

int $n

Return Value

Vector

in BaseVector at line 361
bool offsetExists(mixed $offset)

No description

Parameters

mixed $offset

Return Value

bool

in BaseVector at line 367
mixed offsetGet(mixed $offset)

No description

Parameters

mixed $offset

Return Value

mixed

in BaseVector at line 373
void offsetSet(mixed $offset, mixed $value)

No description

Parameters

mixed $offset
mixed $value

Return Value

void

in BaseVector at line 384
void offsetUnset(mixed $offset)

No description

Parameters

mixed $offset

Return Value

void

in BaseVector at line 402
ArrayIterator getIterator()

Retrieve an iterator on the elements of the vector.

This iterator also allows unsetting and modifying values and keys while iterating.

Return Value

ArrayIterator

at line 32
static Vector explode(string $delimiter, string $string, int $limit = PHP_INT_MAX)

Constructs a new instance of a vector by exploding a string according a specified delimiter.

Parameters

string $delimiter

The substring to find for explosion

string $string

The string to explode

int $limit

If specified, the maximum count of vector elements

Return Value

Vector

at line 49
static Vector range(int $start, int $end, int $step = 1)

No description

Parameters

int $start
int $end
int $step

Return Value

Vector

at line 57
Vector toIntegers()

No description

Return Value

Vector