Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions lib/DirectorySync.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class DirectorySync
*
* @throws Exception\WorkOSException
*
* @return array{?string, ?string, Resource\Directory[]} An array containing the Directory ID to use as before and after cursor, and an array of Directory instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and directories array.
* Supports: [$before, $after, $directories] = $result, ["directories" => $dirs] = $result, $result->directories
*/
public function listDirectories(
?string $domain = null,
Expand Down Expand Up @@ -54,13 +55,7 @@ public function listDirectories(
true
);

$directories = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($directories, Resource\Directory::constructFromResponse($responseData));
}

return [$before, $after, $directories];
return Resource\PaginatedResource::constructFromResponse($response, Resource\Directory::class, 'directories');
}

/**
Expand All @@ -75,7 +70,8 @@ public function listDirectories(
*
* @throws Exception\WorkOSException
*
* @return array{?string, ?string, Resource\DirectoryGroup[]} An array containing the Directory Group ID to use as before and after cursor, and an array of Directory Group instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and groups array.
* Supports: [$before, $after, $groups] = $result, ["groups" => $groups] = $result, $result->groups
*/
public function listGroups(
?string $directory = null,
Expand Down Expand Up @@ -108,13 +104,7 @@ public function listGroups(
true
);

$groups = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $response) {
\array_push($groups, Resource\DirectoryGroup::constructFromResponse($response));
}

return [$before, $after, $groups];
return Resource\PaginatedResource::constructFromResponse($response, Resource\DirectoryGroup::class, 'groups');
}

/**
Expand Down Expand Up @@ -151,7 +141,8 @@ public function getGroup($directoryGroup)
* @param null|string $after Directory User ID to look after
* @param Resource\Order $order The Order in which to paginate records
*
* @return array{?string, ?string, Resource\DirectoryUser[]} An array containing the Directory User ID to use as before and after cursor, and an array of Directory User instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and users array.
* Supports: [$before, $after, $users] = $result, ["users" => $users] = $result, $result->users
*
* @throws Exception\WorkOSException
*/
Expand Down Expand Up @@ -186,13 +177,7 @@ public function listUsers(
true
);

$users = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $response) {
\array_push($users, Resource\DirectoryUser::constructFromResponse($response));
}

return [$before, $after, $users];
return Resource\PaginatedResource::constructFromResponse($response, Resource\DirectoryUser::class, 'users');
}

/**
Expand Down
22 changes: 6 additions & 16 deletions lib/Organizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Organizations
* @param null|string $after Organization ID to look after
* @param Resource\Order $order The Order in which to paginate records
*
* @return array{?string, ?string, Resource\Organization[]} An array containing the Organization ID to use as before and after cursor, and an array of Organization instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and organizations array.
* Supports: [$before, $after, $organizations] = $result, ["organizations" => $orgs] = $result, $result->organizations
*
* @throws Exception\WorkOSException
*/
Expand Down Expand Up @@ -49,13 +50,7 @@ public function listOrganizations(
true
);

$organizations = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($organizations, Resource\Organization::constructFromResponse($responseData));
}

return [$before, $after, $organizations];
return Resource\PaginatedResource::constructFromResponse($response, Resource\Organization::class, 'organizations');
}

/**
Expand Down Expand Up @@ -262,7 +257,8 @@ public function listOrganizationRoles($organizationId)
*
* @throws Exception\WorkOSException
*
* @return array{?string, ?string, Resource\FeatureFlag[]} An array containing the FeatureFlag ID to use as before and after cursor, and an array of FeatureFlag instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and feature_flags array.
* Supports: [$before, $after, $flags] = $result, ["feature_flags" => $flags] = $result, $result->feature_flags
*/
public function listOrganizationFeatureFlags(
$organizationId,
Expand All @@ -287,12 +283,6 @@ public function listOrganizationFeatureFlags(
true
);

$featureFlags = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($featureFlags, Resource\FeatureFlag::constructFromResponse($responseData));
}

return [$before, $after, $featureFlags];
return Resource\PaginatedResource::constructFromResponse($response, Resource\FeatureFlag::class, 'feature_flags');
}
}
198 changes: 198 additions & 0 deletions lib/Resource/PaginatedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace WorkOS\Resource;

