Passed
Push — dev ( b6807e...cd2d6f )
by Fike
02:13
created

Tile::createImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Tile;
4
5
use AmaTeam\Image\Projection\API\Image\ImageInterface;
6
use AmaTeam\Image\Projection\API\Tile\PositionInterface;
7
use AmaTeam\Image\Projection\API\Tile\TileInterface;
8
9
/**
10
 * Represents single tile - rectangular projection chunk.
11
 */
12
class Tile implements TileInterface
13
{
14
    /**
15
     * @var PositionInterface
16
     */
17
    private $position;
18
    /**
19
     * @var ImageInterface
20
     */
21
    private $image;
22
23
    /**
24
     * @param PositionInterface $position
25
     * @param ImageInterface $image
26
     */
27
    public function __construct(
28
        PositionInterface $position,
29
        ImageInterface $image
30
    ) {
31
        $this->position = $position;
32
        $this->image = $image;
33
    }
34
35
    public function getImage()
36
    {
37
        return $this->image;
38
    }
39
40
    /**
41
     * @return PositionInterface
42
     */
43
    public function getPosition()
44
    {
45
        return $this->position;
46
    }
47
48
    /**
49
     * Converts single level tiles array into three level tree
50
     * (face -> rows -> columns).
51
     *
52
     * @param TileInterface[] $tiles
53
     * @return TileInterface[][][]
54
     */
55
    public static function treeify(array $tiles)
56
    {
57
        $target = [];
58
        foreach ($tiles as $tile) {
59
            $position = $tile->getPosition();
60
            $cursor = &$target;
61
            foreach ([$position->getFace(), $position->getY()] as $segment) {
62
                if (!isset($cursor[$segment])) {
63
                    $cursor[$segment] = [];
64
                }
65
                $cursor = &$cursor[$segment];
66
            }
67
            $cursor[$position->getX()] = $tile;
68
        }
69
        return $target;
70
    }
71
}
72