forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetDescriptor.php
More file actions
332 lines (291 loc) · 8.95 KB
/
Copy pathAssetDescriptor.php
File metadata and controls
332 lines (291 loc) · 8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Asset;
use Cloudinary\ArrayUtils;
use Cloudinary\FileUtils;
use Cloudinary\JsonUtils;
/**
* Class AssetDescriptor
*
* @property string $suffix SEO URL suffix.
*
* @api
*/
class AssetDescriptor implements AssetInterface
{
/**
* @var string $assetType The type of the asset, A.K.A resource type.
* Use the constants defined in the AssetType class.
* @see AssetType
*/
public string $assetType = AssetType::IMAGE;
/**
* @var string $deliveryType The delivery type of the asset, A.K.A type.
* Use the constants defined in the DeliveryType class.
* @see DeliveryType
*/
public string $deliveryType = DeliveryType::UPLOAD; // A.K.A type
/**
* @var int|string|null $version Asset version, typically set to unix timestamp.
*/
public string|int|null $version = null;
/**
* @var ?string $location Can be directory, URL(including path, excluding filename), etc.
*/
public ?string $location = null;
/**
* @var ?string $filename Basename without extension.
*/
public ?string $filename = null;
/**
* @var ?string $extension A.K.A format.
*/
public ?string $extension = null;
/**
* @var ?string $suffix SEO URL suffix.
*/
private ?string $suffix = null;
/**
* AssetDescriptor constructor.
*
* @param string $publicId The public ID of the asset.
* @param string $assetType The type of the asset.
*/
public function __construct(string $publicId, string $assetType = AssetType::IMAGE)
{
$this->setPublicId($publicId);
$this->assetType = $assetType;
}
/**
* Gets inaccessible class property by name.
*
* @param string $name The name of the property.
*
* @return string|null
*/
public function __get(string $name)
{
if ($name === 'suffix') {
return $this->suffix;
}
trigger_error('Undefined property: ' . static::class . '::$' . $name);
return null;
}
/**
* Indicates whether the inaccessible class property is set.
*
* @param string $key The class property name.
*
* @return bool
*/
public function __isset(string $key)
{
try {
if (null === $this->__get($key)) {
return false;
}
} catch (\Exception $e) { // Undefined property
return false;
}
return true;
}
/**
* Sets the inaccessible class property.
*
* @param string $name The class property name.
* @param mixed $value The class property value.
*/
public function __set(string $name, mixed $value)
{
$this->setAssetProperty($name, $value);
}
/**
* Sets the public ID of the asset.
*
* @param string $publicId The public ID of the asset.
*
* @return $this
*/
public function setPublicId(string $publicId): static
{
list($this->location, $this->filename, $this->extension) = FileUtils::splitPathFilenameExtension($publicId);
return $this;
}
/**
* Gets the public ID of the asset
*
* @param bool $noExtension When true, omits file extension.
*
*/
public function publicId(bool $noExtension = false): string
{
return ArrayUtils::implodeFiltered(
'.',
[
ArrayUtils::implodeFiltered('/', [$this->location, $this->filename]),
$noExtension ? '' : $this->extension,
]
);
}
/**
* Sets the URL SEO suffix of the asset.
*
* @param ?string $suffix The SEO suffix.
*
* @return $this
*/
public function setSuffix(?string $suffix): static
{
if (is_null($suffix)) {
return $this;
}
if (str_contains($suffix, '.') || str_contains($suffix, '/')) {
throw new \UnexpectedValueException(static::class . '::$suffix must not include . or /');
}
$this->suffix = $suffix;
return $this;
}
/**
* Creates a new asset from the provided string (URL).
*
* @param string $string The asset string (URL).
*
*/
public static function fromString(string $string): mixed
{
throw new \BadMethodCallException('Not Implemented');
}
/**
* Creates a new asset from the provided JSON.
*
* @param array|string $json The asset json. Can be an array or a JSON string.
*
*/
public static function fromJson(array|string $json): AssetDescriptor
{
$new = new self('');
$new->importJson($json);
return $new;
}
/**
* Creates a new asset from the provided source and an array of (legacy) parameters.
*
* @param string $source The public ID of the asset.
* @param array $params The asset parameters.
*
*/
public static function fromParams(string $source, array $params): AssetDescriptor
{
$assetJson = [
'asset_type' => ArrayUtils::get($params, 'resource_type', AssetType::IMAGE),
'delivery_type' => ArrayUtils::get($params, 'type', DeliveryType::UPLOAD),
'version' => ArrayUtils::get($params, 'version'),
'suffix' => ArrayUtils::get($params, 'url_suffix'),
];
list(
$assetJson['location'],
$assetJson['filename'],
$assetJson['extension']
)
= FileUtils::splitPathFilenameExtension($source);
// Explicit 'format' parameter overrides extension. (Fetch URLs are not affected).
if ($assetJson['delivery_type'] != DeliveryType::FETCH
|| ! ArrayUtils::get($params, 'use_fetch_format', false)
) {
ArrayUtils::addNonEmpty($assetJson, 'extension', ArrayUtils::get($params, 'format'));
}
return self::fromJson(['asset' => $assetJson]);
}
/**
* Imports data from the provided string (URL).
*
* @param string $string The asset string (URL).
*
*/
public function importString(string $string): mixed
{
throw new \BadMethodCallException('Not Implemented');
}
/**
* Imports data from the provided JSON.
*
* @param array|string $json The asset json. Can be an array or a JSON string.
*
*/
public function importJson(array|string $json): static
{
$json = JsonUtils::decode($json);
if (! array_key_exists('asset', $json) || ! array_key_exists('filename', $json['asset'])) {
throw new \InvalidArgumentException('Invalid asset JSON');
}
$assetJson = $json['asset'];
$this->assetType = ArrayUtils::get($assetJson, 'asset_type', AssetType::IMAGE);
$this->deliveryType = ArrayUtils::get($assetJson, 'delivery_type', DeliveryType::UPLOAD);
$this->version = ArrayUtils::get($assetJson, 'version');
$this->location = ArrayUtils::get($assetJson, 'location');
$this->filename = ArrayUtils::get($assetJson, 'filename');
$this->extension = ArrayUtils::get($assetJson, 'extension');
$this->setSuffix(ArrayUtils::get($assetJson, 'suffix'));
return $this;
}
/**
* Serializes to string.
*
* @return string
*/
public function __toString()
{
return ArrayUtils::implodeUrl(array_values($this->jsonSerialize()));
}
/**
* Serializes to json.
*
* @param bool $includeEmptyKeys Whether to include empty keys.
*
*/
public function jsonSerialize(bool $includeEmptyKeys = false): array
{
$dataArr = [
'asset_type' => $this->assetType,
'delivery_type' => $this->deliveryType,
'version' => $this->version,
'location' => $this->location,
'filename' => $this->filename,
'extension' => $this->extension,
'suffix' => $this->suffix,
];
if (! $includeEmptyKeys) {
$dataArr = array_filter($dataArr);
}
return ['asset' => $dataArr];
}
/**
* Sets the property of the asset descriptor.
*
* @param string $propertyName The name of the property.
* @param mixed $propertyValue The value of the property.
*
* @return $this
*
* @internal
*/
public function setAssetProperty(string $propertyName, mixed $propertyValue): static
{
/** @noinspection DegradedSwitchInspection */
switch ($propertyName) {
case 'suffix':
$this->setSuffix($propertyValue);
break;
default:
$this->$propertyName = $propertyValue;
}
return $this;
}
}