API Models Collections TypeDefinitions - evansims/openfga-php GitHub Wiki
Collection implementation for OpenFGA type definition objects. This class provides a concrete implementation for managing collections of type definition objects that specify object types, relations, and metadata within an authorization model. Type definitions are fundamental building blocks that define the schema for authorization relationships.
Table of Contents
OpenFGA\Models\Collections
IndexedCollectionInterfaceTraversableJsonSerializableIteratorCountableArrayAccessTypeDefinitionsInterface
- TypeDefinitionsInterface (interface)
public function add(mixed $item): staticAdd 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 |
mixed | The OpenFGA model object to add to the collection |
static
public function clear(): voidRemove 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(): OpenFGA\Models\ModelInterfacepublic function every(callable $callback): boolCheck 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): staticCreate 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)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
|
public function get(int $offset)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 |
public function isEmpty(): boolCheck 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(): arrayarray
public function key(): intGet 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(): voidMove 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): boolCheck 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): ?OpenFGA\Models\ModelInterface| Name | Type | Description |
|---|---|---|
$offset |
mixed |
ModelInterface | null
public function offsetSet(mixed $offset, mixed $value): void| Name | Type | Description |
|---|---|---|
$offset |
mixed |
|
$value |
mixed |
void
public function offsetUnset(mixed $offset): void| Name | Type | Description |
|---|---|---|
$offset |
mixed |
void
public function reduce(mixed $initial, callable $callback): mixedReduce 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 |
mixed |
The initial value to start the reduction |
$callback |
callable |
mixed — The final accumulated value
public function rewind(): voidReset the iterator to the beginning of the collection. This method moves the internal iterator pointer back to the first item in the collection.
void
Implements Models\Collections\IndexedCollectionInterface
public function schema(): CollectionSchemaInterfaceGet the schema definition for this collection type. Returns the schema that defines the structure, validation rules, and serialization behavior for this collection type. The schema specifies the expected item type, validation constraints, and transformation rules that ensure all items in the collection conform to OpenFGA data requirements. Collection schemas enable: - Type validation for all added items - Consistent serialization across different contexts - API compatibility verification - Runtime type checking and error reporting The schema system ensures that authorization data maintains integrity throughout processing, preventing type mismatches that could lead to authorization failures or security vulnerabilities.
CollectionSchemaInterface — The schema definition containing validation rules and type constraints for this collection
public function some(callable $callback): boolCheck 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(): arrayConvert 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 — A standard PHP array containing all collection items
public function valid(): boolCheck 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): staticCreate 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