summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Common/Sort.php
blob: 4728af3e5e23440f79854d7bf76cbb8aeb2bc7b0 (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
151
152
153
154
155
156
157
158
<?php
// Icinga Business Process Modelling | (c) 2023 Icinga GmbH | GPLv2

namespace Icinga\Module\Businessprocess\Common;

use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\Node;
use InvalidArgumentException;
use ipl\Stdlib\Str;

trait Sort
{
    /** @var ?string Current sort specification */
    protected $sort;

    /** @var ?callable Actual sorting function */
    protected $sortFn;

    /**
     * Get the sort specification
     *
     * @return ?string
     */
    public function getSort(): ?string
    {
        return $this->sort;
    }

    /**
     * Set the sort specification
     *
     * @param ?string $sort
     *
     * @return $this
     *
     * @throws InvalidArgumentException When sorting according to the specified specification is not possible
     */
    public function setSort(?string $sort): self
    {
        if (empty($sort)) {
            return $this;
        }

        list($sortBy, $direction) = Str::symmetricSplit($sort, ' ', 2, 'asc');

        switch ($sortBy) {
            case 'manual':
                if ($direction === 'asc') {
                    $this->sortFn = function (array &$nodes) {
                        $firstNode = reset($nodes);
                        if ($firstNode instanceof BpNode && $firstNode->getDisplay() > 0) {
                            $nodes = self::applyManualSorting($nodes);
                        }

                        // Child nodes don't need to be ordered in this case, their implicit order is significant
                    };
                } else {
                    $this->sortFn = function (array &$nodes) {
                        $firstNode = reset($nodes);
                        if ($firstNode instanceof BpNode && $firstNode->getDisplay() > 0) {
                            uasort($nodes, function (BpNode $a, BpNode $b) {
                                return $b->getDisplay() <=> $a->getDisplay();
                            });
                        } else {
                            $nodes = array_reverse($nodes);
                        }
                    };
                }

                break;
            case 'display_name':
                if ($direction === 'asc') {
                    $this->sortFn = function (array &$nodes) {
                        uasort($nodes, function (Node $a, Node $b) {
                            return strnatcasecmp(
                                $a->getAlias() ?? $a->getName(),
                                $b->getAlias() ?? $b->getName()
                            );
                        });
                    };
                } else {
                    $this->sortFn = function (array &$nodes) {
                        uasort($nodes, function (Node $a, Node $b) {
                            return strnatcasecmp(
                                $b->getAlias() ?? $b->getName(),
                                $a->getAlias() ?? $a->getName()
                            );
                        });
                    };
                }

                break;
            case 'state':
                if ($direction === 'asc') {
                    $this->sortFn = function (array &$nodes) {
                        uasort($nodes, function (Node $a, Node $b) {
                            return $a->getSortingState() <=> $b->getSortingState();
                        });
                    };
                } else {
                    $this->sortFn = function (array &$nodes) {
                        uasort($nodes, function (Node $a, Node $b) {
                            return $b->getSortingState() <=> $a->getSortingState();
                        });
                    };
                }

                break;
            default:
                throw new InvalidArgumentException(sprintf(
                    "Can't sort by %s. It's only possible to sort by manual order, display_name or state",
                    $sortBy
                ));
        }

        $this->sort = $sort;

        return $this;
    }

    /**
     * Sort the given nodes as specified by {@see setSort()}
     *
     * If {@see setSort()} has not been called yet, the default sort specification is used
     *
     * @param array $nodes
     *
     * @return array
     */
    public function sort(array $nodes): array
    {
        if (empty($nodes)) {
            return $nodes;
        }

        if ($this->sortFn !== null) {
            call_user_func_array($this->sortFn, [&$nodes]);
        }

        return $nodes;
    }

    /**
     * Apply manual sort order on the given process nodes
     *
     * @param array $bpNodes
     *
     * @return array
     */
    public static function applyManualSorting(array $bpNodes): array
    {
        uasort($bpNodes, function (BpNode $a, BpNode $b) {
            return $a->getDisplay() <=> $b->getDisplay();
        });

        return $bpNodes;
    }
}