1
|
|
|
<?php |
2
|
|
|
namespace Staticus\Diactoros\Response; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
5
|
|
|
use Zend\Diactoros\Response; |
6
|
|
|
use Zend\Diactoros\Stream; |
7
|
|
|
|
8
|
|
|
class FileUploadedResponse extends Response implements FileResponseInterface |
9
|
|
|
{ |
10
|
|
|
use Response\InjectContentTypeTrait; |
11
|
|
|
/** |
12
|
|
|
* @var resource |
13
|
|
|
*/ |
14
|
|
|
protected $resource; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var UploadedFileInterface |
18
|
|
|
*/ |
19
|
|
|
protected $content; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return resource |
23
|
|
|
*/ |
24
|
|
|
public function getResource() |
25
|
|
|
{ |
26
|
|
|
return $this->resource; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return UploadedFileInterface |
31
|
|
|
*/ |
32
|
|
|
public function getContent() |
33
|
|
|
{ |
34
|
|
|
return $this->content; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param UploadedFileInterface $content |
39
|
|
|
*/ |
40
|
|
|
public function setContent($content) |
41
|
|
|
{ |
42
|
|
|
if (!$content instanceof UploadedFileInterface) { |
43
|
|
|
throw new \RuntimeException('Content must be an instance of UploadedFileInterface'); |
44
|
|
|
} |
45
|
|
|
$this->content = $content; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create an empty response with the given status code and attached uploaded file information. |
50
|
|
|
* |
51
|
|
|
* @param UploadedFileInterface $uploadedFile |
52
|
|
|
* @param int $status Status code for the response, if any. |
53
|
|
|
* @param array $headers Headers for the response, if any. |
54
|
|
|
*/ |
55
|
|
|
public function __construct(UploadedFileInterface $uploadedFile, $status = 204, array $headers = []) |
56
|
|
|
{ |
57
|
|
|
$this->setContent($uploadedFile); |
58
|
|
|
$body = $this->createBody(); |
59
|
|
|
parent::__construct($body, $status, $headers); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function createBody() |
63
|
|
|
{ |
64
|
|
|
$stream = new Stream('php://temp', 'r'); |
65
|
|
|
|
66
|
|
|
return $stream; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|