Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
92.86% covered (success)
92.86%
26 / 28
GeometryCalculator
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
16.09
92.86% covered (success)
92.86%
26 / 28
 calculateOffsets
0.00% covered (danger)
0.00%
0 / 1
8.23
84.62% covered (warning)
84.62%
11 / 13
 calculateExtents
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
15 / 15
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 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @copyright   2009-2015 PHPPresentation contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpPresentation;
22
23/**
24 * PhpOffice\PhpPresentation\GeometryCalculator.
25 */
26class GeometryCalculator
27{
28    public const X = 'X';
29    public const Y = 'Y';
30
31    /**
32     * Calculate X and Y offsets for a set of shapes within a container such as a slide or group.
33     *
34     * @return array<string, int>
35     */
36    public static function calculateOffsets(ShapeContainerInterface $container): array
37    {
38        $offsets = [self::X => 0, self::Y => 0];
39
40        if (null !== $container && 0 != count($container->getShapeCollection())) {
41            $shapes = $container->getShapeCollection();
42            if (null !== $shapes[0]) {
43                $offsets[self::X] = $shapes[0]->getOffsetX();
44                $offsets[self::Y] = $shapes[0]->getOffsetY();
45            }
46
47            foreach ($shapes as $shape) {
48                if (null !== $shape) {
49                    if ($shape->getOffsetX() < $offsets[self::X]) {
50                        $offsets[self::X] = $shape->getOffsetX();
51                    }
52
53                    if ($shape->getOffsetY() < $offsets[self::Y]) {
54                        $offsets[self::Y] = $shape->getOffsetY();
55                    }
56                }
57            }
58        }
59
60        return $offsets;
61    }
62
63    /**
64     * Calculate X and Y extents for a set of shapes within a container such as a slide or group.
65     *
66     * @return array<string, int>
67     */
68    public static function calculateExtents(ShapeContainerInterface $container): array
69    {
70        /** @var array<string, int> $extents */
71        $extents = [self::X => 0, self::Y => 0];
72
73        if (null !== $container && 0 != count($container->getShapeCollection())) {
74            $shapes = $container->getShapeCollection();
75            if (null !== $shapes[0]) {
76                $extents[self::X] = (int) ($shapes[0]->getOffsetX() + $shapes[0]->getWidth());
77                $extents[self::Y] = (int) ($shapes[0]->getOffsetY() + $shapes[0]->getHeight());
78            }
79
80            foreach ($shapes as $shape) {
81                if (null !== $shape) {
82                    $extentX = (int) ($shape->getOffsetX() + $shape->getWidth());
83                    $extentY = (int) ($shape->getOffsetY() + $shape->getHeight());
84
85                    if ($extentX > $extents[self::X]) {
86                        $extents[self::X] = $extentX;
87                    }
88
89                    if ($extentY > $extents[self::Y]) {
90                        $extents[self::Y] = $extentY;
91                    }
92                }
93            }
94        }
95
96        return $extents;
97    }
98}