class Set extends AbstractSet (View source)

A set is a collection that contains no duplicate elements.

Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set.

Example usage:

$foo = new \My\Foo();
$set = new Set(\My\Foo::class);

$set->add($foo); // returns TRUE, the element don't exists
$set->add($foo); // returns FALSE, the element already exists

$bar = new \My\Foo();
$set->add($bar); // returns TRUE, $bar !== $foo

Traits

Provides functionality to check values for specific types.
Provides functionality to express a value as string
Provides functionality to extract the value of a property or method from an object.

Properties

protected array $data The items of this array. from AbstractArray

Methods

__construct(string $setType, array $data = [])

Constructs a set object of the specified type, optionally with the specified data.

getIterator()

Returns an iterator for this array.

bool
offsetExists(mixed $offset)

Returns true if the given offset exists in this array.

mixed|null
offsetGet(mixed $offset)

Returns the value at the specified offset.

void
offsetSet(mixed|null $offset, mixed $value)

Sets the given value to the given offset in this set, if it is not already present.

void
offsetUnset(mixed $offset)

Removes the given offset and its value from the array.

string
serialize()

Returns a serialized string representation of this array object.

void
unserialize(string $serialized)

Converts a serialized string representation into an instance object.

int
count()

Returns the number of items in this array.

void
clear()

Removes all items from this array.

array
toArray()

Returns a native PHP array representation of this array object.

bool
isEmpty()

Returns true if this array is empty.

bool
checkType(string $type, mixed $value)

Returns true if value is of the specified type.

from TypeTrait
string
toolValueToString(mixed $value)

Returns a string representation of the value.

mixed
extractValue(object $object, string $propertyOrMethod)

Extracts the value of the given property or method from the object.

bool
add(mixed $element)

Adds the specified element to this set, if it is not already present.

bool
contains(mixed $element, bool $strict = true)

Returns true if this collection contains the specified element.

bool
remove(mixed $element)

Removes a single instance of the specified element from this collection, if it is present.

array
column(string $propertyOrMethod)

Returns the values from given property or method.

mixed
first()

Returns the first item of the collection.

mixed
last()

Returns the last item of the collection.

sort(string $propertyOrMethod, string $order = self::SORT_ASC)

Returns a sorted collection.

filter(callable $callback)

Returns a filtered collection.

where(string $propertyOrMethod, mixed $value)

Returns a collection of matching items.

map(callable $callback)

Applies a callback to each item of the collection.

diff(CollectionInterface $other)

Create a new collection with divergent items between current and given collection.

intersect(CollectionInterface $other)

Create a new collection with intersecting item between current and given collection.

merge(CollectionInterface ...$collections)

Merge current items and items of given collections into a new one.

string
getType()

Returns the type associated with this set.

Details

__construct(string $setType, array $data = [])

Constructs a set object of the specified type, optionally with the specified data.

Parameters

string $setType The type (FQCN) associated with this set.
array $data The initial items to add to this array.

Traversable getIterator()

Returns an iterator for this array.

Return Value

Traversable

bool offsetExists(mixed $offset)

Returns true if the given offset exists in this array.

Parameters

mixed $offset The offset to check.

Return Value

bool

mixed|null offsetGet(mixed $offset)

Returns the value at the specified offset.

Parameters

mixed $offset The offset for which a value should be returned.

Return Value

mixed|null the value stored at the offset, or null if the offset does not exist.

void offsetSet(mixed|null $offset, mixed $value)

Sets the given value to the given offset in this set, if it is not already present.

Parameters

mixed|null $offset The offset to set. If null, the value may be set at a numerically-indexed offset.
mixed $value The value to set at the given offset.

Return Value

void

Exceptions

InvalidArgumentException when the value does not match the specified type for this set.

void offsetUnset(mixed $offset)

Removes the given offset and its value from the array.

Parameters

mixed $offset The offset to remove from the array.

Return Value

void

string serialize()

Returns a serialized string representation of this array object.

Return Value

string a PHP serialized string.

void unserialize(string $serialized)

Converts a serialized string representation into an instance object.

Parameters

string $serialized A PHP serialized string to unserialize.

Return Value

void

int count()

Returns the number of items in this array.

Return Value

int

void clear()

Removes all items from this array.

Return Value

void

array toArray()

