| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Cloud; |
| 4 |
|
| 5 |
use Code_Snippets\Data_Item; |
| 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 integer $page Page of data that this data belongs to. |
| 15 |
* @property integer $total_pages Total number of available pages of items. |
| 16 |
* @property integer $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 bool $success If the request has any results. |
| 19 |
*/ |
| 20 |
class Cloud_Snippets extends Data_Item { |
| 21 |
|
| 22 |
/** |
| 23 |
* Class constructor. |
| 24 |
* |
| 25 |
* @param array<string, Cloud_Snippet[]|integer> $initial_data Initial data. |
| 26 |
*/ |
| 27 |
public function __construct( $initial_data = null ) { |
| 28 |
$initial_data = $this->normalize_cloud_api( $initial_data ); |
| 29 |
parent::__construct( |
| 30 |
[ |
| 31 |
'snippets' => [], |
| 32 |
'total_snippets' => 0, |
| 33 |
'total_pages' => 0, |
| 34 |
'page' => 0, |
| 35 |
'cloud_id_rev' => [], |
| 36 |
], |
| 37 |
$initial_data, |
| 38 |
[ |
| 39 |
'items' => 'snippets', |
| 40 |
'total_items' => 'total_snippets', |
| 41 |
'page' => 'page', |
| 42 |
'cloud_id_rev' => 'cloud_id_rev', |
| 43 |
] |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Prepare a value before it is stored. |
| 49 |
* |
| 50 |
* @param mixed $value Value to prepare. |
| 51 |
* @param string $field Field name. |
| 52 |
* |
| 53 |
* @return mixed Value in the correct format. |
| 54 |
*/ |
| 55 |
protected function prepare_field( $value, string $field ) { |
| 56 |
switch ( $field ) { |
| 57 |
case 'page': |
| 58 |
case 'total_pages': |
| 59 |
case 'total_snippets': |
| 60 |
return absint( $value ); |
| 61 |
|
| 62 |
default: |
| 63 |
return $value; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Prepare the `snippets` field by ensuring it is a list of Cloud_Snippets objects. |
| 69 |
* |
| 70 |
* @param mixed $snippets The field as provided. |
| 71 |
* |
| 72 |
* @return Cloud_Snippets[] The field in the correct format. |
| 73 |
*/ |
| 74 |
protected function prepare_snippets( $snippets ): array { |
| 75 |
$result = []; |
| 76 |
$snippets = is_array( $snippets ) ? $snippets : [ $snippets ]; |
| 77 |
|
| 78 |
foreach ( $snippets as $snippet ) { |
| 79 |
$result[] = $snippet instanceof Cloud_Snippet ? $snippet : new Cloud_Snippet( $snippet ); |
| 80 |
} |
| 81 |
|
| 82 |
return $result; |
| 83 |
} |
| 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 |
} |
| 107 |
} |
| 108 |
|