StorageFactory.php
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace ACA\ACF\Editing;
use AC\MetaType;
use ACA\ACF\CloneColumnFactory;
use ACA\ACF\Column;
use ACA\ACF\GroupColumnFactory;
class StorageFactory {
public function create( Column $column ) {
if ( $this->is_group( $column->get_type() ) ) {
return new Storage\Group(
$this->get_group_key( $column->get_type() ),
$this->get_sub_key( $column->get_type() ),
$this->id_prefix( $column ),
new Storage\Read\Column( $column )
);
}
if ( $this->is_clone( $column->get_type() ) ) {
return new Storage\CloneField(
$this->get_clone_hash( $column->get_type() ),
$this->get_clone_field_hash( $column->get_type() ),
$this->id_prefix( $column ),
new Storage\Read\Column( $column )
);
}
return new Storage\Field(
$column->get_type(),
$this->id_prefix( $column ),
new Storage\Read\Column( $column )
);
}
private function get_group_key( $column_type ) {
$column_type = str_replace( GroupColumnFactory::GROUP_PREFIX, '', $column_type );
$parts = explode( '-', $column_type );
return $parts[0];
}
private function get_sub_key( $column_type ) {
$column_type = str_replace( GroupColumnFactory::GROUP_PREFIX, '', $column_type );
$parts = explode( '-', $column_type );
return $parts[1];
}
private function get_clone_hash( $column_type ) {
$column_type = str_replace( CloneColumnFactory::CLONE_PREFIX, '', $column_type );
$key_parts = explode( '_', $column_type );
return sprintf( '%s_%s', $key_parts[0], $key_parts[1] );
}
private function get_clone_field_hash( $column_type ) {
$column_type = str_replace( CloneColumnFactory::CLONE_PREFIX, '', $column_type );
$key_parts = explode( '_', $column_type );
return sprintf( '%s_%s', $key_parts[2], $key_parts[3] );
}
/**
* @param string $column_type
*
* @return bool
*/
private function is_clone( $column_type ) {
return 0 === strpos( $column_type, CloneColumnFactory::CLONE_PREFIX );
}
/**
* @param string $column_type
*
* @return bool
*/
private function is_group( $column_type ) {
return 0 === strpos( $column_type, GroupColumnFactory::GROUP_PREFIX );
}
private function id_prefix( Column $column ) {
switch ( $column->get_meta_type() ) {
case MetaType::USER:
return 'user_';
case MetaType::COMMENT:
return 'comment_';
case MetaType::SITE:
return 'site_';
case MetaType::TERM:
return $column->get_taxonomy() . '_';
default:
return '';
}
}
}