1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* inetprocess/transformation |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @author Rémi Sauvat |
8
|
|
|
* @copyright 2005-2015 iNet Process |
9
|
|
|
* |
10
|
|
|
* @package inetprocess/transformation |
11
|
|
|
* |
12
|
|
|
* @license GNU General Public License v2.0 |
13
|
|
|
* |
14
|
|
|
* @link http://www.inetprocess.com |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Inet\Transformation\Rule; |
18
|
|
|
|
19
|
|
|
use Inet\Transformation\Exception\TransformationException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Call a function and send back the result |
23
|
|
|
*/ |
24
|
|
|
class SugarCRMMapMultiEnum extends AbstractRule |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Operate the transformation |
28
|
|
|
* |
29
|
|
|
* @param string $input |
30
|
|
|
* @param array $arguments |
31
|
|
|
* |
32
|
|
|
* @throws Inet\Transformation\Exception\TransformationException |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
12 |
|
public function transform($input, array $arguments) |
37
|
|
|
{ |
38
|
|
|
// I should have two arguments: old format / new format |
39
|
12 |
|
if (count($arguments) !== 1 && count($arguments) !== 2) { |
40
|
2 |
|
throw new TransformationException( |
41
|
|
|
'Rule SugarCRMMapMultiEnum expects 1 or 2 argument: mapping, options' |
42
|
2 |
|
); |
43
|
|
|
} |
44
|
10 |
|
$mapping = $arguments[0]; |
45
|
10 |
|
if (!is_array($mapping)) { |
46
|
1 |
|
throw new TransformationException( |
47
|
|
|
'First argument of SugarCRMMapMultiEnum should be an assosiative array' |
48
|
1 |
|
); |
49
|
|
|
} |
50
|
|
|
// Merge options with defaults |
51
|
|
|
$options = array( |
52
|
9 |
|
'separator' => '|', |
53
|
9 |
|
'from_multi_enum' => false, |
54
|
9 |
|
); |
55
|
9 |
|
if (!empty($arguments[1])) { |
56
|
4 |
|
$opts = $arguments[1]; |
57
|
4 |
|
if (!is_array($opts)) { |
58
|
1 |
|
throw new TransformationException( |
59
|
|
|
'Optionnal second argument of SugarCRMMapMultiEnum should be an associative array' |
60
|
1 |
|
); |
61
|
|
|
} |
62
|
3 |
|
foreach ($options as $key => $value) { |
63
|
3 |
|
if (array_key_exists($key, $opts)) { |
64
|
3 |
|
$options[$key] = $opts[$key]; |
65
|
3 |
|
} |
66
|
3 |
|
} |
67
|
3 |
|
} |
68
|
|
|
|
69
|
8 |
|
if ($options['from_multi_enum']) { |
70
|
|
|
// Decode multiEnum value |
71
|
3 |
|
$input = $this->unencodeMultienum($input); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
8 |
|
if (!is_array($input)) { |
75
|
4 |
|
$input = explode($options['separator'], $input); |
76
|
4 |
|
} |
77
|
8 |
|
$ret = array(); |
78
|
8 |
|
foreach ($input as $value) { |
79
|
8 |
|
if (array_key_exists($value, $mapping)) { |
80
|
6 |
|
$value = $mapping[$value]; |
81
|
6 |
|
} |
82
|
8 |
|
if (!empty($value)) { |
83
|
7 |
|
$ret[] = $value; |
84
|
7 |
|
} |
85
|
8 |
|
} |
86
|
8 |
|
return $this->encodeMultienumValue($ret); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Function copied from SugarCRM |
91
|
|
|
*/ |
92
|
3 |
|
public function unencodeMultienum($string) |
93
|
|
|
{ |
94
|
3 |
|
if (is_array($string)) { |
95
|
1 |
|
return $string; |
96
|
|
|
} |
97
|
2 |
|
if (substr($string, 0, 1) == "^" && substr($string, -1) == "^") { |
98
|
|
|
// Remove empty values from beginning and end of the string |
99
|
2 |
|
$string = preg_replace('/^(\^\^,\^)|(\^,\^\^)$/', '^', $string); |
100
|
|
|
|
101
|
|
|
// Get the inner part of the string without leading|trailing ^ chars |
102
|
2 |
|
$string = substr(substr($string, 1), 0, strlen($string) - 2); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
2 |
|
return explode('^,^', $string); |
106
|
|
|
} |
107
|
|
|
|
108
|
8 |
|
public function encodeMultienumValue($arr) |
109
|
|
|
{ |
110
|
8 |
|
if (empty($arr)) { |
111
|
1 |
|
return ""; |
112
|
|
|
} |
113
|
|
|
|
114
|
7 |
|
$string = "^".implode('^,^', $arr)."^"; |
115
|
|
|
|
116
|
7 |
|
return $string; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|