PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / Model / Cloud_Snippets.php

Cloud_Snippets.php in Code Snippets 3.10.1, at php/Model/Cloud_Snippets.php

170 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Model;
4
5 use WP_REST_Response;
6
7 /**
8 * A list of snippets as retrieved from the cloud API.
9 *
10 * @since 3.4.0
11 * @package Code_Snippets
12 *
13 * @property Cloud_Snippet[] $snippets List of snippet items for the current page.
14 * @property int $page Page of data that this data belongs to.
15 * @property int $total_pages Total number of available pages of items.
16 * @property int $total_snippets Total number of available snippet items.
17 * @property array<int, int> $cloud_id_rev An array of all cloud snippet IDs and their revision numbers.
18 * @property array<string, array> $available_filters An array of available filters that can be applied to the collection.
19 */
20 class Cloud_Snippets extends Model {
21
22 /**
23 * List of default values provided for fields.
24 *
25 * @var array<string, mixed>
26 */
27 protected static array $default_values = [
28 'snippets' => [],
29 'total_snippets' => 0,
30 'total_pages' => 0,
31 'page' => 0,
32 'cloud_id_rev' => [],
33 'available_filters' => [],
34 ];
35
36 /**
37 * List of field name aliases to map when resolving a field name.
38 *
39 * @var array<string, string> Field alias names keyed to actual field names.
40 */
41 protected static array $field_aliases = [
42 'items' => 'snippets',
43 'total_items' => 'total_snippets',
44 'page' => 'page',
45 'cloud_id_rev' => 'cloud_id_rev',
46 ];
47
48 /**
49 * Prepare a value before it is stored.
50 *
51 * @param mixed $value Value to prepare.
52 * @param string $field Field name.
53 *
54 * @return mixed Value in the correct format.
55 */
56 protected function prepare_field( $value, string $field ) {
57 switch ( $field ) {
58 case 'page':
59 case 'total_pages':
60 case 'total_snippets':
61 return absint( $value );
62
63 default:
64 return $value;
65 }
66 }
67
68 /**
69 * Prepare the `snippets` field by ensuring it is a list of Cloud_Snippets objects.
70 *
71 * @param mixed $snippets The field as provided.
72 *
73 * @return Cloud_Snippets[] The field in the correct format.
74 * @noinspection PhpUnused
75 */
76 protected function prepare_snippets( $snippets ): array {
77 $result = [];
78 $snippets = is_array( $snippets ) ? $snippets : [ $snippets ];
79
80 foreach ( $snippets as $snippet ) {
81 $result[] = $snippet instanceof Cloud_Snippet ? $snippet : new Cloud_Snippet( $snippet );
82 }
83
84 return $result;
85 }
86
87 /**
88 * Unpack meta values from API response.
89 *
90 * @param array $meta Response meta data.
91 *
92 * @return void
93 */
94 public function unpack_api_meta( array $meta ) {
95 if ( isset( $meta['total'] ) && is_numeric( $meta['total'] ) ) {
96 $this->total_snippets = $meta['total'];
97 }
98
99 if ( isset( $meta['total_pages'] ) && is_numeric( $meta['total_pages'] ) ) {
100 $this->total_pages = $meta['total_pages'];
101 }
102
103 if ( isset( $meta['page'] ) && is_numeric( $meta['page'] ) ) {
104 $this->page = max( 0, (int) $meta['page'] - 1 );
105 }
106 }
107
108 /**
109 * Normalize payloads returned by the cloud API into the shape expected by this class.
110 *
111 * @param array|null $response Response data as returned from API.
112 * @param int|null $page_number Page number requested, if applicible.
113 *
114 * @return Cloud_Snippets Constructed cloud snippets object from response data.
115 */
116 public static function unpack_api_response( ?array $response, ?int $page_number = null ): ?Cloud_Snippets {
117 if ( ! $response ) {
118 return null;
119 }
120
121 $result = new Cloud_Snippets();
122 $snippets_data = $response['snippets'] ?? $response['data'] ?? null;
123
124 if ( is_array( $snippets_data ) ) {
125 $result->snippets = array_map(
126 fn( $snippet_data ) => new Cloud_Snippet( $snippet_data ),
127 $snippets_data
128 );
129 }
130
131 $result->cloud_id_rev = $response['cloud_id_rev'] ?? [];
132 $result->available_filters = $response['available_filters'] ?? [];
133
134 if ( isset( $response['meta'] ) && is_array( $response['meta'] ) ) {
135 $result->unpack_api_meta( $response['meta'] );
136 }
137
138 if ( ! is_null( $page_number ) ) {
139 $result->page = $page_number;
140 }
141
142 return $result;
143 }
144
145 /**
146 * Transform the data stored into this class into a REST API response.
147 *
148 * @return WP_REST_Response
149 */
150 public function to_rest_response(): WP_REST_Response {
151 $response = rest_ensure_response(
152 [
153 'snippets' => array_map(
154 fn( Cloud_Snippet $snippet ) => $snippet->get_fields(),
155 $this->snippets
156 ),
157 'page' => $this->page,
158 'total_pages' => $this->total_pages,
159 'total_snippets' => $this->total_snippets,
160 'available_filters' => $this->available_filters,
161 ]
162 );
163
164 $response->header( 'X-WP-Total', $this->total_snippets );
165 $response->header( 'X-WP-TotalPages', $this->total_pages );
166
167 return $response;
168 }
169 }
170