0 follower

Trait Yiisoft\Arrays\ArrayAccessTrait

ArrayAccessTrait provides the implementation for {@see \IteratorAggregate}, {@see \ArrayAccess} and {@see \Countable}.

Note that ArrayAccessTrait requires the class using it contain a property named data which should be an array. The data will be exposed by ArrayAccessTrait to support accessing the class object like an array.

Public Properties

Hide inherited properties

Property Type Description Defined By
$data array Yiisoft\Arrays\ArrayAccessTrait

Public Methods

Hide inherited methods

Method Description Defined By
count() Returns the number of data items. Yiisoft\Arrays\ArrayAccessTrait
getIterator() Returns an iterator for traversing the data. Yiisoft\Arrays\ArrayAccessTrait
offsetExists() This method is required by the interface {@see \ArrayAccess}. Yiisoft\Arrays\ArrayAccessTrait
offsetGet() This method is required by the interface {@see \ArrayAccess}. Yiisoft\Arrays\ArrayAccessTrait
offsetSet() This method is required by the interface {@see \ArrayAccess}. Yiisoft\Arrays\ArrayAccessTrait
offsetUnset() This method is required by the interface {@see \ArrayAccess}. Yiisoft\Arrays\ArrayAccessTrait

Property Details

Hide inherited properties

$data public property
public array $data null

Method Details

Hide inherited methods

count() public method

Returns the number of data items.

This method is required by Countable interface.

public integer count ( )
return integer

Number of data elements.

                public function count(): int
{
    return count($this->data);
}

            
getIterator() public method

Returns an iterator for traversing the data.

This method is required by the SPL interface {@see \IteratorAggregate}. It will be implicitly called when you use foreach to traverse the collection.

public ArrayIterator getIterator ( )
return ArrayIterator

An iterator for traversing the cookies in the collection.

                public function getIterator(): ArrayIterator
{
    return new ArrayIterator($this->data);
}

            
offsetExists() public method

This method is required by the interface {@see \ArrayAccess}.

public boolean offsetExists ( mixed $offset )
$offset mixed

The offset to check on.

                public function offsetExists(mixed $offset): bool
{
    return isset($this->data[$offset]);
}

            
offsetGet() public method

This method is required by the interface {@see \ArrayAccess}.

public mixed offsetGet ( mixed $offset )
$offset mixed

The offset to retrieve element.

return mixed

The element at the offset, null if no element is found at the offset.

                public function offsetGet(mixed $offset): mixed
{
    return $this->data[$offset] ?? null;
}

            
offsetSet() public method

This method is required by the interface {@see \ArrayAccess}.

public void offsetSet ( mixed $offset, mixed $value )
$offset mixed

The offset to set element.

$value mixed

The element value.

                public function offsetSet(mixed $offset, mixed $value): void
{
    if ($offset === null) {
        $this->data[] = $value;
    } else {
        $this->data[$offset] = $value;
    }
}

            
offsetUnset() public method

This method is required by the interface {@see \ArrayAccess}.

public void offsetUnset ( mixed $offset )
$offset mixed

The offset to unset element.

                public function offsetUnset(mixed $offset): void
{
    unset($this->data[$offset]);
}