Class \Prado\Collections\TArraySubscription
Given an array reference or ArrayAccess object, this class adds an item to an array on self::subscribe() and removes the element of the array when the instance is dereferenced or self::unsubscribe() is called.
These are specific types that can be subscribed: PHP Array, Object implementing ArrayAccess, TList and subclasses, and TMap and subclasses. A priority can be specified for IPriorityCollection instances.
When an array is associative, the original element is saved and restored (with its original priority). IWeakCollections retain their elements in their weakened "storage" state.
$map = new TPriorityMap(['key' => 'original value']);
$subscription = new TArraySubscription($map, 'key', 'new value', priority: 5);
$subscription->getIsSubscribed() === true;
$map['key'] === 'new value'; // @ priority = 5
$subscription->unsubscribe();
$subscription->getIsSubscribed() === false;
$map['key'] === 'original value'; // @ priority = default (10)
When the TArraySubscription is dereferenced, the item is also automatically unsubscribed.
{
$list = [];
{
$subscription = new TArraySubscription($list, null, 'subscribed item', isAssociative: false);
$list[0] === 'subscribed item';
array_splice($list, 0, 0, ['first item']);
$list[] = 'last item'; // ['first item', 'subscribed item', 'last item']
...
} // $subscription unsubscribes here, out of scope
$list[0] === 'first item';
$list[1] === 'last item'; // count = 2
}
Class hierarchy
Author: Brad Anderson <belisoful@icloud.com>Since: 4.3.0
public
|
__construct([mixed &$array = null ][, mixed $key = null ][, mixed $item = null ][, null|float|int $priority = null ][, null|bool|int $isAssociative = 1 ][, bool $autoSubscribe = null ]) : mixed
Constructs the TArraySubscription. If there is an key or item then the item is
automatically subscribed to the array when $autoSubscribe is the default null.
When $autoSubscribe = true, the item is added regardless of a key and/or item has a value. |
public
|
__destruct() : mixed
Cleans up the instance on destruction.
If the item is subscribed to the array, the item is removed. |
public
|
getArray([bool $weak = false ]) : array<string|int, mixed>|ArrayAccess
Returns the collection to which the item is subscribed.
Be very careful with the returnad array as it is passed by reference and could change the original variable to something other than an array.
|
public
|
getIsAssociative() : null|bool
Whether to add to the array by association or by splicing (for an ordered list).
|
public
|
|
public
|
|
public
|
getKey() : null|int|string
If the item is subscribed and the key is null, the item key will be discovered
with the TList (by {@see \Prado\Collections\TList::indexOf()}) or array (by array_search).
|
public
|
setArray(null|array<string|int, mixed>|ArrayAccess &$value) : static
Sets the array or ArrayAccess object.
|
public
|
|
public
|
|
public
|
|
public
|
setPriority(numeric-string|int|float|null $value) : static
This is on applicable to {@see \Prado\Collections\IPriorityCollection}.
|
public
|
subscribe() : bool|null
Places the item in the array at the key (and priority, where possible).
List based ArrayAccess must also implement \Countable as well. |
public
|
unsubscribe() : bool|null
Removes the item from the array at the key (and priority, where possible).
|
getPriority, setPriority, _priorityItemZappableSleepProps |