Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

CoordinateGeometry::isInside()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 11
nc 16
nop 4
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\ImageProcessing;
12
13
use Intervention\Image\Size;
14
use Symfony\Component\PropertyAccess\PropertyAccess;
15
16
/**
17
 * Class CoordinateGeometry
18
 *
19
 * @package IrishDan\ResponsiveImageBundle
20
 */
21
class CoordinateGeometry
22
{
23
    private $x1;
24
    private $y1;
25
    private $x2;
26
    private $y2;
27
    private $aspectRatio;
28
    private $propertyAccessor;
29
30
    public function __construct($x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0)
31
    {
32
        $this->setPoints($x1, $y1, $x2, $y2);
33
        $this->setAspectRatio();
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    public function getAspectRatio()
40
    {
41
        return $this->aspectRatio;
42
    }
43
44
    public function scaleSize($scaleX, $scaleY)
45
    {
46
        $width  = $this->axisLength('x');
47
        $height = $this->axisLength('y');
48
49
        $size = new Size($width, $height);
50
51
        $size->resize(
52
            $scaleX,
53
            $scaleY,
54
            function ($constraint) {
55
                $constraint->aspectRatio();
56
            }
57
        );
58
59
        return [
60
            'width'  => $size->width,
61
            'height' => $size->height,
62
        ];
63
    }
64
65
    public function setPoints($x1 = 0, $y1 = 0, $x2 = 100, $y2 = 100)
66
    {
67
        $this->x1 = $x1;
68
        $this->y1 = $y1;
69
        $this->x2 = $x2;
70
        $this->y2 = $y2;
71
    }
72
73
    protected function setAspectRatio()
74
    {
75
        $this->aspectRatio = $this->aspectRatio();
76
    }
77
78
    public function axisLength($axis = 'x')
79
    {
80
        $axis = strtolower($axis);
81
        if ($axis == 'x') {
82
            return $this->x2 - $this->x1;
83
        }
84
        else {
85
            return $this->y2 - $this->y1;
86
        }
87
    }
88
89
    public function aspectRatio($width = null, $length = null)
90
    {
91
        $l = empty($width) ? $this->axisLength('x') : $width;
92
        $w = empty($length) ? $this->axisLength('y') : $length;
93
94
        return $w / $l;
95
    }
96
97
    public function compareWithAspectRatios($width, $height)
98
    {
99
        $aspectRatio = $this->aspectRatio($width, $height);
100
101
        return $this->aspectRatio / $aspectRatio;
102
    }
103
104
    protected function shouldScaleAxis($x, $y)
105
    {
106
        $styleAspect = $this->aspectRatio($x, $y);
107
        if (empty($x) || $styleAspect >= $this->aspectRatio) {
108
            return 'x';
109
        }
110
111
        if (empty($y) || $styleAspect < $this->aspectRatio) {
112
            return 'y';
113
        }
114
115
        return 'y';
116
    }
117
118
    public function isInside($x1, $y1, $x2, $y2)
119
    {
120
        $inside = true;
121
        if ($x1 < $this->x1) {
122
            $inside = false;
123
        }
124
125
        if ($y1 < $this->y1) {
126
            $inside = false;
127
        }
128
129
        if ($x2 > $this->x2) {
130
            $inside = false;
131
        }
132
133
        if ($y2 > $this->y2) {
134
            $inside = false;
135
        }
136
137
        return $inside;
138
    }
139
140
    public function roundAll(array $data)
141
    {
142
        if (empty($this->propertyAccessor)) {
143
            $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
144
        }
145
146
        foreach ($data as $property => $value) {
147
            $this->propertyAccessor->setValue($data, '[' . $property . ']', round($value));
148
        }
149
150
        return $data;
151
    }
152
}