Failed Conditions
Push — master ( 67fec4...924347 )
by Adrien
07:48
created

Matrix   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 96
ccs 40
cts 41
cp 0.9756
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transpose() 0 18 4
A extractRowValue() 0 13 3
B index() 0 36 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
7
class Matrix
8
{
9
    /**
10
     * TRANSPOSE.
11
     *
12
     * @param mixed (array) $matrixData A matrix of values
13
     *
14
     * @return array
15
     */
16 6
    public static function transpose($matrixData)
17
    {
18 6
        $returnMatrix = [];
19 6
        if (!is_array($matrixData)) {
20 1
            $matrixData = [[$matrixData]];
21
        }
22
23 6
        $column = 0;
24 6
        foreach ($matrixData as $matrixRow) {
25 6
            $row = 0;
26 6
            foreach ($matrixRow as $matrixCell) {
27 6
                $returnMatrix[$row][$column] = $matrixCell;
28 6
                ++$row;
29
            }
30 6
            ++$column;
31
        }
32
33 6
        return $returnMatrix;
34
    }
35
36
    /**
37
     * INDEX.
38
     *
39
     * Uses an index to choose a value from a reference or array
40
     *
41
     * Excel Function:
42
     *        =INDEX(range_array, row_num, [column_num])
43
     *
44
     * @param mixed $matrix A range of cells or an array constant
45
     * @param mixed $rowNum The row in the array or range from which to return a value.
46
     *                          If row_num is omitted, column_num is required.
47
     * @param mixed $columnNum The column in the array or range from which to return a value.
48
     *                          If column_num is omitted, row_num is required.
49
     *
50
     * @return mixed the value of a specified cell or array of cells
51
     */
52 16
    public static function index($matrix, $rowNum = 0, $columnNum = 0)
53
    {
54 16
        $rowNum = Functions::flattenSingleValue($rowNum);
55 16
        $columnNum = Functions::flattenSingleValue($columnNum);
56
57 16
        if (!is_numeric($rowNum) || !is_numeric($columnNum) || ($rowNum < 0) || ($columnNum < 0)) {
58 2
            return Functions::VALUE();
59
        }
60
61 14
        if (!is_array($matrix) || ($rowNum > count($matrix))) {
62 2
            return Functions::REF();
63
        }
64
65 12
        $rowKeys = array_keys($matrix);
66 12
        $columnKeys = @array_keys($matrix[$rowKeys[0]]);
67
68 12
        if ($columnNum > count($columnKeys)) {
69 1
            return Functions::REF();
70
        }
71
72 11
        if ($columnNum == 0) {
73 5
            return self::extractRowValue($matrix, $rowKeys, $rowNum);
74
        }
75
76 7
        $columnNum = $columnKeys[--$columnNum];
77 7
        if ($rowNum == 0) {
78 2
            return array_map(
79
                function ($value) {
80 2
                    return [$value];
81 2
                },
82 2
                array_column($matrix, $columnNum)
83
            );
84
        }
85 6
        $rowNum = $rowKeys[--$rowNum];
86
87 6
        return $matrix[$rowNum][$columnNum];
88
    }
89
90 5
    private static function extractRowValue(array $matrix, array $rowKeys, int $rowNum)
91
    {
92 5
        if ($rowNum == 0) {
93 1
            return $matrix;
94
        }
95
96 4
        $rowNum = $rowKeys[--$rowNum];
97 4
        $row = $matrix[$rowNum];
98 4
        if (is_array($row)) {
99 4
            return [$rowNum => $row];
100
        }
101
102
        return $row;
103
    }
104
}
105