|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* responsive-images-css |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Respimgcss |
|
8
|
|
|
* @subpackage Jkphl\Respimgcss\Application |
|
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\Application\Factory; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException; |
|
40
|
|
|
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface; |
|
41
|
|
|
use Jkphl\Respimgcss\Domain\Model\DensityImageCandidate; |
|
42
|
|
|
use Jkphl\Respimgcss\Domain\Model\WidthImageCandidate; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Image candidate factory |
|
46
|
|
|
* |
|
47
|
|
|
* @package Jkphl\Respimgcss |
|
48
|
|
|
* @subpackage Jkphl\Respimgcss\Application |
|
49
|
|
|
*/ |
|
50
|
|
|
class ImageCandidateFactory |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* Create an image candidate from an image candidate string |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $imageCandidateString Image Candidate string |
|
56
|
|
|
* |
|
57
|
|
|
* @return ImageCandidateInterface Image candidate |
|
58
|
|
|
* @throws InvalidArgumentException If the image candidate string is invalid |
|
59
|
|
|
*/ |
|
60
|
12 |
|
public static function createImageCandidateFromString(string $imageCandidateString): ImageCandidateInterface |
|
61
|
|
|
{ |
|
62
|
12 |
|
$imageCandidateStringParts = (array)(preg_split('/\s+/', trim($imageCandidateString)) ?: null); |
|
63
|
12 |
|
$imageCandidateStringParts = array_replace([null, '1x'], $imageCandidateStringParts); |
|
64
|
|
|
|
|
65
|
|
|
// If the image candidate string is invalid |
|
66
|
12 |
|
if (count($imageCandidateStringParts) !== 2) { |
|
67
|
1 |
|
throw new InvalidArgumentException( |
|
68
|
1 |
|
sprintf(InvalidArgumentException::INVALID_IMAGE_CANDIDATE_STRING_STR, $imageCandidateString), |
|
69
|
1 |
|
InvalidArgumentException::INVALID_IMAGE_CANDIDATE_STRING |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
11 |
|
return self::createImageCandidateFromFileAndDescriptor( |
|
74
|
11 |
|
$imageCandidateStringParts[0], |
|
75
|
11 |
|
$imageCandidateStringParts[1] |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Create an image candidate from a file path and descriptor |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $imageCandidateFile Image candidate file path and name |
|
83
|
|
|
* @param string $imageCandidateDescriptor Image candidate descriptor |
|
84
|
|
|
* |
|
85
|
|
|
* @return ImageCandidateInterface Image candidate |
|
86
|
|
|
*/ |
|
87
|
16 |
|
public static function createImageCandidateFromFileAndDescriptor( |
|
88
|
|
|
string $imageCandidateFile, |
|
89
|
|
|
string $imageCandidateDescriptor = '1x' |
|
90
|
|
|
): ImageCandidateInterface { |
|
91
|
16 |
|
$imageCandidateFile = self::validateImageCandidate($imageCandidateFile); |
|
92
|
15 |
|
list($imageCandidateType, $imageCandidateValue) = self::matchImageCandidateTypeValue($imageCandidateDescriptor); |
|
93
|
14 |
|
return ($imageCandidateType == ImageCandidateInterface::TYPE_DENSITY) ? |
|
94
|
8 |
|
new DensityImageCandidate($imageCandidateFile, $imageCandidateValue) : |
|
95
|
14 |
|
new WidthImageCandidate($imageCandidateFile, $imageCandidateValue); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Validate and image candidate file |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $imageCandidateFile Image candidate file |
|
102
|
|
|
* |
|
103
|
|
|
* @return string Validated image candidate file |
|
104
|
|
|
* @throws InvalidArgumentException If the Image candidate file is invalid |
|
105
|
|
|
*/ |
|
106
|
16 |
|
protected static function validateImageCandidate(string $imageCandidateFile): string |
|
107
|
|
|
{ |
|
108
|
16 |
|
$imageCandidateFile = trim($imageCandidateFile); |
|
109
|
|
|
|
|
110
|
|
|
// If the Image candidate file is invalid |
|
111
|
16 |
|
if (!strlen($imageCandidateFile)) { |
|
112
|
1 |
|
throw new InvalidArgumentException( |
|
113
|
1 |
|
sprintf(InvalidArgumentException::INVALID_IMAGE_CANDIDATE_FILE_STR, $imageCandidateFile), |
|
114
|
1 |
|
InvalidArgumentException::INVALID_IMAGE_CANDIDATE_FILE |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
15 |
|
return $imageCandidateFile; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Match the image candidate type and value |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $imageCandidateDescriptor Image candidate descriptor |
|
125
|
|
|
* |
|
126
|
|
|
* @return string[] Image candidate type and value |
|
127
|
|
|
* @throws InvalidArgumentException If the Image candidate descriptor is invalid |
|
128
|
|
|
*/ |
|
129
|
15 |
|
protected static function matchImageCandidateTypeValue(string $imageCandidateDescriptor): array |
|
130
|
|
|
{ |
|
131
|
|
|
// If the image candidate list is invalid |
|
132
|
15 |
|
if (!preg_match('/^(\d+)(w|x)$/', trim($imageCandidateDescriptor), $imageCandidateDescriptorMatch)) { |
|
133
|
1 |
|
throw new InvalidArgumentException( |
|
134
|
1 |
|
sprintf(InvalidArgumentException::INVALID_IMAGE_CANDIDATE_DESCRIPTOR_STR, $imageCandidateDescriptor), |
|
135
|
1 |
|
InvalidArgumentException::INVALID_IMAGE_CANDIDATE_DESCRIPTOR |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
14 |
|
return [$imageCandidateDescriptorMatch[2], $imageCandidateDescriptorMatch[1]]; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|