|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WorkOS\Resource; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class PaginatedResource |
| 7 | + * |
| 8 | + * A flexible paginated resource that supports multiple access patterns: |
| 9 | + * 1. Bare destructuring (backwards compatible): [$before, $after, $data] = $result |
| 10 | + * 2. Named destructuring: ["users" => $users, "after" => $after] = $result |
| 11 | + * 3. Fluent property access: $result->users, $result->after, $result->before |
| 12 | + * |
| 13 | + * This class standardizes pagination across all WorkOS resources while maintaining |
| 14 | + * backwards compatibility with existing code. |
| 15 | + */ |
| 16 | +class PaginatedResource implements \ArrayAccess, \IteratorAggregate |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var ?string Before cursor for pagination |
| 20 | + */ |
| 21 | + private $before; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var ?string After cursor for pagination |
| 25 | + */ |
| 26 | + private $after; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var array The paginated data items |
| 30 | + */ |
| 31 | + private $data; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string The key name for the data array (e.g., 'users', 'directories') |
| 35 | + */ |
| 36 | + private $dataKey; |
| 37 | + |
| 38 | + /** |
| 39 | + * PaginatedResource constructor. |
| 40 | + * |
| 41 | + * @param ?string $before Before cursor |
| 42 | + * @param ?string $after After cursor |
| 43 | + * @param array $data Array of resource objects |
| 44 | + * @param string $dataKey The key name for accessing the data |
| 45 | + */ |
| 46 | + public function __construct(?string $before, ?string $after, array $data, string $dataKey) |
| 47 | + { |
| 48 | + $this->before = $before; |
| 49 | + $this->after = $after; |
| 50 | + $this->data = $data; |
| 51 | + $this->dataKey = $dataKey; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Construct a PaginatedResource from an API response |
| 56 | + * |
| 57 | + * @param array $response The API response containing 'data', 'list_metadata', etc. |
| 58 | + * @param string $resourceClass The fully qualified class name of the resource type |
| 59 | + * @param string $dataKey The key name for the data array (e.g., 'users', 'directories') |
| 60 | + * @return self |
| 61 | + */ |
| 62 | + public static function constructFromResponse(array $response, string $resourceClass, string $dataKey): self |
| 63 | + { |
| 64 | + $data = []; |
| 65 | + list($before, $after) = \WorkOS\Util\Request::parsePaginationArgs($response); |
| 66 | + |
| 67 | + foreach ($response["data"] as $responseData) { |
| 68 | + \array_push($data, $resourceClass::constructFromResponse($responseData)); |
| 69 | + } |
| 70 | + |
| 71 | + return new self($before, $after, $data, $dataKey); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Magic getter for fluent property access |
| 76 | + * |
| 77 | + * @param string $name Property name |
| 78 | + * @return mixed |
| 79 | + */ |
| 80 | + public function __get(string $name) |
| 81 | + { |
| 82 | + if ($name === 'before') { |
| 83 | + return $this->before; |
| 84 | + } |
| 85 | + |
| 86 | + if ($name === 'after') { |
| 87 | + return $this->after; |
| 88 | + } |
| 89 | + |
| 90 | + if ($name === 'data' || $name === $this->dataKey) { |
| 91 | + return $this->data; |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * ArrayAccess: Check if offset exists |
| 99 | + * |
| 100 | + * @param mixed $offset |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + #[\ReturnTypeWillChange] |
| 104 | + public function offsetExists($offset): bool |
| 105 | + { |
| 106 | + // Support numeric indices for bare destructuring |
| 107 | + if (is_int($offset)) { |
| 108 | + return $offset >= 0 && $offset <= 2; |
| 109 | + } |
| 110 | + |
| 111 | + // Support named keys for named destructuring |
| 112 | + return in_array($offset, ['before', 'after', 'data', $this->dataKey], true); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * ArrayAccess: Get value at offset |
| 117 | + * |
| 118 | + * @param mixed $offset |
| 119 | + * @return mixed |
| 120 | + */ |
| 121 | + #[\ReturnTypeWillChange] |
| 122 | + public function offsetGet($offset) |
| 123 | + { |
| 124 | + // Support numeric indices for bare destructuring: [0 => before, 1 => after, 2 => data] |
| 125 | + if ($offset === 0) { |
| 126 | + return $this->before; |
| 127 | + } |
| 128 | + |
| 129 | + if ($offset === 1) { |
| 130 | + return $this->after; |
| 131 | + } |
| 132 | + |
| 133 | + if ($offset === 2) { |
| 134 | + return $this->data; |
| 135 | + } |
| 136 | + |
| 137 | + // Support named keys for named destructuring |
| 138 | + if ($offset === 'before') { |
| 139 | + return $this->before; |
| 140 | + } |
| 141 | + |
| 142 | + if ($offset === 'after') { |
| 143 | + return $this->after; |
| 144 | + } |
| 145 | + |
| 146 | + if ($offset === 'data' || $offset === $this->dataKey) { |
| 147 | + return $this->data; |
| 148 | + } |
| 149 | + |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * ArrayAccess: Set value at offset (not supported) |
| 155 | + * |
| 156 | + * @param mixed $offset |
| 157 | + * @param mixed $value |
| 158 | + * @return void |
| 159 | + */ |
| 160 | + #[\ReturnTypeWillChange] |
| 161 | + public function offsetSet($offset, $value): void |
| 162 | + { |
| 163 | + throw new \BadMethodCallException('PaginatedResource is immutable'); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * ArrayAccess: Unset offset (not supported) |
| 168 | + * |
| 169 | + * @param mixed $offset |
| 170 | + * @return void |
| 171 | + */ |
| 172 | + #[\ReturnTypeWillChange] |
| 173 | + public function offsetUnset($offset): void |
| 174 | + { |
| 175 | + throw new \BadMethodCallException('PaginatedResource is immutable'); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * IteratorAggregate: Get iterator for the data array |
| 180 | + * |
| 181 | + * @return \ArrayIterator |
| 182 | + */ |
| 183 | + public function getIterator(): \Traversable |
| 184 | + { |
| 185 | + return new \ArrayIterator($this->data); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Magic isset for property checking |
| 190 | + * |
| 191 | + * @param string $name |
| 192 | + * @return bool |
| 193 | + */ |
| 194 | + public function __isset(string $name): bool |
| 195 | + { |
| 196 | + return in_array($name, ['before', 'after', 'data', $this->dataKey], true); |
| 197 | + } |
| 198 | +} |
0 commit comments