1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of InFw\File package. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace InFw\File; |
8
|
|
|
|
9
|
|
|
use InFw\Size\SizeInterface; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AbstractFile. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractFile implements FileInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* File name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* File mime type. |
26
|
|
|
* |
27
|
|
|
* @var MimeTypeInterface |
28
|
|
|
*/ |
29
|
|
|
protected $mimeType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* File size. |
33
|
|
|
* |
34
|
|
|
* @var SizeInterface |
35
|
|
|
*/ |
36
|
|
|
protected $size; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* File tmp path name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $tmpName; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AbstractFile constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @param MimeTypeInterface $mimeType |
50
|
|
|
* @param SizeInterface $size |
51
|
|
|
* @param string $tmpName |
52
|
|
|
*/ |
53
|
27 |
|
public function __construct($name, MimeTypeInterface $mimeType, SizeInterface $size, $tmpName) |
54
|
|
|
{ |
55
|
27 |
|
$this->name = $name; |
56
|
27 |
|
$this->mimeType = $mimeType; |
57
|
27 |
|
$this->size = $size; |
58
|
|
|
|
59
|
27 |
|
if (false === file_exists($tmpName)) { |
60
|
3 |
|
throw new InvalidArgumentException( |
61
|
3 |
|
'File ' . $tmpName . ' does not exists.' |
62
|
2 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
if (false === $this->mimeType->isValid($tmpName, [$this->mimeType->get()])) { |
66
|
3 |
|
throw new InvalidArgumentException( |
67
|
3 |
|
'File ' . $tmpName . ' and given mime type don\'t match.' |
68
|
2 |
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
21 |
|
$this->tmpName = $tmpName; |
72
|
21 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Obtain File name. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
6 |
|
public function getName() |
80
|
|
|
{ |
81
|
6 |
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Obtain File mime type. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
9 |
|
public function getMimeType() |
90
|
|
|
{ |
91
|
9 |
|
return (string) $this->mimeType; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Obtain File size in kb. |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
6 |
|
public function getSize() |
100
|
|
|
{ |
101
|
6 |
|
return (int) $this->size; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Obtain File tmp path name. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
6 |
|
public function getTmpName() |
110
|
|
|
{ |
111
|
6 |
|
return $this->tmpName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Specify data which should be serialized to JSON. |
116
|
|
|
* |
117
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
118
|
|
|
* |
119
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
120
|
|
|
* which is a value of any type other than a resource. |
121
|
|
|
* |
122
|
|
|
* @since 5.4.0 |
123
|
|
|
*/ |
124
|
3 |
|
public function jsonSerialize() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
3 |
|
'name' => $this->name, |
128
|
3 |
|
'mimeType' => $this->mimeType->get(), |
129
|
3 |
|
'size' => $this->size->get(), |
130
|
2 |
|
]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|