File size: 4,375 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
namespace ProcessMaker\Util;

class ArrayUtil
{
    public static function boolToIntValues($array)
    {
        array_walk($array, function (&$v) {
            if ($v === false) $v = 0;
            elseif ($v === true) $v = 1;
            elseif ($v === null) $v = 0;
        });

        return $array;
    }

    /**
     * Function that sorts an associative array by given array keys
     *
     * @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
     * @param array $data associative contains array data to sort by key.
     * @param array $columns contains the key to sort by those them.
     * @param mixed $direction this param by default is SORT_ASC, it can contains a value from [SORT_ASC|SORT_DESC]
     *                         php constants, or it can be a array(SORT_ASC,SORT_DESC,..), it must have the same length
     *                         of $columns array.
     * @return array
     * @throws \Exception
     *
     * Example:
     *   $data = array();
     *   $data[] = array('volume' => 67, 'edition' => 2);
     *   $data[] = array('volume' => 86, 'edition' => 1);
     *   $data[] = array('volume' => 85, 'edition' => 6);
     *   $data[] = array('volume' => 98, 'edition' => 2);
     *   $data[] = array('volume' => 86, 'edition' => 6);
     *   $data[] = array('volume' => 67, 'edition' => 7);
     *
     *   $r = ArrayUtil::sort($data, array("volume", "edition"), array(SORT_DESC, SORT_ASC));
     *   print_r($r);
     *
     * Example Output:
     * Array
     * (
     *     [0] => Array (
     *         [volume] => 98
     *         [edition] => 2
     *     )
     *     [1] => Array (
     *         [volume] => 86
     *         [edition] => 1
     *     )
     *     [2] => Array (
     *         [volume] => 86
     *         [edition] => 6
     *     )
     *     [3] => Array (
     *         [volume] => 85
     *         [edition] => 6
     *     )
     *     [4] => Array (
     *         [volume] => 67
     *         [edition] => 2
     *     )
     *     [5] => Array (
     *         [volume] => 67
     *         [edition] => 7
     *     )
     * )
     */
    public static function sort($data, $columns, $direction = SORT_ASC)
    {
        if (empty($data)) {
            return $data;
        }

        $composedData = array();

        if (is_array($direction)) {
            if (count($direction) !== count($columns)) {
                echo "PHP Warning:  ProcessMaker\\Util\\ArrayUtil::sort(): Argument (array)#2 and Argument (array)#3 lengths must be equals.";
                return false;
            }
        }

        foreach ($data as $row) {
            $j = 0;
            foreach ($columns as $i => $col) {
                if (! isset($row[$col])) {
                    echo "PHP Warning:  ProcessMaker\\Util\\ArrayUtil::sort(): Undefined key: $col, is set on Argument (array)#2, it must be set on Argument (array)#1";
                    return false;
                }
                $composedData[$j++][] = $row[$col];
                $composedData[$j++] = is_array($direction) ? $direction[$i] : $direction;
            }
        }

        $composedData[] = & $data;

        if (PHP_VERSION_ID < 50400) {
            switch (count($columns)) {
                case 1: array_multisort($composedData[0], $composedData[1], $composedData[2]); break;
                case 2: array_multisort($composedData[0], $composedData[1], $composedData[2], $composedData[3], $composedData[4]); break;
                case 3: array_multisort($composedData[0], $composedData[1], $composedData[2], $composedData[3], $composedData[4],
                    $composedData[5], $composedData[6]); break;
                case 4: array_multisort($composedData[0], $composedData[1], $composedData[2], $composedData[3], $composedData[4],
                    $composedData[5],$composedData[6], $composedData[7], $composedData[8]); break;
                case 5: array_multisort($composedData[0], $composedData[1], $composedData[2], $composedData[3], $composedData[4],
                    $composedData[5],$composedData[6], $composedData[7], $composedData[8], $composedData[9], $composedData[10]); break;
                default:
                    return false;
            }
        } else {
            call_user_func_array("array_multisort", $composedData);
        }

        return $data;
    }
}