Matrix::compute()   C
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 43
rs 6.7272
cc 7
eloc 21
nc 15
nop 0
1
<?php
2
/*
3
 * This file is part of JoliCi.
4
 *
5
 * (c) Joel Wurtz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Joli\JoliCi;
12
13
/**
14
 * Create the Job list by computing all available possibility through dimensions
15
 */
16
class Matrix
17
{
18
    private $dimensions = array();
19
20
    /**
21
     * Set a dimension for this matrix
22
     *
23
     * @param string $name   Name of the dimension
24
     * @param array  $values Value for this dimension
25
     */
26
    public function setDimension($name, array $values)
27
    {
28
        if (empty($values)) {
29
            $values = array(null);
30
        }
31
32
        $this->dimensions[$name] = $values;
33
    }
34
35
    /**
36
     * Return all possibility for the matrix
37
     *
38
     * @return array
39
     */
40
    public function compute()
41
    {
42
        $dimensions = $this->dimensions;
43
44
        if (empty($dimensions)) {
45
            return array();
46
        }
47
48
        // Pop first dimension
49
        $values = reset($dimensions);
50
        $name   = key($dimensions);
51
        unset($dimensions[$name]);
52
53
        // Create all possiblites for the first dimension
54
        $posibilities = array();
55
56
        foreach ($values as $v) {
57
            $posibilities[] = array($name => $v);
58
        }
59
60
        // If only one dimension return simple all the possibilites created (break point of recursivity)
61
        if (empty($dimensions)) {
62
            return $posibilities;
63
        }
64
65
        // If not create a new matrix with remaining dimension
66
        $matrix = new Matrix();
67
68
        foreach ($dimensions as $name => $values) {
69
            $matrix->setDimension($name, $values);
70
        }
71
72
        $result    = $matrix->compute();
73
        $newResult = array();
74
75
        foreach ($result as $value) {
76
            foreach ($posibilities as $possiblity) {
77
                $newResult[] = $value + $possiblity;
78
            }
79
        }
80
81
        return $newResult;
82
    }
83
}
84