1: <?php
2:
3: namespace PHPixie\HTTP\Messages;
4:
5: use PHPixie\HTTP\Messages;
6: use Psr\Http\Message\StreamInterface;
7: use Psr\Http\Message\UploadedFileInterface;
8: use RuntimeException;
9:
10: /**
11: * Base PSR-7 UploadedFile implementation
12: */
13: abstract class UploadedFile implements UploadedFileInterface
14: {
15: /**
16: * @var Messages
17: */
18: protected $messages;
19:
20: /**
21: * @var string
22: */
23: protected $clientFilename;
24:
25: /**
26: * @var string
27: */
28: protected $clientMediaType;
29:
30: /**
31: * @var string
32: */
33: protected $file;
34:
35: /**
36: * @var int
37: */
38: protected $error;
39:
40: /**
41: * @var int|null
42: */
43: protected $size;
44:
45: /**
46: * @var StreamInterface
47: */
48: protected $stream;
49:
50: /**
51: * Constructor
52: * @param Messages $messages
53: */
54: public function __construct($messages)
55: {
56: $this->messages = $messages;
57: }
58:
59: /**
60: * @inheritdoc
61: */
62: public function getStream()
63: {
64: if($this->stream === null) {
65: $this->assertValidUpload();
66: $this->stream = $this->messages->stream($this->file);
67: }
68:
69: return $this->stream;
70: }
71:
72: /**
73: * @inheritdoc
74: */
75: public function getClientFilename()
76: {
77: return $this->clientFilename;
78: }
79:
80: /**
81: * @inheritdoc
82: */
83: public function getClientMediaType()
84: {
85: return $this->clientMediaType;
86: }
87:
88: /**
89: * @inheritdoc
90: */
91: public function getError()
92: {
93: return $this->error;
94: }
95:
96: /**
97: * @inheritdoc
98: */
99: public function getSize()
100: {
101: return $this->size;
102: }
103:
104: protected function assertValidUpload()
105: {
106: if($this->error !== UPLOAD_ERR_OK) {
107: throw new RuntimeException("File was not successfully uploaded");
108: }
109: }
110:
111: /**
112: * @inheritdoc
113: */
114: abstract public function moveTo($path);
115: }
116: