Passed
Push — master ( 7f2ac9...01b5e4 )
by Sebastian
04:22
created

setTransferEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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
   /**
48
    * @var string
49
    */
50
    protected $transferEncoding = '';
51
    
52
    public function __construct(RequestHelper_Boundaries $boundaries, string $content)
53
    {
54
        $this->boundaries = $boundaries;
55
        $this->content = $content;
56
    }
57
    
58
    public function getContentLength() : int
59
    {
60
        return strlen($this->content);
61
    }
62
    
63
   /**
64
    * Sets the name of the request parameter.
65
    * 
66
    * @param string $name
67
    * @return RequestHelper_Boundaries_Boundary
68
    */
69
    public function setName(string $name) : RequestHelper_Boundaries_Boundary
70
    {
71
        return $this->setDispositionParam('name', $name);
72
    }
73
    
74
   /**
75
    * Sets the filename to use for the content, if applicable.
76
    *  
77
    * @param string $fileName
78
    * @return RequestHelper_Boundaries_Boundary
79
    */
80
    public function setFileName(string $fileName) : RequestHelper_Boundaries_Boundary
81
    {
82
        return $this->setDispositionParam('filename', $fileName);
83
    }
84
    
85
   /**
86
    * Sets the content type to use for the content.
87
    * 
88
    * @param string $contentType
89
    * @return RequestHelper_Boundaries_Boundary
90
    */
91
    public function setContentType(string $contentType) : RequestHelper_Boundaries_Boundary
92
    {
93
        $this->contentType = $contentType;
94
        return $this;
95
    }
96
    
97
   /**
98
    * Sets the encoding to specify for the content.
99
    * 
100
    * @param string $encoding An encoding string, e.g. "UTF-8", "ASCII"
101
    * @return RequestHelper_Boundaries_Boundary
102
    */
103
    public function setContentEncoding(string $encoding) : RequestHelper_Boundaries_Boundary
104
    {
105
        $this->contentEncoding = $encoding;
106
        return $this;
107
    }
108
    
109
    public function setTransferEncoding(string $encoding) : RequestHelper_Boundaries_Boundary
110
    {
111
        $this->transferEncoding = $encoding;
112
        
113
        return $this;
114
    }
115
    
116
    protected function setDispositionParam(string $name, string $value) : RequestHelper_Boundaries_Boundary
117
    {
118
        $this->dispositionParams[$name] = $value;
119
        return $this;
120
    }
121
    
122
   /**
123
    * Renders the mime boundary text.
124
    * 
125
    * @return string
126
    */
127
    public function render()
128
    {
129
        $eol = $this->boundaries->getEOL();
130
        
131
        $lines = array();
132
        $lines[] = '--'.$this->boundaries->getMimeBoundary();
133
        $lines[] = $this->renderContentDisposition();
134
        
135
        if(!empty($this->contentType)) 
136
        {
137
            $lines[] = $this->renderContentType();
138
        }
139
        
140
        if(!empty($this->transferEncoding))
141
        {
142
            $lines[] = $this->renderTransferEncoding();
143
        }
144
        
145
        $lines[] = '';
146
        $lines[] = $this->content;
147
        
148
        return implode($eol, $lines).$eol;
149
    }
150
    
151
    protected function renderContentDisposition() : string
152
    {
153
        $result = 'Content-Disposition: form-data';
154
        
155
        foreach($this->dispositionParams as $name => $value) 
156
        {
157
            $result .= '; '.$name.'="' . $value . '"';
158
        }   
159
        
160
        return $result;
161
    }
162
    
163
    protected function renderContentType() : string
164
    {
165
        $result = 'Content-Type: ' . $this->contentType; 
166
        
167
        if(!empty($this->contentEncoding)) 
168
        {
169
            $result .= '; charset="' . $this->contentEncoding.'"';
170
        }
171
        
172
        return $result;
173
    }
174
    
175
    protected function renderTransferEncoding() : string
176
    {
177
        $result = 'Content-Transfer-Encoding: ' . $this->transferEncoding;
178
        
179
        return $result;
180
    }
181
}
182