-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInfo.php
More file actions
88 lines (71 loc) 路 2.89 KB
/
Copy pathFileInfo.php
File metadata and controls
88 lines (71 loc) 路 2.89 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
<?php
declare(strict_types=1);
namespace SixtyEightPublishers\ImageStorage;
use SixtyEightPublishers\FileStorage\FileInfo as BaseFileInfo;
use SixtyEightPublishers\FileStorage\PathInfoInterface;
use SixtyEightPublishers\ImageStorage\Exception\InvalidStateException;
use SixtyEightPublishers\ImageStorage\LinkGenerator\LinkGeneratorInterface as ImageLinkGeneratorInterface;
use SixtyEightPublishers\ImageStorage\PathInfoInterface as ImagePathInfoInterface;
use SixtyEightPublishers\ImageStorage\Responsive\Descriptor\DescriptorInterface;
use SixtyEightPublishers\ImageStorage\Responsive\SrcSet;
use function assert;
final class FileInfo extends BaseFileInfo implements FileInfoInterface
{
public function __construct(ImageLinkGeneratorInterface $linkGenerator, PathInfoInterface $pathInfo, string $imageStorageName)
{
parent::__construct($linkGenerator, $pathInfo, $imageStorageName);
}
public function srcSet(?DescriptorInterface $descriptor = null, bool $absolute = true): SrcSet
{
assert($this->linkGenerator instanceof ImageLinkGeneratorInterface);
return $this->linkGenerator->srcSet(
info: $this,
descriptor: $descriptor,
absolute: $absolute,
);
}
public function getModifiers(): string|array|null
{
return $this->pathInfo instanceof ImagePathInfoInterface ? $this->pathInfo->getModifiers() : null;
}
public function withModifiers(string|array|null $modifiers): static
{
$pathInfo = $this->checkPathInfoType(__METHOD__);
$info = clone $this;
$info->pathInfo = $pathInfo->withModifiers($modifiers);
return $info;
}
public function withEncodedModifiers(string $modifiers): static
{
$pathInfo = $this->checkPathInfoType(__METHOD__);
$info = clone $this;
$info->pathInfo = $pathInfo->withEncodedModifiers($modifiers);
return $info;
}
public function toArray(): array
{
$array = parent::toArray();
# A path doesn't contain an extension if a modifier is NULL
if ($this->pathInfo instanceof ImagePathInfoInterface && null === $this->getModifiers() && null !== $this->getExtension()) {
# Save with the default file extension
$array['path'] .= '.' . $this->getExtension();
}
return $array;
}
/**
* @throws InvalidStateException
*/
private function checkPathInfoType(string $method): ImagePathInfoInterface
{
$pathInfo = $this->pathInfo;
if (!$pathInfo instanceof ImagePathInfoInterface) {
throw new InvalidStateException(sprintf(
'An instance of %s must be implementor of an interface %s if you want to use the method %s().',
get_class($this->pathInfo),
ImagePathInfoInterface::class,
$method,
));
}
return $pathInfo;
}
}