Passed
Push — master ( 10b137...2bffcf )
by Adrien
10:25
created

Matrix   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 101
ccs 43
cts 44
cp 0.9773
rs 10
c 1
b 0
f 0
wmc 16

3 Methods

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