| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Type DTO class |
| 4 |
* |
| 5 |
* Data Transfer Object for snippet types 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_Type class - represents a snippet type/category from the API. |
| 20 |
*/ |
| 21 |
class WINP_DTO_Type extends WINP_DTO_Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Type ID. |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public $id; |
| 29 |
|
| 30 |
/** |
| 31 |
* Type title. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $title; |
| 36 |
|
| 37 |
/** |
| 38 |
* Type slug. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $slug; |
| 43 |
|
| 44 |
/** |
| 45 |
* Create instance from associative array. |
| 46 |
* |
| 47 |
* @param array<string, mixed> $data Associative array of data. |
| 48 |
* |
| 49 |
* @return self |
| 50 |
* @throws Exception If required fields are missing. |
| 51 |
*/ |
| 52 |
public static function from_array( array $data ) { |
| 53 |
// Validate required fields. |
| 54 |
self::require_field( $data, 'id', 'Type' ); |
| 55 |
self::require_field( $data, 'title', 'Type' ); |
| 56 |
self::require_field( $data, 'slug', 'Type' ); |
| 57 |
|
| 58 |
$instance = new self(); |
| 59 |
|
| 60 |
$instance->id = (int) $data['id']; |
| 61 |
$instance->title = (string) $data['title']; |
| 62 |
$instance->slug = (string) $data['slug']; |
| 63 |
|
| 64 |
return $instance; |
| 65 |
} |
| 66 |
} |
| 67 |
|