summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Automation/CompareBasketObject.php
blob: f1ab6a91772375c10fe1766ccea2f4fd38c6235f (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

namespace Icinga\Module\Director\DirectorObject\Automation;

use Icinga\Module\Director\Core\Json;
use ipl\Html\Error;
use RuntimeException;
use function array_key_exists;
use function is_array;
use function is_object;
use function is_scalar;

class CompareBasketObject
{
    public static function normalize(&$value)
    {
        if (is_scalar($value)) {
            return;
        }
        if (is_array($value)) {
            foreach ($value as $k => &$v) {
                static::normalize($v);
            }
            unset($v);
        }
        if (is_object($value)) {
            $sorted = (array) $value;
            // foreign baskets might not sort as we do:
            ksort($sorted);
            foreach ($sorted as $k => &$v) {
                static::normalize($v);
            }
            unset($v);
            $value = (object) $sorted;

            // foreign baskets might not sort those lists correctly:
            if (isset($value->list_name) && isset($value->entries)) {
                static::sortListBy('entry_name', $value->entries);
            }
            if (isset($value->fields)) {
                static::sortListBy('datafield_id', $value->fields);
            }
        }
    }

    protected static function sortListBy($key, &$list)
    {
        usort($list, function ($a, $b) use ($key) {
            if (is_array($a)) {
                return $a[$key] > $b[$key] ? -1 : 1;
            } else {
                return $a->$key > $b->$key ? -1 : 1;
            }
        });
    }

    public static function equals($a, $b)
    {
        if (is_scalar($a)) {
            return $a === $b;
        }

        if ($a === null) {
            return $b === null;
        }

        // Well... this is annoying :-/
        $a = Json::decode(Json::encode($a, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        $b = Json::decode(Json::encode($b, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
        if (is_array($a)) {
            // Empty arrays VS empty objects :-( This is a fallback, not needed unless en/decode takes place
            if (empty($a) && is_object($b) && (array) $b === []) {
                return true;
            }
            if (! is_array($b)) {
                return false;
            }
            if (array_keys($a) !== array_keys($b)) {
                return false;
            }
            foreach ($a as $k => $v) {
                if (array_key_exists($k, $b) && static::equals($b[$k], $v)) {
                    continue;
                }
                return  false;
            }

            return true;
        }

        if (is_object($a)) {
            // Well... empty arrays VS empty objects :-(
            if ($b === [] && (array) $a === []) {
                return true;
            }
            if (! is_object($b)) {
                return false;
            }

            // Workaround, same as above
            if (isset($a->list_name) && isset($a->entries)) {
                if (! isset($b->entries)) {
                    return false;
                }
                static::sortListBy('entry_name', $a->entries);
                static::sortListBy('entry_name', $b->entries);
            }
            if (isset($a->fields) && isset($b->fields)) {
                static::sortListBy('datafield_id', $a->fields);
                static::sortListBy('datafield_id', $b->fields);
            }
            foreach ((array) $a as $k => $v) {
                if (property_exists($b, $k) && static::equals($v, $b->$k)) {
                    continue;
                }
                if (! property_exists($b, $k)) {
                    if ($v === null) {
                        continue;
                    }
                    // Deal with two special defaults:
                    if ($k === 'set_if_format' && $v === 'string') {
                        continue;
                    }
                    if ($k === 'disabled' && $v === false) {
                        continue;
                    }
                }
                return false;
            }
            foreach ((array) $b as $k => $v) {
                if (! property_exists($a, $k)) {
                    if ($v === null) {
                        continue;
                    }
                    // Once again:
                    if ($k === 'set_if_format' && $v === 'string') {
                        continue;
                    }
                    if ($k === 'disabled' && $v === false) {
                        continue;
                    }
                    return false;
                }
            }
            return true;
        }

        throw new RuntimeException("Cannot compare " . Error::getPhpTypeName($a));
    }
}