1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace urvin\phikaru; |
4
|
|
|
|
5
|
|
|
class UrlBuilder |
6
|
|
|
{ |
7
|
|
|
const WIDTH_HEIGHT_SEPARATOR = 'x'; |
8
|
|
|
|
9
|
|
|
const CAST_RESIZE_TENSILE = 2; |
10
|
|
|
const CAST_RESIZE_PRECISE = 4; |
11
|
|
|
const CAST_RESIZE_INVERSE = 8; |
12
|
|
|
const CAST_TRIM = 16; |
13
|
|
|
const CAST_EXTENT = 32; |
14
|
|
|
const CAST_OPAGUE_BACKGROUND = 64; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $baseUrl; |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signatureSalt; |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $width = 0; |
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $height = 0; |
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $cast = 0; |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $filename = ''; |
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $extension = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $baseUrl |
47
|
|
|
* @param string $signatureSalt |
48
|
|
|
* @return UrlBuilder |
49
|
|
|
*/ |
50
|
2 |
|
public static function construct(string $baseUrl, string $signatureSalt) |
51
|
|
|
{ |
52
|
2 |
|
return new static($baseUrl, $signatureSalt); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* UrlBuilder constructor. |
57
|
|
|
* @param string $baseUrl |
58
|
|
|
* @param string $signatureSalt |
59
|
|
|
*/ |
60
|
18 |
|
public function __construct(string $baseUrl, string $signatureSalt) |
61
|
|
|
{ |
62
|
18 |
|
if(empty($baseUrl)) { |
63
|
2 |
|
throw new \InvalidArgumentException("Base URL should not be empty"); |
64
|
|
|
} |
65
|
16 |
|
$this->baseUrl = rtrim($baseUrl, '/'); |
66
|
16 |
|
$this->signatureSalt = $signatureSalt; |
67
|
16 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $filename |
71
|
|
|
* @return UrlBuilder |
72
|
|
|
*/ |
73
|
5 |
|
public function filename(string $filename): UrlBuilder |
74
|
|
|
{ |
75
|
5 |
|
if(empty($filename)) { |
76
|
1 |
|
throw new \InvalidArgumentException("Filename should not be empty"); |
77
|
|
|
} |
78
|
4 |
|
$this->filename = $filename; |
79
|
|
|
|
80
|
4 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $extension |
85
|
|
|
* @return UrlBuilder |
86
|
|
|
*/ |
87
|
5 |
|
public function extension(string $extension): UrlBuilder |
88
|
|
|
{ |
89
|
5 |
|
if(empty($extension)) { |
90
|
1 |
|
throw new \InvalidArgumentException("Extension should not be empty"); |
91
|
|
|
} |
92
|
4 |
|
$this->extension = $extension; |
93
|
|
|
|
94
|
4 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $cast |
99
|
|
|
* @return UrlBuilder |
100
|
|
|
*/ |
101
|
3 |
|
public function cast(int $cast): UrlBuilder |
102
|
|
|
{ |
103
|
3 |
|
if(empty($cast)) { |
104
|
1 |
|
$this->cast = 0; |
105
|
|
|
} |
106
|
3 |
|
elseif ($cast < 0) { |
107
|
1 |
|
throw new \InvalidArgumentException("Cast flag should not be less than nil"); |
108
|
|
|
} |
109
|
|
|
else { |
110
|
2 |
|
$this->cast |= $cast; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $value |
118
|
|
|
* @return UrlBuilder |
119
|
|
|
*/ |
120
|
3 |
|
public function width(int $value): UrlBuilder |
121
|
|
|
{ |
122
|
3 |
|
if ($value < 0) { |
123
|
1 |
|
throw new \InvalidArgumentException("Width should not be less than nil"); |
124
|
|
|
} |
125
|
2 |
|
$this->width = $value; |
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param int $value |
131
|
|
|
* @return UrlBuilder |
132
|
|
|
*/ |
133
|
3 |
|
public function height(int $value): UrlBuilder |
134
|
|
|
{ |
135
|
3 |
|
if ($value < 0) { |
136
|
1 |
|
throw new \InvalidArgumentException("Width should not be less than nil"); |
137
|
|
|
} |
138
|
2 |
|
$this->height = $value; |
139
|
2 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
3 |
|
protected function signature(): string |
146
|
|
|
{ |
147
|
3 |
|
return md5($this->signatureSalt . $this->width . $this->height . $this->cast . $this->filename); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
2 |
|
public function __toString() |
154
|
|
|
{ |
155
|
2 |
|
return join('/', [ |
156
|
2 |
|
$this->baseUrl, |
157
|
2 |
|
$this->signature(), |
158
|
2 |
|
$this->width . self::WIDTH_HEIGHT_SEPARATOR . $this->height, |
159
|
2 |
|
$this->cast, |
160
|
2 |
|
$this->filename . '.' . $this->extension |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
} |