API Models Collections ComputedsInterface - evansims/openfga-php GitHub Wiki
Table of Contents
OpenFGA\Models\Collections
IndexedCollectionInterface
Traversable
JsonSerializable
Iterator
Countable
ArrayAccess
- Computeds (implementation)
public function add(T $item): static
Add an item to the end of the collection. This method appends a new model object to the collection, automatically assigning it the next available integer index. The item is validated to ensure it matches the expected type for this collection, maintaining type safety throughout the authorization data processing pipeline. This operation modifies the current collection instance directly, making it suitable for building collections incrementally. For immutable operations, use the withItems()
method instead, which creates new collection instances without modifying the original.
Name | Type | Description |
---|---|---|
$item |
T |
The OpenFGA model object to add to the collection |
static
public function clear(): void
Remove all items from the collection. This method empties the collection, resetting it to its initial state with no items and a count of zero.
void
public function count(): int<0, max>
int<
0,
max>
public function current(): T
T
public function every(callable $callback): bool
Check if all items in the collection match the given condition. This method tests whether all items in the collection satisfy the provided callback function. Returns true if all items pass the test, false if any item fails.
Name | Type | Description |
---|---|---|
$callback |
callable |
bool
— True if all items match the condition, false otherwise
public function filter(callable $callback): static
Create a new collection containing only items that match the condition. This method creates a new collection containing only the items from the current collection that satisfy the provided callback function.
Name | Type | Description |
---|---|---|
$callback |
callable |
static
— A new collection containing only the matching items
public function first(?callable $callback = NULL): T|null
Get the first item in the collection, optionally matching a condition. When called without a callback, returns the first item in the collection. When called with a callback, returns the first item that satisfies the condition.
Name | Type | Description |
---|---|---|
$callback |
callable | null
|
T
| null
— The first matching item, or null if none found
public function get(int $offset): T|null
Get an item by its position in the collection. This method retrieves the item at the specified index position. Returns null if the index is out of bounds.
Name | Type | Description |
---|---|---|
$offset |
int |
The index position of the item to retrieve |
T
| null
— The item at the specified position, or null if not found
public function isEmpty(): bool
Check if the collection contains no items. This method provides a convenient way to test whether the collection is empty without needing to check the count.
bool
— True if the collection is empty, false otherwise
public function jsonSerialize(): array<int, array{userset: string}>
array<
int,
array{userset: string}>
public function key(): int
Get the current iterator key. This method returns the current position in the collection iteration, which is always an integer for indexed collections.
int
— The current iterator position
public function next(): void
Move the iterator to the next position. This method advances the internal iterator pointer to the next item in the collection sequence.
void
public function offsetExists(mixed $offset): bool
Check if an offset exists in the collection. This method determines whether the collection contains an item at the specified offset position.
Name | Type | Description |
---|---|---|
$offset |
mixed |
The offset to check for existence |
bool
— True if the offset exists, false otherwise
public function offsetGet(mixed $offset): T|null
Name | Type | Description |
---|---|---|
$offset |
mixed |
T
| null
public function offsetSet(int|string|null $offset, T $value): void
Name | Type | Description |
---|---|---|
$offset |
int | string | null
|
|
$value |
T |
void
public function offsetUnset(mixed $offset): void
Name | Type | Description |
---|---|---|
$offset |
mixed |
void
public function reduce(U $initial, callable $callback): U
Reduce the collection to a single value using a callback function. This method iteratively applies a callback function to accumulate the collection items into a single value, starting with an initial value.
Name | Type | Description |
---|---|---|
$initial |
U |
The initial value to start the reduction |
$callback |
callable |
U
— The final accumulated value
public function rewind(): void
Reset the iterator to the beginning of the collection. This method moves the internal iterator pointer back to the first item in the collection.
void
public function some(callable $callback): bool
Check if at least one item in the collection matches the given condition. This method tests whether at least one item in the collection satisfies the provided callback function. Returns true if any item passes the test, false if all items fail.
Name | Type | Description |
---|---|---|
$callback |
callable |
bool
— True if any item matches the condition, false otherwise
public function toArray(): array<int|string, T>
Convert the collection to a standard PHP array. This method creates a native PHP array containing all items in the collection, preserving their order and indexes.
array<int
| string, T>
— A standard PHP array containing all collection items
public function valid(): bool
Check if the current iterator position is valid. This method determines whether the current iterator position points to a valid item in the collection.
bool
— True if the current position is valid, false otherwise
public function withItems(mixed $items): static
Create a new collection with the specified items. This method creates a fresh collection instance containing only the provided items, leaving the original collection unchanged.
Name | Type | Description |
---|---|---|
$items |
mixed |
static
— A new collection instance containing the specified items