Connection.php
2 months ago
Edge.php
2 months ago
IdCursorFilter.php
2 months ago
PageInfo.php
2 months ago
PaginationParams.php
2 months ago
Connection.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Api\Pagination; |
| 6 | |
| 7 | /** |
| 8 | * Represents a Relay-style paginated connection. |
| 9 | */ |
| 10 | class Connection { |
| 11 | /** |
| 12 | * Connection edges wrapping each node with its cursor. |
| 13 | * |
| 14 | * @var Edge[] |
| 15 | */ |
| 16 | public array $edges; |
| 17 | |
| 18 | /** |
| 19 | * The raw nodes without cursor wrappers. |
| 20 | * |
| 21 | * @var object[] |
| 22 | */ |
| 23 | public array $nodes; |
| 24 | |
| 25 | public PageInfo $page_info; |
| 26 | |
| 27 | public int $total_count; |
| 28 | |
| 29 | /** |
| 30 | * Whether this connection has already been sliced. |
| 31 | * |
| 32 | * When true, subsequent calls to slice() return $this immediately, |
| 33 | * preventing double-slicing when both the command class and the |
| 34 | * auto-generated resolver call slice(). |
| 35 | * |
| 36 | * @var bool |
| 37 | */ |
| 38 | private bool $sliced = false; |
| 39 | |
| 40 | /** |
| 41 | * Create a pre-sliced connection for the performance path. |
| 42 | * |
| 43 | * Use this when the DB query already applied pagination limits, |
| 44 | * so no further slicing is needed. |
| 45 | * |
| 46 | * @param Edge[] $edges The already-paginated edges. |
| 47 | * @param PageInfo $page_info The pagination info. |
| 48 | * @param int $total_count The total count before pagination. |
| 49 | * @return self A Connection marked as already sliced. |
| 50 | */ |
| 51 | public static function pre_sliced( array $edges, PageInfo $page_info, int $total_count ): self { |
| 52 | $connection = new self(); |
| 53 | $connection->edges = $edges; |
| 54 | $connection->nodes = array_map( fn( Edge $e ) => $e->node, $edges ); |
| 55 | $connection->page_info = $page_info; |
| 56 | $connection->total_count = $total_count; |
| 57 | $connection->sliced = true; |
| 58 | |
| 59 | return $connection; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Return a new Connection sliced according to the given pagination args. |
| 64 | * |
| 65 | * Applies the Relay cursor-based pagination algorithm: first narrow by |
| 66 | * after/before cursors, then take first N or last N from the remainder. |
| 67 | * |
| 68 | * @param array $args Pagination arguments with keys: first, last, after, before. |
| 69 | * @return self A new Connection with sliced edges/nodes and updated page_info. |
| 70 | */ |
| 71 | public function slice( array $args ): self { |
| 72 | if ( $this->sliced ) { |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | // Enforce the same 0..MAX_PAGE_SIZE bounds that PaginationParams |
| 77 | // applies to root queries. Without this, nested connection fields |
| 78 | // (e.g. `variations(first: 1000)`) would slip past the cap because |
| 79 | // the generated resolver passes raw GraphQL args straight in. |
| 80 | PaginationParams::validate_args( $args ); |
| 81 | |
| 82 | $first = $args['first'] ?? null; |
| 83 | $last = $args['last'] ?? null; |
| 84 | $after = $args['after'] ?? null; |
| 85 | $before = $args['before'] ?? null; |
| 86 | |
| 87 | // No pagination requested — return as-is. |
| 88 | if ( null === $first && null === $last && null === $after && null === $before ) { |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | $edges = $this->edges; |
| 93 | |
| 94 | // Narrow by "after" cursor. |
| 95 | if ( null !== $after ) { |
| 96 | $found = false; |
| 97 | foreach ( $edges as $i => $edge ) { |
| 98 | if ( $edge->cursor === $after ) { |
| 99 | $edges = array_slice( $edges, $i + 1 ); |
| 100 | $found = true; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | if ( ! $found ) { |
| 105 | $edges = array(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Narrow by "before" cursor. |
| 110 | if ( null !== $before ) { |
| 111 | $filtered = array(); |
| 112 | foreach ( $edges as $edge ) { |
| 113 | if ( $edge->cursor === $before ) { |
| 114 | break; |
| 115 | } |
| 116 | $filtered[] = $edge; |
| 117 | } |
| 118 | $edges = $filtered; |
| 119 | } |
| 120 | |
| 121 | $total_after_cursors = count( $edges ); |
| 122 | |
| 123 | // Apply first/last. |
| 124 | if ( null !== $first && $first >= 0 ) { |
| 125 | $edges = array_slice( $edges, 0, $first ); |
| 126 | } |
| 127 | if ( null !== $last && $last >= 0 ) { |
| 128 | $edges = array_slice( $edges, max( 0, count( $edges ) - $last ) ); |
| 129 | } |
| 130 | |
| 131 | // Build the sliced connection. |
| 132 | $connection = new self(); |
| 133 | $connection->edges = array_values( $edges ); |
| 134 | $connection->nodes = array_map( fn( Edge $e ) => $e->node, $edges ); |
| 135 | $connection->total_count = $this->total_count; |
| 136 | $connection->sliced = true; |
| 137 | |
| 138 | $page_info = new PageInfo(); |
| 139 | $page_info->start_cursor = ! empty( $edges ) ? $edges[0]->cursor : null; |
| 140 | $page_info->end_cursor = ! empty( $edges ) ? $edges[ count( $edges ) - 1 ]->cursor : null; |
| 141 | $page_info->has_next_page = null !== $first ? count( $edges ) < $total_after_cursors : $this->page_info->has_next_page; |
| 142 | $page_info->has_previous_page = null !== $last ? count( $edges ) < $total_after_cursors : ( null !== $after ); |
| 143 | $connection->page_info = $page_info; |
| 144 | |
| 145 | return $connection; |
| 146 | } |
| 147 | } |
| 148 |