| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Cloud; |
| 4 |
|
| 5 |
use Code_Snippets\Data_Item; |
| 6 |
|
| 7 |
/** |
| 8 |
* A connection between a local snippet and remote cloud snippet. |
| 9 |
* |
| 10 |
* @package Code_Snippets |
| 11 |
* |
| 12 |
* @property integer $local_id ID of local snippet as stored in WordPress database, if applicable. |
| 13 |
* @property integer $cloud_id ID of remote snippet on cloud platform, if applicable. |
| 14 |
* @property boolean $is_owner Ownership status of remote snippet on cloud platform. |
| 15 |
* @property boolean $in_codevault Whether the remote snippet is stored in the users' codevault. |
| 16 |
* @property boolean $update_available If synchronised, whether there is an update available on the cloud platform. |
| 17 |
*/ |
| 18 |
class Cloud_Link extends Data_Item { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor function |
| 22 |
* |
| 23 |
* @param array<string, mixed>|object $data Initial data fields. |
| 24 |
*/ |
| 25 |
public function __construct( $data = null ) { |
| 26 |
parent::__construct( |
| 27 |
[ |
| 28 |
'local_id' => 0, |
| 29 |
'cloud_id' => 0, |
| 30 |
'is_owner' => false, |
| 31 |
'in_codevault' => false, |
| 32 |
'update_available' => false, |
| 33 |
], |
| 34 |
$data |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Prepare a value before it is stored. |
| 40 |
* |
| 41 |
* @param mixed $value Value to prepare. |
| 42 |
* @param string $field Field name. |
| 43 |
* |
| 44 |
* @return mixed Value in the correct format. |
| 45 |
*/ |
| 46 |
protected function prepare_field( $value, string $field ) { |
| 47 |
switch ( $field ) { |
| 48 |
case 'local_id': |
| 49 |
case 'remote_id': |
| 50 |
return absint( $value ); |
| 51 |
|
| 52 |
case 'is_owner': |
| 53 |
case 'in_codevault': |
| 54 |
case 'update_available': |
| 55 |
return is_bool( $value ) ? $value : (bool) $value; |
| 56 |
|
| 57 |
default: |
| 58 |
return $value; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|