| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Snippet DTO class |
| 4 |
* |
| 5 |
* Data Transfer Object for snippets from the Woody API. |
| 6 |
* Replaces the non-GPL-compatible JsonMapper implementation. |
| 7 |
* |
| 8 |
* @package Woody_Code_Snippets |
| 9 |
* @copyright Copyright (c) 2025, Themeisle |
| 10 |
* @license GPL-2.0+ |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* WINP_DTO_Snippet class - represents a code snippet from the API. |
| 20 |
*/ |
| 21 |
class WINP_DTO_Snippet extends WINP_DTO_Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Snippet ID. |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public $id; |
| 29 |
|
| 30 |
/** |
| 31 |
* Snippet title. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $title; |
| 36 |
|
| 37 |
/** |
| 38 |
* Snippet description. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $description; |
| 43 |
|
| 44 |
/** |
| 45 |
* Snippet content. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $content; |
| 50 |
|
| 51 |
/** |
| 52 |
* Snippets Library: video link. |
| 53 |
* |
| 54 |
* @var string|null |
| 55 |
*/ |
| 56 |
public $video_link; |
| 57 |
|
| 58 |
/** |
| 59 |
* Snippet scope. |
| 60 |
* |
| 61 |
* @var string|null |
| 62 |
*/ |
| 63 |
public $execute_everywhere; |
| 64 |
|
| 65 |
/** |
| 66 |
* Snippet status. |
| 67 |
* |
| 68 |
* @var bool |
| 69 |
*/ |
| 70 |
public $status; |
| 71 |
|
| 72 |
/** |
| 73 |
* Type ID. |
| 74 |
* |
| 75 |
* @var int |
| 76 |
*/ |
| 77 |
public $type_id; |
| 78 |
|
| 79 |
/** |
| 80 |
* Snippet premium marker. |
| 81 |
* |
| 82 |
* @var bool|null |
| 83 |
*/ |
| 84 |
public $is_premium; |
| 85 |
|
| 86 |
/** |
| 87 |
* Snippet updated time (timestamp). |
| 88 |
* |
| 89 |
* @var int |
| 90 |
*/ |
| 91 |
public $updated_at; |
| 92 |
|
| 93 |
/** |
| 94 |
* Snippet created time (timestamp). |
| 95 |
* |
| 96 |
* @var int |
| 97 |
*/ |
| 98 |
public $created_at; |
| 99 |
|
| 100 |
/** |
| 101 |
* Snippet type object. |
| 102 |
* |
| 103 |
* @var WINP_DTO_Type|null |
| 104 |
*/ |
| 105 |
public $type; |
| 106 |
|
| 107 |
/** |
| 108 |
* Snippet priority. |
| 109 |
* |
| 110 |
* @var int |
| 111 |
*/ |
| 112 |
public $priority; |
| 113 |
|
| 114 |
/** |
| 115 |
* Create instance from associative array. |
| 116 |
* |
| 117 |
* @param array<string, mixed> $data Associative array of data. |
| 118 |
* |
| 119 |
* @return self |
| 120 |
* @throws Exception If required fields are missing. |
| 121 |
*/ |
| 122 |
public static function from_array( array $data ) { |
| 123 |
// Validate required fields. |
| 124 |
self::require_field( $data, 'id', 'Snippet' ); |
| 125 |
self::require_field( $data, 'title', 'Snippet' ); |
| 126 |
|
| 127 |
$instance = new self(); |
| 128 |
|
| 129 |
// Map scalar properties. |
| 130 |
$instance->id = (int) $data['id']; |
| 131 |
$instance->title = (string) $data['title']; |
| 132 |
$instance->description = isset( $data['description'] ) ? (string) $data['description'] : ''; |
| 133 |
$instance->content = isset( $data['content'] ) ? (string) $data['content'] : ''; |
| 134 |
$instance->type_id = isset( $data['type_id'] ) ? (int) $data['type_id'] : 0; |
| 135 |
$instance->status = isset( $data['status'] ) ? (bool) $data['status'] : false; |
| 136 |
$instance->updated_at = isset( $data['updated_at'] ) ? (int) $data['updated_at'] : 0; |
| 137 |
$instance->created_at = isset( $data['created_at'] ) ? (int) $data['created_at'] : 0; |
| 138 |
$instance->priority = isset( $data['priority'] ) ? (int) $data['priority'] : 0; |
| 139 |
|
| 140 |
// Nullable properties. |
| 141 |
$instance->video_link = isset( $data['video_link'] ) ? (string) $data['video_link'] : null; |
| 142 |
$instance->execute_everywhere = isset( $data['execute_everywhere'] ) ? $data['execute_everywhere'] : null; |
| 143 |
$instance->is_premium = isset( $data['is_premium'] ) ? (bool) $data['is_premium'] : null; |
| 144 |
|
| 145 |
// Nested type object. |
| 146 |
if ( isset( $data['type'] ) ) { |
| 147 |
if ( is_object( $data['type'] ) ) { |
| 148 |
$instance->type = WINP_DTO_Type::from_json( $data['type'] ); |
| 149 |
} elseif ( is_array( $data['type'] ) ) { |
| 150 |
/** |
| 151 |
* Type data array. |
| 152 |
* |
| 153 |
* @var array<string, mixed> $type_array |
| 154 |
*/ |
| 155 |
$type_array = $data['type']; |
| 156 |
$instance->type = WINP_DTO_Type::from_array( $type_array ); |
| 157 |
} |
| 158 |
} else { |
| 159 |
$instance->type = null; |
| 160 |
} |
| 161 |
|
| 162 |
return $instance; |
| 163 |
} |
| 164 |
} |
| 165 |
|