Returns a native PHP array representation of this array object.

Return Value

array

bool isEmpty()

Returns true if this array is empty.

Return Value

bool

protected bool checkType(string $type, mixed $value)

Returns true if value is of the specified type.

Parameters

string $type The type to check the value against.
mixed $value The value to check.

Return Value

bool

protected string toolValueToString(mixed $value)

Returns a string representation of the value.

  • null value: 'NULL'
  • boolean: 'TRUE', 'FALSE'
  • array: 'Array'
  • scalar: converted-value
  • resource: '(type resource #number)'
  • object with __toString(): result of __toString()
  • object DateTime: ISO 8601 date
  • object: '(className Object)'
  • anonymous function: same as object

Parameters

mixed $value the value to return as a string.

Return Value

string

protected mixed extractValue(object $object, string $propertyOrMethod)

Extracts the value of the given property or method from the object.

Parameters

object $object The object to extract the value from.
string $propertyOrMethod The property or method for which the value should be extracted.

Return Value

mixed the value extracted from the specified property or method.

Exceptions

ValueExtractionException if the method or property is not defined.

bool add(mixed $element)

Adds the specified element to this set, if it is not already present.

Parameters

mixed $element The element to add to the collection.

Return Value

bool true if this collection changed as a result of the call.

Exceptions

InvalidArgumentException when the element does not match the specified type for this set.

bool contains(mixed $element, bool $strict = true)

Returns true if this collection contains the specified element.

Parameters

mixed $element The element to check whether the collection contains.
bool $strict Whether to perform a strict type check on the value.

Return Value

bool

bool remove(mixed $element)

Removes a single instance of the specified element from this collection, if it is present.

Parameters

mixed $element The element to remove from the collection.

Return Value

bool true if an element was removed as a result of this call.

array column(string $propertyOrMethod)

Returns the values from given property or method.

Parameters

string $propertyOrMethod The property or method name to filter by.

Return Value

array

Exceptions

ValueExtractionException if property or method is not defined.

mixed first()

Returns the first item of the collection.

Return Value

mixed

Exceptions

OutOfBoundsException when the collection is empty.

mixed last()

Returns the last item of the collection.

Return Value

mixed

Exceptions

OutOfBoundsException when the collection is empty.

CollectionInterface sort(string $propertyOrMethod, string $order = self::SORT_ASC)

Returns a sorted collection.

{@inheritdoc}

Parameters

string $propertyOrMethod The property or method to sort by.
string $order The sort order for the resulting collection (one of this interface's SORT_* constants).

Return Value

CollectionInterface

Exceptions

InvalidSortOrderException if neither "asc" nor "desc" was given as the order.
ValueExtractionException if property or method is not defined.

CollectionInterface filter(callable $callback)

Returns a filtered collection.

{@inheritdoc}

Parameters

callable $callback A callable to use for filtering elements.

Return Value

CollectionInterface

CollectionInterface where(string $propertyOrMethod, mixed $value)

Returns a collection of matching items.

{@inheritdoc}

Parameters

string $propertyOrMethod The property or method to evaluate.
mixed $value The value to match.

Return Value

CollectionInterface

Exceptions

ValueExtractionException if property or method is not defined.

CollectionInterface map(callable $callback)

Applies a callback to each item of the collection.

{@inheritdoc}

Parameters

callable $callback A callable to apply to each item of the collection.

Return Value

CollectionInterface

CollectionInterface diff(CollectionInterface $other)

Create a new collection with divergent items between current and given collection.

Parameters

CollectionInterface $other The collection to check for divergent items.

Return Value

CollectionInterface

Exceptions

CollectionMismatchException if the given collection is not of the same type.

CollectionInterface intersect(CollectionInterface $other)

Create a new collection with intersecting item between current and given collection.

Parameters

CollectionInterface $other The collection to check for intersecting items.

Return Value

CollectionInterface

Exceptions

CollectionMismatchException if the given collection is not of the same type.

CollectionInterface merge(CollectionInterface ...$collections)

Merge current items and items of given collections into a new one.

Parameters

CollectionInterface ...$collections The collections to merge.

Return Value

CollectionInterface

Exceptions

CollectionMismatchException if any of the given collections are not of the same type.

string getType()

Returns the type associated with this set.

Return Value

string