MatrixParser::getCalculatedMatrix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GGGGino\WarehousePath\Parser;
4
5
use GGGGino\WarehousePath\Entity\Place;
6
use GGGGino\WarehousePath\PlacesCollector;
7
8
class MatrixParser implements ParserInterface
9
{
10
    /**
11
     * The original matrix values
12
     *
13
     * @var array
14
     */
15
    protected $originalMatrix;
16
17
    /**
18
     * The original matrix values
19
     *
20
     * @var array
21
     */
22
    protected $calculatedMatrix;
23
24
    /**
25
     * The array with the calculated Objects
26
     *
27
     * @var array
28
     */
29
    protected $calculatedArray;
30
31
    /**
32
     * @var PlacesCollector
33
     */
34
    protected $placeCollector;
35
36
    public function __construct(array $originalMatrix, PlacesCollector $placeCollector)
37
    {
38
        $this->calculatedMatrix = array();
39
        $this->originalMatrix = $originalMatrix;
40
        $this->placeCollector = $placeCollector;
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function parse()
47
    {
48
        foreach($this->originalMatrix as $rKey => $row) {
49
            foreach($row as $cKey => $column) {
50
                $placeType = $this->placeCollector->getPlaceTypeByWeight($column['weight']);
51
                $placeTypeNew = new Place($placeType);
52
53
                $placeTypeNew->setName($rKey . $cKey);
54
                if( isset($this->originalMatrix[$rKey - 1][$cKey]['obj']) ) {
55
                    $placeTypeNew->setTopRef($this->originalMatrix[$rKey - 1][$cKey]['obj']);
56
                }
57
58
                if( isset($this->originalMatrix[$rKey][$cKey - 1]['obj']) ) {
59
                    $placeTypeNew->setLeftRef($this->originalMatrix[$rKey][$cKey - 1]['obj']);
60
                }
61
62
                $this->calculatedMatrix[$rKey][$cKey] = $placeTypeNew;
63
                $this->calculatedArray[] = $placeTypeNew;
64
                $this->originalMatrix[$rKey][$cKey]['obj'] = $placeTypeNew;
65
            }
66
        }
67
68
        $this->placeCollector->setPlaces($this->calculatedArray);
69
70
        return $this->calculatedArray;
71
    }
72
73
    /**
74
     * @param array $originalMatrix
75
     * @return MatrixParser
76
     */
77
    public function setOriginalMatrix($originalMatrix)
78
    {
79
        $this->originalMatrix = $originalMatrix;
80
        return $this;
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getCalculatedMatrix(): array
87
    {
88
        return $this->calculatedMatrix;
89
    }
90
}