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
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
namespace Icinga\Module\Director\Db\Branch;
use Icinga\Module\Director\Data\Json;
use function in_array;
/**
* Hardcoded branch-related settings
*/
class BranchSettings
{
// TODO: Ranges is weird. key = scheduled_downtime_id, range_type, range_key
const ENCODED_ARRAYS = ['imports', 'groups', 'ranges', 'users', 'usergroups'];
const ENCODED_DICTIONARIES = ['vars', 'arguments'];
const BRANCH_SPECIFIC_PROPERTIES = [
'uuid',
'branch_uuid',
'branch_created',
'branch_deleted',
'set_null',
];
const BRANCH_BOOLEANS = [
'branch_created',
'branch_deleted',
];
const RELATED_SETS = [
'types',
'states',
];
public static function propertyIsEncodedArray($property)
{
return in_array($property, self::ENCODED_ARRAYS, true);
}
public static function propertyIsRelatedSet($property)
{
// TODO: get from object class
return in_array($property, self::RELATED_SETS, true);
}
public static function propertyIsEncodedDictionary($property)
{
return in_array($property, self::ENCODED_DICTIONARIES, true);
}
public static function propertyIsBranchSpecific($property)
{
return in_array($property, self::BRANCH_SPECIFIC_PROPERTIES, true);
}
public static function flattenEncodedDicationaries(array &$properties)
{
foreach (self::ENCODED_DICTIONARIES as $property) {
self::flattenProperty($properties, $property);
}
}
public static function normalizeBranchedObjectFromDb($row)
{
$normalized = [];
$row = (array) $row;
foreach ($row as $key => $value) {
if (! static::propertyIsBranchSpecific($key)) {
if (is_resource($value)) {
$value = stream_get_contents($value);
}
if ($value !== null && static::propertyIsEncodedArray($key)) {
$value = Json::decode($value);
}
if ($value !== null && static::propertyIsRelatedSet($key)) {
// TODO: We might want to combine them (current VS branched)
$value = Json::decode($value);
}
if ($value !== null && static::propertyIsEncodedDictionary($key)) {
$value = Json::decode($value);
}
if ($value !== null) {
$normalized[$key] = $value;
}
}
}
static::flattenEncodedDicationaries($row);
if (isset($row['set_null'])) {
foreach (Json::decode($row['set_null']) as $property) {
$normalized[$property] = null;
}
}
foreach (self::BRANCH_BOOLEANS as $key) {
if ($row[$key] === 'y') {
$row[$key] = true;
} elseif ($row[$key] === 'n') {
$row[$key] = false;
} else {
throw new \RuntimeException(sprintf(
"Boolean DB property expected, got '%s' for '%s'",
$row[$key],
$key
));
}
}
return $normalized;
}
public static function flattenProperty(array &$properties, $property)
{
// TODO: dots in varnames -> throw or escape?
if (isset($properties[$property])) {
foreach ((array) $properties[$property] as $key => $value) {
$properties["$property.$key"] = $value;
}
unset($properties[$property]);
}
}
}
|