File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ class Cloud_Snippets extends Data_Item {
2525 * @param array<string, Cloud_Snippet[]|integer> $initial_data Initial data.
2626 */
2727 public function __construct ( $ initial_data = null ) {
28+ $ initial_data = $ this ->normalize_cloud_api ( $ initial_data );
2829 parent ::__construct (
2930 [
3031 'snippets ' => [],
@@ -80,4 +81,27 @@ protected function prepare_snippets( $snippets ): array {
8081
8182 return $ result ;
8283 }
84+
85+ /**
86+ * Normalize payloads returned by the cloud API into the shape expected by this class.
87+ *
88+ * @param mixed $initial_data Raw data passed into the constructor.
89+ *
90+ * @return mixed Normalized data array or original value when no normalization is required.
91+ */
92+ private function normalize_cloud_api ( $ initial_data ) {
93+ // pagination metadata is nested under a 'meta' key.
94+ if ( is_array ( $ initial_data ) && isset ( $ initial_data ['meta ' ] ) ) {
95+ $ meta = $ initial_data ['meta ' ];
96+ $ normalized = [];
97+ $ normalized ['snippets ' ] = $ initial_data ['snippets ' ] ?? $ initial_data ['data ' ] ?? [];
98+ $ normalized ['total_snippets ' ] = isset ( $ meta ['total ' ] ) ? (int ) $ meta ['total ' ] : 0 ;
99+ $ normalized ['total_pages ' ] = isset ( $ meta ['total_pages ' ] ) ? (int ) $ meta ['total_pages ' ] : 0 ;
100+ $ normalized ['page ' ] = isset ( $ meta ['page ' ] ) ? max ( 0 , (int ) $ meta ['page ' ] - 1 ) : 0 ;
101+ $ normalized ['cloud_id_rev ' ] = $ initial_data ['cloud_id_rev ' ] ?? [];
102+ $ initial_data = $ normalized ;
103+ }
104+
105+ return $ initial_data ;
106+ }
83107}
You can’t perform that action at this time.
0 commit comments