1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@link RequestHelper_Boundaries_Boundary} class. |
4
|
|
|
* @package Application Utils |
5
|
|
|
* @subpackage RequestHelper |
6
|
|
|
* @see RequestHelper_Boundaries_Boundary |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace AppUtils; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Handles the rendering of a single boundary. |
15
|
|
|
* |
16
|
|
|
* @package Application Utils |
17
|
|
|
* @subpackage RequestHelper |
18
|
|
|
* @author Sebastian Mordziol <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class RequestHelper_Boundaries_Boundary |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $content; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $dispositionParams = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $contentType = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $contentEncoding = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var RequestHelper_Boundaries |
44
|
|
|
*/ |
45
|
|
|
protected $boundaries; |
46
|
|
|
|
47
|
|
|
public function __construct(RequestHelper_Boundaries $boundaries, string $content) |
48
|
|
|
{ |
49
|
|
|
$this->boundaries = $boundaries; |
50
|
|
|
$this->content = $content; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getContentLength() : int |
54
|
|
|
{ |
55
|
|
|
return strlen($this->content); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setName(string $name) : RequestHelper_Boundaries_Boundary |
59
|
|
|
{ |
60
|
|
|
return $this->setDispositionParam('name', $name); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setFileName(string $fileName) : RequestHelper_Boundaries_Boundary |
64
|
|
|
{ |
65
|
|
|
return $this->setDispositionParam('filename', $fileName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function setContentType(string $contentType) : RequestHelper_Boundaries_Boundary |
69
|
|
|
{ |
70
|
|
|
$this->contentType = $contentType; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setContentEncoding(string $encoding) : RequestHelper_Boundaries_Boundary |
75
|
|
|
{ |
76
|
|
|
$this->contentEncoding = $encoding; |
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function setDispositionParam(string $name, string $value) : RequestHelper_Boundaries_Boundary |
81
|
|
|
{ |
82
|
|
|
$this->dispositionParams[$name] = $value; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function render() |
87
|
|
|
{ |
88
|
|
|
$eol = $this->boundaries->getEOL(); |
89
|
|
|
|
90
|
|
|
$lines = array(); |
91
|
|
|
$lines[] = '--'.$this->boundaries->getMimeBoundary(); |
92
|
|
|
$lines[] = $this->renderContentDisposition(); |
93
|
|
|
|
94
|
|
|
if(!empty($this->contentType)) { |
95
|
|
|
$lines[] = $this->renderContentType(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$lines[] = $eol; |
99
|
|
|
$lines[] = $this->content; |
100
|
|
|
|
101
|
|
|
return implode($eol, $lines).$eol; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function renderContentDisposition() : string |
105
|
|
|
{ |
106
|
|
|
$result = 'Content-Disposition: form-data'; |
107
|
|
|
|
108
|
|
|
foreach($this->dispositionParams as $name => $value) |
109
|
|
|
{ |
110
|
|
|
$result .= '; '.$name.'="' . $value . '"'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $result; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function renderContentType() : string |
117
|
|
|
{ |
118
|
|
|
$result = 'Content-Type: ' . $this->contentType; |
119
|
|
|
|
120
|
|
|
if(!empty($this->contentEncoding)) |
121
|
|
|
{ |
122
|
|
|
$result .= '; charset=' . $this->contentEncoding; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|