|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Helper\Helper; |
|
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Base datastore. |
|
10
|
|
|
* Defines the key datastore methods and their default implementations. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Datastore implements Datastore_Interface { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The related object id |
|
16
|
|
|
* |
|
17
|
|
|
* @var integer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $object_id = 0; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initialize the datastore. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() { |
|
25
|
|
|
$this->init(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Initialization tasks for concrete datastores. |
|
30
|
|
|
* |
|
31
|
|
|
* @abstract |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public function init(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the related object id |
|
37
|
|
|
* |
|
38
|
|
|
* @return integer |
|
39
|
|
|
*/ |
|
40
|
|
|
public function get_object_id() { |
|
41
|
|
|
return $this->object_id; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set the related object id |
|
46
|
|
|
* |
|
47
|
|
|
* @param integer $object_id |
|
48
|
|
|
*/ |
|
49
|
|
|
public function set_object_id( $object_id ) { |
|
50
|
|
|
$this->object_id = $object_id; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create a new datastore of type $raw_type. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $raw_type |
|
57
|
|
|
* @return Datastore_Interface |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function factory( $raw_type ) { |
|
60
|
|
|
$type = Helper::normalize_type( $raw_type ); |
|
61
|
|
|
$class = Helper::type_to_class( $type, __NAMESPACE__, '_Datastore' ); |
|
62
|
|
|
if ( ! class_exists( $class ) ) { |
|
63
|
|
|
Incorrect_Syntax_Exception::raise( 'Unknown datastore type "' . $raw_type . '".' ); |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$datastore = new $class(); |
|
68
|
|
|
|
|
69
|
|
|
return $datastore; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* An alias of factory(). |
|
74
|
|
|
* |
|
75
|
|
|
* @see Datastore::factory() |
|
76
|
|
|
* @return Datastore_Interface |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function make() { |
|
79
|
|
|
return call_user_func_array( array( get_class(), 'factory' ), func_get_args() ); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|