UniformGradient::gradient()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 19
cts 19
cp 1
rs 9.2408
c 0
b 0
f 0
cc 5
nc 6
nop 4
crap 5
1
<?php
2
namespace nstdio\svg\gradient;
3
4
use nstdio\svg\container\ContainerInterface;
5
use nstdio\svg\ElementInterface;
6
7
/**
8
 * Class UniformGradient
9
 *
10
 * @package nstdio\svg\gradient
11
 * @author  Edgar Asatryan <[email protected]>
12
 */
13
abstract class UniformGradient extends Gradient
14
{
15
16
    /**
17
     * @param ElementInterface $svg
18
     * @param array            $colors
19
     * @param null             $id
20
     * @param string           $gradientType
21
     *
22
     * @return UniformGradient
23
     */
24 35
    public static function gradient(ElementInterface $svg, array $colors, $id = null, $gradientType = 'linear')
25
    {
26 35
        $gradient = self::getGradient($svg, $id, $gradientType);
27 35
        if (count($colors) <= 1) {
28 2
            $colors = array_merge($colors, $colors);
29 2
            if (empty($colors)) {
30 1
                $colors = ['white', 'black'];
31 35
            }
32 2
        }
33 35
        $step = 1 / (count($colors) - 1);
34 35
        $offsets = array_map(function($item) {
35 35
            return $item % 1 !== 0 ? sprintf('%0.2f', $item) : $item;
36 35
        }, range(0, 1, $step));
37
38 35
        foreach ($colors as $key => $color) {
39 35
            $ret = [];
40 35
            $ret['offset'] = $offsets[$key];
41 35
            $ret['stop-color'] = $color;
42 35
            $gradient->appendStop(new Stop($gradient, $ret));
43 35
        }
44
45 35
        return $gradient;
46
    }
47
48
    /**
49
     * @param ContainerInterface $container
50
     * @param array              $colors
51
     * @param null               $id
52
     *
53
     * @return $this|ElementInterface
54
     */
55 2
    public static function verticalFromTop(ContainerInterface $container, array $colors, $id = null)
56
    {
57 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('topBottom'));
58
    }
59
60
    /**
61
     * @param ContainerInterface $container
62
     * @param array              $colors
63
     * @param null               $id
64
     *
65
     * @return $this|ElementInterface
66
     */
67 2
    public static function verticalFromBottom(ContainerInterface $container, array $colors, $id = null)
68
    {
69 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomTop'));
70
    }
71
72
    /**
73
     * @param ContainerInterface $container
74
     * @param array              $colors
75
     * @param null               $id
76
     *
77
     * @return $this|ElementInterface
78
     */
79 2
    public static function diagonalFromTopLeft(ContainerInterface $container, array $colors, $id = null)
80
    {
81 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('topLeftBottomRight'));
82
    }
83
84
    /**
85
     * @param ContainerInterface $container
86
     * @param array              $colors
87
     * @param null               $id
88
     *
89
     * @return $this|ElementInterface
90
     */
91 2
    public static function diagonalFromBottomRight(ContainerInterface $container, array $colors, $id = null)
92
    {
93 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomRightTopLeft'));
94
    }
95
96
    /**
97
     * @param ContainerInterface $container
98
     * @param array              $colors
99
     * @param null               $id
100
     *
101
     * @return $this|ElementInterface
102
     */
103 2
    public static function diagonalFromBottomLeft(ContainerInterface $container, array $colors, $id = null)
104
    {
105 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomLeftTopRight'));
106
    }
107
108
    /**
109
     * @param ContainerInterface $container
110
     * @param array              $colors
111
     * @param null               $id
112
     *
113
     * @return $this|ElementInterface
114
     */
115 2
    public static function diagonalFromTopRight(ContainerInterface $container, array $colors, $id = null)
116
    {
117 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('topRightBottomLeft'));
118
    }
119
120
    /**
121
     * @param ContainerInterface $container
122
     * @param array              $colors
123
     * @param null               $id
124
     *
125
     * @return $this|ElementInterface
126
     */
127 2
    public static function horizontalFromLeft(ContainerInterface $container, array $colors, $id = null)
128
    {
129 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('leftRight'));
130
    }
131
132
    /**
133
     * @param ContainerInterface $container
134
     * @param array              $colors
135
     * @param null               $id
136
     *
137
     * @return $this|ElementInterface
138
     */
139 2
    public static function horizontalFromRight(ContainerInterface $container, array $colors, $id = null)
140
    {
141 2
        return self::gradient($container, $colors, $id)->apply(Direction::get('rightLeft'));
142
    }
143
144
    /**
145
     * @param ContainerInterface $container
146
     * @param array              $colors
147
     * @param null               $id
148
     *
149
     * @return $this|ElementInterface
150
     */
151 2
    public static function radialTopLeft(ContainerInterface $container, array $colors, $id = null)
152
    {
153 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopLeft'));
154
    }
155
156
157
    /**
158
     * @param ContainerInterface $container
159
     * @param array              $colors
160
     * @param null               $id
161
     *
162
     * @return $this|ElementInterface
163
     */
164 2
    public static function radialTopRight(ContainerInterface $container, array $colors, $id = null)
165
    {
166 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopRight'));
167
    }
168
169
    /**
170
     * @param ContainerInterface $container
171
     * @param array              $colors
172
     * @param null               $id
173
     *
174
     * @return $this|ElementInterface
175
     */
176 2
    public static function radialBottomLeft(ContainerInterface $container, array $colors, $id = null)
177
    {
178 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomLeft'));
179
    }
180
181
    /**
182
     * @param ContainerInterface $container
183
     * @param array              $colors
184
     * @param null               $id
185
     *
186
     * @return $this|ElementInterface
187
     */
188 2
    public static function radialBottomRight(ContainerInterface $container, array $colors, $id = null)
189
    {
190 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomRight'));
191
    }
192
193
    /**
194
     * @param ContainerInterface $container
195
     * @param array              $colors
196
     * @param null               $id
197
     *
198
     * @return $this|ElementInterface
199
     */
200 2
    public static function radialTopCenter(ContainerInterface $container, array $colors, $id = null)
201
    {
202 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopCenter'));
203
    }
204
205
    /**
206
     * @param ContainerInterface $container
207
     * @param array              $colors
208
     * @param null               $id
209
     *
210
     * @return $this|ElementInterface
211
     */
212 2
    public static function radialLeftCenter(ContainerInterface $container, array $colors, $id = null)
213
    {
214 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialLeftCenter'));
215
    }
216
217
    /**
218
     * @param ContainerInterface $container
219
     * @param array              $colors
220
     * @param null               $id
221
     *
222
     * @return $this|ElementInterface
223
     */
224 2
    public static function radialBottomCenter(ContainerInterface $container, array $colors, $id = null)
225
    {
226 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomCenter'));
227
    }
228
229
    /**
230
     * @param ContainerInterface $container
231
     * @param array              $colors
232
     * @param null               $id
233
     *
234
     * @return $this|ElementInterface
235
     */
236 2
    public static function radialRightCenter(ContainerInterface $container, array $colors, $id = null)
237
    {
238 2
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialRightCenter'));
239
    }
240
241
    /**
242
     * @param ElementInterface $svg
243
     * @param                  $id
244
     * @param                  $gradientType
245
     *
246
     * @return Gradient
247
     */
248 35
    private static function getGradient(ElementInterface $svg, $id, $gradientType)
249
    {
250 35
        return $gradientType === self::LINEAR ? new LinearGradient($svg, $id) : new RadialGradient($svg, $id);
251
    }
252
}