Completed
Push — develop ( d9e4d8...cd1d80 )
by Franck
23s
created

GeometryCalculator::calculateOffsets()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.0231

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 8.4444
c 0
b 0
f 0
cc 8
nc 3
nop 1
crap 8.0231
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation;
19
20
/**
21
 * PhpOffice\PhpPresentation\GeometryCalculator
22
 */
23
class GeometryCalculator
24
{
25
    const X = 'X';
26
    const Y = 'Y';
27
28
    /**
29
    * Calculate X and Y offsets for a set of shapes within a container such as a slide or group.
30
    *
31
    * @param  \PhpOffice\PhpPresentation\ShapeContainerInterface $container
32
    * @return array
33
    */
34 121
    public static function calculateOffsets(ShapeContainerInterface $container)
35
    {
36 121
        $offsets = array(self::X => 0, self::Y => 0);
37
38 121
        if ($container !== null && count($container->getShapeCollection()) != 0) {
39 92
            $shapes = $container->getShapeCollection();
40 92
            if ($shapes[0] !== null) {
41 92
                $offsets[self::X] = $shapes[0]->getOffsetX();
42 92
                $offsets[self::Y] = $shapes[0]->getOffsetY();
43
            }
44
45 92
            foreach ($shapes as $shape) {
46 92
                if ($shape !== null) {
47 92
                    if ($shape->getOffsetX() < $offsets[self::X]) {
48
                        $offsets[self::X] = $shape->getOffsetX();
49
                    }
50
51 92
                    if ($shape->getOffsetY() < $offsets[self::Y]) {
52 92
                        $offsets[self::Y] = $shape->getOffsetY();
53
                    }
54
                }
55
            }
56
        }
57
58 121
        return $offsets;
59
    }
60
61
    /**
62
    * Calculate X and Y extents for a set of shapes within a container such as a slide or group.
63
    *
64
    * @param  \PhpOffice\PhpPresentation\ShapeContainerInterface $container
65
    * @return array
66
    */
67 119
    public static function calculateExtents(ShapeContainerInterface $container)
68
    {
69 119
        $extents = array(self::X => 0, self::Y => 0);
70
71 119
        if ($container !== null && count($container->getShapeCollection()) != 0) {
72 90
            $shapes = $container->getShapeCollection();
73 90
            if ($shapes[0] !== null) {
74 90
                $extents[self::X] = $shapes[0]->getOffsetX() + $shapes[0]->getWidth();
75 90
                $extents[self::Y] = $shapes[0]->getOffsetY() + $shapes[0]->getHeight();
76
            }
77
78 90
            foreach ($shapes as $shape) {
79 90
                if ($shape !== null) {
80 90
                    $extentX = $shape->getOffsetX() + $shape->getWidth();
81 90
                    $extentY = $shape->getOffsetY() + $shape->getHeight();
82
83 90
                    if ($extentX > $extents[self::X]) {
84 1
                        $extents[self::X] = $extentX;
85
                    }
86
87 90
                    if ($extentY > $extents[self::Y]) {
88 90
                        $extents[self::Y] = $extentY;
89
                    }
90
                }
91
            }
92
        }
93
94 119
        return $extents;
95
    }
96
}
97