PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 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 All 65 releases
code-snippets / php / Model / Cloud_Snippets.php

Cloud_Snippets.php in Code Snippets 4.0.0-beta.1, at php/Model/Cloud_Snippets.php

149 lines 4.2 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 $cloud_id_rev An array of all cloud snippet IDs and their revision numbers.
18 * @property 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 /**
50 * Class constructor.
51 *
52 * @param array|null $initial_data Initial data from the cloud API response.
53 */
54 public function __construct( $initial_data = null ) {
55 parent::__construct( $this->normalize_cloud_api( $initial_data ) );
56 }
57
58 /**
59 * Prepare a value before it is stored.
60 *
61 * @param mixed $value Value to prepare.
62 * @param string $field Field name.
63 *
64 * @return mixed Value in the correct format.
65 */
66 protected function prepare_field( $value, string $field ) {
67 switch ( $field ) {
68 case 'page':
69 case 'total_pages':
70 case 'total_snippets':
71 return absint( $value );
72
73 default:
74 return $value;
75 }
76 }
77
78 /**
79 * Prepare the `snippets` field by ensuring it is a list of Cloud_Snippets objects.
80 *
81 * @param mixed $snippets The field as provided.
82 *
83 * @return Cloud_Snippets[] The field in the correct format.
84 * @noinspection PhpUnused
85 */
86 protected function prepare_snippets( $snippets ): array {
87 $result = [];
88 $snippets = is_array( $snippets ) ? $snippets : [ $snippets ];
89
90 foreach ( $snippets as $snippet ) {
91 $result[] = $snippet instanceof Cloud_Snippet ? $snippet : new Cloud_Snippet( $snippet );
92 }
93
94 return $result;
95 }
96
97 /**
98 * Normalize payloads returned by the cloud API into the shape expected by this class.
99 *
100 * @param mixed $initial_data Raw data passed into the constructor.
101 *
102 * @return array Normalized data array or original value when no normalization is required.
103 */
104 private function normalize_cloud_api( $initial_data ): array {
105 $result = [];
106
107 if ( is_array( $initial_data ) ) {
108 $meta = $initial_data['meta'] ?? [];
109
110 $result['snippets'] = $initial_data['snippets'] ?? [];
111 $result['cloud_id_rev'] = $initial_data['cloud_id_rev'] ?? [];
112
113 if ( $meta ) {
114 $result['total_snippets'] = isset( $meta['total'] ) ? (int) $meta['total'] : 0;
115 $result['total_pages'] = isset( $meta['total_pages'] ) ? (int) $meta['total_pages'] : 0;
116 $result['page'] = isset( $meta['page'] ) ? max( 0, (int) $meta['page'] - 1 ) : 0;
117 $result['available_filters'] = $initial_data['available_filters'] ?? [];
118 }
119 }
120
121 return $result;
122 }
123
124 /**
125 * Transform the data stored into this class into a REST API response.
126 *
127 * @return WP_REST_Response
128 */
129 public function to_rest_response(): WP_REST_Response {
130 $response = rest_ensure_response(
131 [
132 'snippets' => array_map(
133 fn( Cloud_Snippet $snippet ) => $snippet->get_fields(),
134 $this->snippets
135 ),
136 'page' => $this->page,
137 'total_pages' => $this->total_pages,
138 'total_snippets' => $this->total_snippets,
139 'available_filters' => $this->available_filters,
140 ]
141 );
142
143 $response->header( 'X-WP-Total', $this->total_snippets );
144 $response->header( 'X-WP-TotalPages', $this->total_pages );
145
146 return $response;
147 }
148 }
149