/**
* Class PaginatedResource
*
* A flexible paginated resource that supports multiple access patterns:
* 1. Bare destructuring (backwards compatible): [$before, $after, $data] = $result
* 2. Named destructuring: ["users" => $users, "after" => $after] = $result
* 3. Fluent property access: $result->users, $result->after, $result->before
*
* This class standardizes pagination across all WorkOS resources while maintaining
* backwards compatibility with existing code.
*/
class PaginatedResource implements \ArrayAccess, \IteratorAggregate
{
/**
* @var ?string Before cursor for pagination
*/
private $before;

/**
* @var ?string After cursor for pagination
*/
private $after;

/**
* @var array The paginated data items
*/
private $data;

/**
* @var string The key name for the data array (e.g., 'users', 'directories')
*/
private $dataKey;

/**
* PaginatedResource constructor.
*
* @param ?string $before Before cursor
* @param ?string $after After cursor
* @param array $data Array of resource objects
* @param string $dataKey The key name for accessing the data
*/
public function __construct(?string $before, ?string $after, array $data, string $dataKey)
{
$this->before = $before;
$this->after = $after;
$this->data = $data;
$this->dataKey = $dataKey;
}

/**
* Construct a PaginatedResource from an API response
*
* @param array $response The API response containing 'data', 'list_metadata', etc.
* @param string $resourceClass The fully qualified class name of the resource type
* @param string $dataKey The key name for the data array (e.g., 'users', 'directories')
* @return self
*/
public static function constructFromResponse(array $response, string $resourceClass, string $dataKey): self
{
$data = [];
list($before, $after) = \WorkOS\Util\Request::parsePaginationArgs($response);

foreach ($response["data"] as $responseData) {
\array_push($data, $resourceClass::constructFromResponse($responseData));
}

return new self($before, $after, $data, $dataKey);
}

/**
* Magic getter for fluent property access
*
* @param string $name Property name
* @return mixed
*/
public function __get(string $name)
{
if ($name === 'before') {
return $this->before;
}

if ($name === 'after') {
return $this->after;
}

if ($name === 'data' || $name === $this->dataKey) {
return $this->data;
}

return null;
}

/**
* ArrayAccess: Check if offset exists
*
* @param mixed $offset
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset): bool
{
// Support numeric indices for bare destructuring
if (is_int($offset)) {
return $offset >= 0 && $offset <= 2;
}

// Support named keys for named destructuring
return in_array($offset, ['before', 'after', 'data', $this->dataKey], true);
}

/**
* ArrayAccess: Get value at offset
*
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
// Support numeric indices for bare destructuring: [0 => before, 1 => after, 2 => data]
if ($offset === 0) {
return $this->before;
}

if ($offset === 1) {
return $this->after;
}

if ($offset === 2) {
return $this->data;
}

// Support named keys for named destructuring
if ($offset === 'before') {
return $this->before;
}

if ($offset === 'after') {
return $this->after;
}

if ($offset === 'data' || $offset === $this->dataKey) {
return $this->data;
}

return null;
}

/**
* ArrayAccess: Set value at offset (not supported)
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value): void
{
throw new \BadMethodCallException('PaginatedResource is immutable');
}

/**
* ArrayAccess: Unset offset (not supported)
*
* @param mixed $offset
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset): void
{
throw new \BadMethodCallException('PaginatedResource is immutable');
}

/**
* IteratorAggregate: Get iterator for the data array
*
* @return \ArrayIterator
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->data);
}

/**
* Magic isset for property checking
*
* @param string $name
* @return bool
*/
public function __isset(string $name): bool
{
return in_array($name, ['before', 'after', 'data', $this->dataKey], true);
}
}
11 changes: 3 additions & 8 deletions lib/SSO.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ public function getConnection($connection)
* @param null|string $after Connection ID to look after
* @param Resource\Order $order The Order in which to paginate records
*
* @return array{?string, ?string, Resource\Connection[]} An array containing the Directory Connection ID to use as before and after cursor, and an array of Connection instances
* @return Resource\PaginatedResource A paginated resource containing before/after cursors and connections array.
* Supports: [$before, $after, $connections] = $result, ["connections" => $connections] = $result, $result->connections
*
* @throws Exception\WorkOSException
*/
Expand Down Expand Up @@ -246,12 +247,6 @@ public function listConnections(
true
);

$connections = [];
list($before, $after) = Util\Request::parsePaginationArgs($response);
foreach ($response["data"] as $responseData) {
\array_push($connections, Resource\Connection::constructFromResponse($responseData));
}

return [$before, $after, $connections];
return Resource\PaginatedResource::constructFromResponse($response, Resource\Connection::class, 'connections');
}
}
Loading