1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Domain |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Respimgcss\Domain\Service; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface; |
40
|
|
|
use Jkphl\Respimgcss\Domain\Contract\CssRulesetCompilerServiceInterface; |
41
|
|
|
use Jkphl\Respimgcss\Domain\Contract\CssRulesetInterface; |
42
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateSetInterface; |
43
|
|
|
use Jkphl\Respimgcss\Domain\Contract\LengthFactoryInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Abstract CSS Ruleset compiler service |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\Respimgcss |
49
|
|
|
* @subpackage Jkphl\Respimgcss\Domain |
50
|
|
|
*/ |
51
|
|
|
abstract class AbstractCssRulesetCompilerService implements CssRulesetCompilerServiceInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* CSS Ruleset |
55
|
|
|
* |
56
|
|
|
* @var CssRulesetInterface |
57
|
|
|
*/ |
58
|
|
|
protected $cssRuleset; |
59
|
|
|
/** |
60
|
|
|
* Breakpoints |
61
|
|
|
* |
62
|
|
|
* @var AbsoluteLengthInterface[] |
63
|
|
|
*/ |
64
|
|
|
protected $breakpoints; |
65
|
|
|
/** |
66
|
|
|
* Image candidates |
67
|
|
|
* |
68
|
|
|
* @var ImageCandidateSetInterface |
69
|
|
|
*/ |
70
|
|
|
protected $imageCandidates = null; |
71
|
|
|
/** |
72
|
|
|
* Length factory |
73
|
|
|
* |
74
|
|
|
* @var LengthFactoryInterface |
75
|
|
|
*/ |
76
|
|
|
protected $lengthFactory; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* CSS Ruleset Compiler Service constructor |
80
|
|
|
* |
81
|
|
|
* @param CssRulesetInterface $cssRuleset CSS Ruleset |
82
|
|
|
* @param AbsoluteLengthInterface[] $breakpoints Breakpoints |
83
|
|
|
* @param ImageCandidateSetInterface $imageCandidates Image candidates |
84
|
|
|
* @param LengthFactoryInterface $lengthFactory Length factory |
85
|
|
|
*/ |
86
|
13 |
|
public function __construct( |
87
|
|
|
CssRulesetInterface $cssRuleset, |
88
|
|
|
array $breakpoints, |
89
|
|
|
ImageCandidateSetInterface $imageCandidates, |
90
|
|
|
LengthFactoryInterface $lengthFactory |
91
|
|
|
) { |
92
|
13 |
|
$this->cssRuleset = $cssRuleset; |
93
|
13 |
|
$this->imageCandidates = $imageCandidates; |
94
|
13 |
|
$this->lengthFactory = $lengthFactory; |
95
|
13 |
|
$this->breakpoints = $this->prepareBreakpoints($breakpoints); |
96
|
13 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Prepare the list of breakpoints |
100
|
|
|
* |
101
|
|
|
* @param AbsoluteLengthInterface[] $breakpoints Breakpoints |
102
|
|
|
* |
103
|
|
|
* @return AbsoluteLengthInterface[] Prepared breakpoints |
104
|
|
|
*/ |
105
|
13 |
|
protected function prepareBreakpoints(array $breakpoints): array |
106
|
|
|
{ |
107
|
13 |
|
usort($breakpoints, [$this, 'sortBreakpoints']); |
108
|
|
|
|
109
|
|
|
// Add a virtual zero-width breakpoint to the beginning of the list if necessary |
110
|
13 |
|
if (count($breakpoints) && ($breakpoints[0]->getValue() != 0)) { |
111
|
12 |
|
array_unshift($breakpoints, $this->lengthFactory->createAbsoluteLength(0)); |
112
|
|
|
} |
113
|
|
|
|
114
|
13 |
|
return $breakpoints; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sort two breakpoints by size |
119
|
|
|
* |
120
|
|
|
* @param AbsoluteLengthInterface $breakpoint1 Breakpoint 1 |
121
|
|
|
* @param AbsoluteLengthInterface $breakpoint2 Breakpoint 2 |
122
|
|
|
* |
123
|
|
|
* @return int Sorting |
124
|
|
|
*/ |
125
|
11 |
|
protected function sortBreakpoints(AbsoluteLengthInterface $breakpoint1, AbsoluteLengthInterface $breakpoint2): int |
126
|
|
|
{ |
127
|
11 |
|
return ($breakpoint1->getValue() == $breakpoint2->getValue()) ? 0 : |
128
|
11 |
|
($breakpoint1->getValue() > $breakpoint2->getValue()) ? 1 : -1; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|