1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* responsive-images-css |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Respimgcss |
8
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
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\Infrastructure; |
38
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface; |
40
|
|
|
use Jkphl\Respimgcss\Domain\Contract\CssMediaConditionInterface as DomainMediaConditionInterface; |
41
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition; |
42
|
|
|
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition; |
43
|
|
|
use Jkphl\Respimgcss\Infrastructure\CssMediaConditionInterface as RenderableMediaConditionInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* CSS media condition factory |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\Respimgcss |
49
|
|
|
* @subpackage Jkphl\Respimgcss\Infrastructure |
50
|
|
|
*/ |
51
|
|
|
class CssMediaConditionFactory |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Create renderable media conditions from a domain media condition |
55
|
|
|
* |
56
|
|
|
* @param DomainMediaConditionInterface $mediaCondition Domain media condition |
57
|
|
|
* |
58
|
|
|
* @return RenderableMediaConditionInterface[] Renderable media conditions |
59
|
|
|
*/ |
60
|
11 |
|
public static function createFromMediaCondition(DomainMediaConditionInterface $mediaCondition): array |
61
|
|
|
{ |
62
|
|
|
switch (true) { |
63
|
11 |
|
case $mediaCondition instanceof ResolutionMediaCondition: |
64
|
8 |
|
$renderableMediaConditions = self::createFromResolutionMediaCondition($mediaCondition); |
65
|
8 |
|
break; |
66
|
8 |
|
case $mediaCondition instanceof WidthMediaCondition: |
67
|
4 |
|
$renderableMediaConditions = self::createFromWidthMediaCondition($mediaCondition); |
68
|
4 |
|
break; |
69
|
4 |
|
case (strlen($mediaCondition->getProperty()) > 0): |
70
|
|
|
$renderableMediaConditions = [ |
71
|
2 |
|
self::createMediaCondition($mediaCondition->getProperty(), '', $mediaCondition->getValue()) |
72
|
|
|
]; |
73
|
2 |
|
break; |
74
|
|
|
default: |
75
|
2 |
|
$renderableMediaConditions = [self::createLiteralMediaCondition($mediaCondition->getValue())]; |
76
|
|
|
} |
77
|
|
|
|
78
|
11 |
|
return $renderableMediaConditions; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create renderable media conditions from a resolution media condition |
83
|
|
|
* |
84
|
|
|
* @param ResolutionMediaCondition $resolutionMediaCondition Resolution media condition |
85
|
|
|
* |
86
|
|
|
* @return RenderableMediaConditionInterface[] Renderable media conditions |
87
|
|
|
*/ |
88
|
8 |
|
protected static function createFromResolutionMediaCondition( |
89
|
|
|
ResolutionMediaCondition $resolutionMediaCondition |
90
|
|
|
): array { |
91
|
8 |
|
$resolutionValue = $resolutionMediaCondition->getValue()->getValue(); |
92
|
8 |
|
$resolutionModifier = $resolutionMediaCondition->getModifier(); |
93
|
8 |
|
$resolutionProperties = ['-webkit-%sdevice-pixel-ratio', '%sresolution', '%sresolution']; |
94
|
|
|
$resolutionValues = [ |
95
|
8 |
|
strval($resolutionValue), |
96
|
8 |
|
round($resolutionValue * 96).'dpi', |
97
|
8 |
|
$resolutionValue.'ddpx' |
98
|
|
|
]; |
99
|
|
|
|
100
|
8 |
|
return array_map( |
101
|
8 |
|
function ($resolutionProperty, $resolutionValue) use ($resolutionModifier) { |
102
|
8 |
|
return self::createMediaCondition($resolutionProperty, $resolutionModifier, $resolutionValue); |
103
|
8 |
|
}, |
104
|
8 |
|
$resolutionProperties, |
105
|
8 |
|
$resolutionValues |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create a renderable media condition |
111
|
|
|
* |
112
|
|
|
* @param string $property Condition property |
113
|
|
|
* @param string $modifier Condition modifier |
114
|
|
|
* @param string $value Condition value |
115
|
|
|
* |
116
|
|
|
* @return CssMediaConditionInterface Renderable media condition |
117
|
|
|
*/ |
118
|
10 |
|
protected static function createMediaCondition( |
119
|
|
|
string $property, |
120
|
|
|
string $modifier, |
121
|
|
|
string $value |
122
|
|
|
): CssMediaConditionInterface { |
123
|
10 |
|
$rule = new CssMediaConditionRule(sprintf($property, $modifier)); |
124
|
10 |
|
$rule->setValue($value); |
125
|
|
|
|
126
|
10 |
|
return new CssMediaCondition($rule); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Create a literal media condition |
131
|
|
|
* |
132
|
|
|
* @param string $value Condition value |
133
|
|
|
* |
134
|
|
|
* @return CssLiteralMediaCondition Literal media condition |
135
|
|
|
*/ |
136
|
2 |
|
protected static function createLiteralMediaCondition(string $value): CssLiteralMediaCondition |
137
|
|
|
{ |
138
|
2 |
|
return new CssLiteralMediaCondition($value); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Create renderable media conditions from a width media condition |
143
|
|
|
* |
144
|
|
|
* @param WidthMediaCondition $widthMediaCondition Resolution media condition |
145
|
|
|
* |
146
|
|
|
* @return RenderableMediaConditionInterface[] Renderable media conditions |
147
|
|
|
*/ |
148
|
4 |
|
protected static function createFromWidthMediaCondition(WidthMediaCondition $widthMediaCondition): array |
149
|
|
|
{ |
150
|
4 |
|
$widthValue = $widthMediaCondition->getValue(); |
151
|
4 |
|
$widthUnit = ($widthValue instanceof UnitLengthInterface) ? $widthValue->getUnit() : ''; |
152
|
4 |
|
$widthModifier = $widthMediaCondition->getModifier(); |
153
|
4 |
|
$widthRule = new CssMediaConditionRule(sprintf('%swidth', $widthModifier)); |
154
|
4 |
|
$widthRule->setValue($widthValue->getValue().$widthUnit); |
155
|
|
|
|
156
|
4 |
|
return [new CssMediaCondition($widthRule)]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|