1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\File; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\File\Attributes\ContentTypeAttribute; |
15
|
|
|
use Liip\ImagineBundle\File\Attributes\ExtensionAttribute; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
* |
20
|
|
|
* @author Rob Frawley 2nd <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractFileBlob |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContentTypeAttribute |
26
|
|
|
*/ |
27
|
|
|
protected $contentType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ExtensionAttribute |
31
|
|
|
*/ |
32
|
|
|
protected $extension; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ContentTypeAttribute|null $contentType |
36
|
|
|
* @param ExtensionAttribute|null $extension |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ContentTypeAttribute $contentType = null, ExtensionAttribute $extension = null) |
39
|
|
|
{ |
40
|
|
|
$this->contentType = $contentType ?: new ContentTypeAttribute(); |
41
|
|
|
$this->extension = $extension ?: new ExtensionAttribute(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function __toString(): string |
48
|
|
|
{ |
49
|
|
|
return $this->getContents() ?: ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getContentType(): ContentTypeAttribute |
53
|
|
|
{ |
54
|
|
|
return $this->contentType; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function hasContentType(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->getContentType()->isValid(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getExtension(): ExtensionAttribute |
63
|
|
|
{ |
64
|
|
|
return $this->extension; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function hasExtension(): bool |
68
|
|
|
{ |
69
|
|
|
return $this->getExtension()->isValid(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getContents(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->doGetContents(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function hasContents(): bool |
78
|
|
|
{ |
79
|
|
|
return null !== $this->doGetContents(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $contents |
84
|
|
|
* @param bool $append |
85
|
|
|
* |
86
|
|
|
* @return FileInterface |
87
|
|
|
*/ |
88
|
|
|
public function setContents(string $contents = '', bool $append = false): FileInterface |
89
|
|
|
{ |
90
|
|
|
$this->doSetContents($contents, $append); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function getContentsLength(): int |
99
|
|
|
{ |
100
|
|
|
return mb_strlen($this->getContents()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function hasFile(): bool |
104
|
|
|
{ |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return null|string |
110
|
|
|
*/ |
111
|
|
|
abstract protected function doGetContents(): ?string; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $contents |
115
|
|
|
* @param bool $append |
116
|
|
|
*/ |
117
|
|
|
abstract protected function doSetContents(string $contents, bool $append): void; |
118
|
|
|
} |
119
|
|
|
|