|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Dataset; |
|
4
|
|
|
|
|
5
|
|
|
use Wordlift\Jsonld\Jsonld_Service; |
|
6
|
|
|
use Wordlift\Object_Type_Enum; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Abstract_Sync_Object_Adapter implements Sync_Object_Adapter { |
|
9
|
|
|
|
|
10
|
|
|
private $object_id; |
|
11
|
|
|
|
|
12
|
|
|
private $type; |
|
13
|
|
|
|
|
14
|
|
|
private $get_meta; |
|
15
|
|
|
|
|
16
|
|
|
private $update_meta; |
|
17
|
|
|
|
|
18
|
|
|
private static $meta_name = array( |
|
19
|
|
|
Object_Type_Enum::POST => 'post', |
|
20
|
|
|
Object_Type_Enum::TERM => 'term', |
|
21
|
|
|
Object_Type_Enum::USER => 'user', |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Sync_Object_Adapter constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param int $type One of Object_Type_Enum. |
|
28
|
|
|
* @param int $object_id A post or term id. |
|
29
|
|
|
* @param Jsonld_Service |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
function __construct( $type, $object_id ) { |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$this->type = filter_var( $type, FILTER_VALIDATE_INT ); |
|
36
|
|
|
$this->object_id = filter_var( $object_id, FILTER_VALIDATE_INT ); |
|
37
|
|
|
|
|
38
|
|
|
if ( null === $this->type || ! isset( self::$meta_name[ $this->type ] ) ) { |
|
39
|
|
|
throw new \Exception( 'Invalid $type.' ); |
|
40
|
|
|
} |
|
41
|
|
|
if ( null === $this->object_id ) { |
|
42
|
|
|
throw new \Exception( 'Invalid $object.' ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->get_meta = sprintf( 'get_%s_meta', self::$meta_name[ $this->type ] ); |
|
46
|
|
|
$this->update_meta = sprintf( 'update_%s_meta', self::$meta_name[ $this->type ] ); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
function get_meta( $meta_key, $single ) { |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
return call_user_func( $this->get_meta, $this->object_id, $meta_key, $single ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
function update_meta( $meta_key, $meta_value ) { |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
call_user_func( $this->update_meta, $this->object_id, $meta_key, $meta_value ); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.