1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace midcom\datamanager\storage; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Experimental storage class |
10
|
|
|
*/ |
11
|
|
|
class parameter extends delayed |
12
|
|
|
{ |
13
|
|
|
private $multiple_separator = '|'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This member contains the other key, in case it is set. In case of multiselects, |
17
|
|
|
* the full list of unknown keys is collected here, in case of single select, this value |
18
|
|
|
* takes precedence from the standard selection. |
19
|
|
|
* |
20
|
|
|
* This is only valid if the allow_other flag is set. |
21
|
|
|
* TODO: still to be implentend |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
2 |
|
private $other = array(); |
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
2 |
|
public function load() |
29
|
2 |
|
{ |
30
|
|
|
$source = $this->object->get_parameter($this->config['storage']['domain'], $this->config['storage']['name']); |
31
|
|
|
if(isset($this->config['type_config']['multiple_storagemode'])) { |
32
|
2 |
|
$source = $this->convert_multiple_from_storage($source); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $source; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function save() |
42
|
|
|
{ |
43
|
|
|
//workaround for weird mgd API behavior where setting falsy (i.e. deleting) a nonexistent parameter |
44
|
|
|
//returns an error |
45
|
|
|
if ( !$this->value |
46
|
|
|
&& $this->load() === null) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
if(isset($this->config['type_config']['multiple_storagemode'])) { |
50
|
|
|
$this->value = $this->convert_multiple_to_storage(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->object->set_parameter($this->config['storage']['domain'], $this->config['storage']['name'], $this->value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function convert_multiple_from_storage($source) |
57
|
|
|
{ |
58
|
|
|
$glue = $this->multiple_separator; |
59
|
|
|
|
60
|
|
|
switch ($this->config['type_config']['multiple_storagemode']) { |
61
|
|
|
case 'serialized': |
62
|
|
|
case 'array': |
63
|
|
|
if ( !is_array($source) |
64
|
|
|
&& empty($source)) { |
65
|
|
|
$source = array(); |
66
|
|
|
} |
67
|
|
|
return $source; |
68
|
|
|
|
69
|
|
|
case 'imploded': |
70
|
|
|
if (!is_string($source)) { |
71
|
|
|
return array(); |
72
|
|
|
} |
73
|
|
|
return explode($glue, $source); |
74
|
|
|
|
75
|
|
|
case 'imploded_wrapped': |
76
|
|
|
if (!is_string($source)) { |
77
|
|
|
return array(); |
78
|
|
|
} |
79
|
|
|
return explode($glue, substr($source, 1, -1)); |
80
|
|
|
|
81
|
|
|
default: |
82
|
|
|
throw new midcom_error("The multiple_storagemode '{$this->multiple_storagemode}' is invalid, cannot continue."); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Converts the selected options according to the multiple_storagemode setting. |
88
|
|
|
* |
89
|
|
|
* @return mixed The data converted to the final data storage. |
90
|
|
|
*/ |
91
|
|
|
function convert_multiple_to_storage() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
switch ($this->config['type_config']['multiple_storagemode']) { |
94
|
|
|
case 'array': |
95
|
|
|
return $this->value; |
96
|
|
|
|
97
|
|
|
case 'serialized': |
98
|
|
|
if ($this->others) { |
99
|
|
|
return array_merge($this->value, $this->others); |
100
|
|
|
} |
101
|
|
|
return $this->value; |
102
|
|
|
|
103
|
|
|
case 'imploded': |
104
|
|
|
$options = $this->getImplodedOptions(); |
105
|
|
|
return $options; |
106
|
|
|
|
107
|
|
|
case 'imploded_wrapped': |
108
|
|
|
$glue = $this->multiple_separator; |
109
|
|
|
$options = $this->getImplodedOptions(); |
110
|
|
|
return "{$glue}{$options}{$glue}"; |
111
|
|
|
|
112
|
|
|
default: |
113
|
|
|
throw new midcom_error("The multiple_storagemode '{$this->config['type_config']['multiple_storagemode']}' is invalid, cannot continue."); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Prepares the imploded storage string. All entries containing the pipe char (used as glue) |
119
|
|
|
* will be logged and skipped silently. |
120
|
|
|
* |
121
|
|
|
* @return string The imploded data string. |
122
|
|
|
*/ |
123
|
|
|
private function getImplodedOptions() |
124
|
|
|
{ |
125
|
|
|
$glue = $this->multiple_separator; |
126
|
|
|
|
127
|
|
|
if ($this->others) { |
128
|
|
|
if (is_string($this->others)) { |
129
|
|
|
$this->others = array( |
130
|
|
|
$this->others => $this->others, |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
$options = array_merge($this->value, $this->others); |
134
|
|
|
} else { |
135
|
|
|
$options = $this->value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$result = array(); |
139
|
|
|
foreach ($options as $key) { |
140
|
|
|
if (strpos($key, $glue) !== false) { |
141
|
|
|
debug_add("The option key '{$key}' contained the multiple separator ($this->config['type_config']['multiple_storagemode']) char, which is not allowed for imploded storage targets. ignoring silently.", |
142
|
|
|
MIDCOM_LOG_WARN); |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$result[] = $key; |
147
|
|
|
} |
148
|
|
|
return implode($glue, $result); |
149
|
|
|
} |
150
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.