API Models Collections UsersInterface - evansims/openfga-php GitHub Wiki

Represents a collection of users in authorization contexts. This collection manages users who have been granted access through various authorization mechanisms. Users can be individual identities, usersets, or wildcard patterns depending on the authorization model configuration.

Table of Contents


Namespace

OpenFGA\Models\Collections


Source

View source code


Implements


Related Classes


Methods

add

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.

View source


Parameters

Name Type Description
$item T The OpenFGA model object to add to the collection

Returns

static


clear

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.

View source


Returns

void


count

public function count(): int<0, max>

View source


Returns

int&lt;0, max&gt;


current

public function current(): T

View source


Returns

T


every

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.

View source


Parameters

Name Type Description
$callback callable

Returns

bool — True if all items match the condition, false otherwise


filter

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.

View source


Parameters

Name Type Description
$callback callable

Returns

static — A new collection containing only the matching items


first

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.

View source


Parameters

Name Type Description
$callback callable | null

Returns

T | null — The first matching item, or null if none found


get

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.

View source


Parameters

Name Type Description
$offset int The index position of the item to retrieve

Returns

T | null — The item at the specified position, or null if not found


isEmpty

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.

View source


Returns

bool — True if the collection is empty, false otherwise


jsonSerialize

public function jsonSerialize(): array<int, array{object?: mixed, userset?: array{type: string, id: string, relation: string}, wildcard?: array{type: string}}>

View source


Returns

array&lt;int, array{object?: mixed, userset?: array{type: string, id: string, relation: string}, wildcard?: array{type: string}}&gt;


key

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.

View source


Returns

int — The current iterator position


next

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.

View source


Returns

void


offsetExists

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.

View source


Parameters

Name Type Description
$offset mixed The offset to check for existence

Returns

bool — True if the offset exists, false otherwise


offsetGet

public function offsetGet(mixed $offset): T|null

View source


Parameters

Name Type Description
$offset mixed

Returns

T | null


offsetSet

public function offsetSet(int|string|null $offset, T $value): void

View source


Parameters

Name Type Description
$offset int | string | null
$value T

Returns

void


offsetUnset

public function offsetUnset(mixed $offset): void

View source


Parameters

Name Type Description
$offset mixed

Returns

void


reduce

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.

View source


Parameters

Name Type Description
$initial U The initial value to start the reduction
$callback callable

Returns

U — The final accumulated value


rewind

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.

View source


Returns

void


some

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.

View source


Parameters

Name Type Description
$callback callable

Returns

bool — True if any item matches the condition, false otherwise


toArray

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.

View source


Returns

array&lt;int | string, T&gt; — A standard PHP array containing all collection items


valid

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.

View source


Returns

bool — True if the current position is valid, false otherwise


withItems

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.

View source


Parameters

Name Type Description
$items mixed

Returns

static — A new collection instance containing the specified items

⚠️ **GitHub.com Fallback** ⚠️