Array properties

From Sunhill Framework Documentation

Array properties are properties that are accessed like a php array via an index. Array properties are derived from ArrayProperty (or MapProperty) which in turn is a child of AbstractProperty and therefor inherits all its properties and methods. But there are some array specific topics:

Accessing an element

Like with any array one can access an element via [$index].

Example:

echo $array_property[0]; // prints the first element (when it exists)

Adding an element

A new element is added via the the [] operator.

Example:

$array_property[] = 5; // appends the element 5 to the array.

Replacing an element

A replacement of an element works the same as with php arrays

Example:

$array_property[0] = 10; // Sets the first element to 10

Unseting an element

To unset an element call the unset() method

Example:

$array_property->unset(0); // Unsets the first element

Indextypes and Indices

As every array there is always a index => value pair. The allowed types for value are described later. The index can be

  • Integer
  • String

Per default 'integer' is expected as an index type.

setIndexType(string $index_type): static

$index_type may only be 'integer' or 'string'.

getIndexType(): string

Returns the current set index type.

indexExists(mixed $index): bool

Returns if the given index exists in this array.

Allowed element types

As an array could not take any element type as an value. An array of integers couldn't accept a string element. Therefor array properties provide the following methods:

setAllowedElementTypes($type_or_types): self

Sets the allowed element. The parameter could be a single string, a class or an array of strings.

Example:

setAllowedElementTypes(TypeString::class); // Only strings are allowed as an element
setAllowedElementTypes($string_property); // The same as above: Only string are allowed
setAllowedElementTypes([TypeString::class, TypeInteger::class]); // Only strings or integers are accepted as an element type

Note: If you call this method repeatedly all given elements are merged together. So

setAllowedElementTypes(TypeString::class);
setAllowedElementTypes(TypeInteger::class);

is the same as

setAllowedAElementTypes([TypeString::class, TypeInteger::class]);

getAllowedElementTypes(): array

Returns an array of types that are allowed as an element type for this property.

Note: An array is even then returned if only one element exists

Empty array and null

There is a difference between an empty array and a null array. While one can access the function count() on an empty array (but of cause no elements) it is not possible for a null array. A new element can be assigned to both.

Example:

$array_property->count(); // would be 0 for an empty array and raises an exception for a null array
$array_property[] = 12; // Would append the element '5' to an empty array and create an array and add a first element to a null array

Assigning other array like structures as value

While calling setValue() with a scalar would raise an excpetion it is possible to assign an array like structure via setValue().

Example:

$array_property->setValue([1,2,3]); // Replaces the values of the current array with 1,2,3
$array_property->setValue(collect([1,2,3])); // Does the same
$array_property->setValue($another_array_property); // Is possible too