1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@link RequestHelper_Boundaries} class. |
4
|
|
|
* @package Application Utils |
5
|
|
|
* @subpackage RequestHelper |
6
|
|
|
* @see RequestHelper_Boundaries |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace AppUtils; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Container for the collection of boundaries that will be |
15
|
|
|
* sent in the request. |
16
|
|
|
* |
17
|
|
|
* @package Application Utils |
18
|
|
|
* @subpackage RequestHelper |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RequestHelper_Boundaries |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var RequestHelper |
25
|
|
|
*/ |
26
|
|
|
protected $helper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestHelper_Boundaries_Boundary[] |
30
|
|
|
*/ |
31
|
|
|
protected $boundaries = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
protected $contentLength = 0; |
37
|
|
|
|
38
|
|
|
public function __construct(RequestHelper $helper) |
39
|
|
|
{ |
40
|
|
|
$this->helper = $helper; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getMimeBoundary() : string |
44
|
|
|
{ |
45
|
|
|
return $this->helper->getMimeBoundary(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getEOL() : string |
49
|
|
|
{ |
50
|
|
|
return $this->helper->getEOL(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieves the total content length of all boundaries. |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
|
|
public function getContentLength() : int |
58
|
|
|
{ |
59
|
|
|
return $this->contentLength; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds a file to be sent with the request. |
64
|
|
|
* |
65
|
|
|
* @param string $varName The variable name to send the file in |
66
|
|
|
* @param string $fileName The name of the file as it should be received at the destination |
67
|
|
|
* @param string $content The raw content of the file |
68
|
|
|
* @param string $contentType The content type, use the constants to specify this |
69
|
|
|
* @param string $encoding The encoding of the file, use the constants to specify this |
70
|
|
|
*/ |
71
|
|
|
public function addFile(string $varName, string $fileName, string $content, string $contentType = RequestHelper::FILETYPE_TEXT, string $encoding = RequestHelper::ENCODING_UTF8) : RequestHelper_Boundaries |
72
|
|
|
{ |
73
|
|
|
$boundary = $this->createBoundary(chunk_split(base64_encode($content))) |
74
|
|
|
->setName($varName) |
75
|
|
|
->setFileName(basename($fileName)) |
76
|
|
|
->setContentType($contentType) |
77
|
|
|
->setContentEncding($encoding); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->addBoundary($boundary); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Adds arbitrary content. |
84
|
|
|
* |
85
|
|
|
* @param string $varName |
86
|
|
|
* @param string $content |
87
|
|
|
* @param string $contentType |
88
|
|
|
*/ |
89
|
|
|
public function addContent(string $varName, string $content, string $contentType) : RequestHelper_Boundaries |
90
|
|
|
{ |
91
|
|
|
$boundary = $this->createBoundary($content) |
92
|
|
|
->setName($varName) |
93
|
|
|
->setContentType($contentType) |
94
|
|
|
->setContentEncoding(RequestHelper::ENCODING_UTF8); |
95
|
|
|
|
96
|
|
|
return $this->addBoundary($boundary); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Adds a variable to be sent with the request. If it |
101
|
|
|
* already exists, its value is overwritten. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* @param string $value |
105
|
|
|
*/ |
106
|
|
|
public function addVariable(string $name, string $value) : RequestHelper_Boundaries |
107
|
|
|
{ |
108
|
|
|
$boundary = $this->createBoundary($value) |
109
|
|
|
->setName($name); |
110
|
|
|
|
111
|
|
|
return $this->addBoundary($boundary); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function addBoundary(RequestHelper_Boundaries_Boundary $boundary) : RequestHelper_Boundaries |
115
|
|
|
{ |
116
|
|
|
$this->boundaries[] = $boundary; |
117
|
|
|
$this->contentLength += $boundary->getContentLength(); |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function render() : string |
123
|
|
|
{ |
124
|
|
|
$result = ''; |
125
|
|
|
|
126
|
|
|
foreach($this->boundaries as $boundary) |
127
|
|
|
{ |
128
|
|
|
$result .= $boundary->render(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$result .= "--" . $this->getMimeBoundary() . "--" . |
132
|
|
|
$this->getEOL() . $this->getEOL(); // always finish with two eol's!! |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function createBoundary(string $content) : RequestHelper_Boundaries_Boundary |
138
|
|
|
{ |
139
|
|
|
return new RequestHelper_Boundaries_Boundary($this, $content); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.