Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
21 / 21
CRAP
100.00% covered (success)
100.00%
1 / 1
Body
100.00% covered (success)
100.00%
57 / 57
100.00% covered (success)
100.00%
21 / 21
38
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 setContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setContentFromFile
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEncoding
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
5
 getEncoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasEncoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isBase64Encoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isQuotedEncoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isUrlEncoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRawUrlEncoding
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSplit
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSplit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasSplit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAsFile
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAsEncoded
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isEncoded
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
8
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Mime\Part;
15
16/**
17 * MIME part body class
18 *
19 * @category   Pop
20 * @package    Pop\Mime
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    1.1.0
25 */
26class Body
27{
28
29    /**
30     * Encoding constants
31     * @var string
32     */
33    const BASE64  = 'BASE64';
34    const QUOTED  = 'QUOTED';
35    const URL     = 'URL';
36    const RAW_URL = 'RAW_URL';
37
38    /**
39     * Content
40     * @var string
41     */
42    protected $content = null;
43
44    /**
45     * Encoding
46     * @var string
47     */
48    protected $encoding = null;
49
50    /**
51     * Chunk split
52     * @var int|boolean
53     */
54    protected $split = null;
55
56    /**
57     * Is file flag
58     * @var boolean
59     */
60    protected $isFile = false;
61
62    /**
63     * Is encoded flag
64     * @var boolean
65     */
66    protected $isEncoded = false;
67
68    /**
69     * Constructor
70     *
71     * Instantiate the body object
72     *
73     * @param string $content
74     * @param string $encoding
75     * @param int    $split
76     */
77    public function __construct($content = null, $encoding = null, $split = null)
78    {
79        if (null !== $content) {
80            $this->setContent($content);
81        }
82        if (null !== $encoding) {
83            $this->setEncoding($encoding);
84        }
85        if (null !== $split) {
86            $this->setSplit($split);
87        }
88    }
89
90    /**
91     * Set the body content
92     *
93     * @param  string $content
94     * @return Body
95     */
96    public function setContent($content)
97    {
98        $this->content = $content;
99        return $this;
100    }
101
102    /**
103     * Set the body content from file
104     *
105     * @param  string $file
106     * @param  string $encoding
107     * @param  string $split
108     * @throws Exception
109     * @return Body
110     */
111    public function setContentFromFile($file, $encoding = null, $split = null)
112    {
113        if (!file_exists($file)) {
114            throw new Exception("Error: The file '" . $file . "' does not exist.");
115        }
116
117        $this->content = file_get_contents($file);
118        $this->setAsFile(true);
119
120        if (null !== $encoding) {
121            $this->setEncoding($encoding);
122        }
123        if (null !== $split) {
124            $this->setSplit($split);
125        }
126
127        return $this;
128    }
129
130    /**
131     * Get the body content
132     *
133     * @return string
134     */
135    public function getContent()
136    {
137        return $this->content;
138    }
139
140    /**
141     * Has body content
142     *
143     * @return boolean
144     */
145    public function hasContent()
146    {
147        return (null !== $this->content);
148    }
149
150    /**
151     * Set the encoding
152     *
153     * @param  string $encoding
154     * @return Body
155     */
156    public function setEncoding($encoding)
157    {
158        switch ($encoding) {
159            case self::BASE64:
160            case self::QUOTED:
161            case self::URL:
162            case self::RAW_URL:
163            $this->encoding = $encoding;
164        }
165        return $this;
166    }
167
168    /**
169     * Get the encoding
170     *
171     * @return string
172     */
173    public function getEncoding()
174    {
175        return $this->encoding;
176    }
177
178    /**
179     * Has encoding
180     *
181     * @return boolean
182     */
183    public function hasEncoding()
184    {
185        return (null !== $this->encoding);
186    }
187
188    /**
189     * Is encoding base64
190     *
191     * @return boolean
192     */
193    public function isBase64Encoding()
194    {
195        return ($this->encoding == self::BASE64);
196    }
197
198    /**
199     * Is encoding quoted-printable
200     *
201     * @return boolean
202     */
203    public function isQuotedEncoding()
204    {
205        return ($this->encoding == self::QUOTED);
206    }
207
208    /**
209     * Is encoding URL
210     *
211     * @return boolean
212     */
213    public function isUrlEncoding()
214    {
215        return ($this->encoding == self::URL);
216    }
217
218    /**
219     * Is encoding raw URL
220     *
221     * @return boolean
222     */
223    public function isRawUrlEncoding()
224    {
225        return ($this->encoding == self::RAW_URL);
226    }
227
228    /**
229     * Set the split
230     *
231     * @param  int|boolean $split
232     * @return Body
233     */
234    public function setSplit($split)
235    {
236        $this->split = $split;
237        return $this;
238    }
239
240    /**
241     * Get the split
242     *
243     * @return int|boolean
244     */
245    public function getSplit()
246    {
247        return $this->split;
248    }
249
250    /**
251     * Has split
252     *
253     * @return boolean
254     */
255    public function hasSplit()
256    {
257        return (null !== $this->split);
258    }
259
260    /**
261     * Set as file
262     *
263     * @param  boolean $isFile
264     * @return Body
265     */
266    public function setAsFile($isFile)
267    {
268        $this->isFile = (bool)$isFile;
269        return $this;
270    }
271
272    /**
273     * Is file
274     *
275     * @return boolean
276     */
277    public function isFile()
278    {
279        return $this->isFile;
280    }
281
282    /**
283     * Set as encoded
284     *
285     * @param  boolean $isEncoded
286     * @return Body
287     */
288    public function setAsEncoded($isEncoded)
289    {
290        $this->isEncoded = (bool)$isEncoded;
291        return $this;
292    }
293
294    /**
295     * Is encoded
296     *
297     * @return boolean
298     */
299    public function isEncoded()
300    {
301        return $this->isEncoded;
302    }
303
304    /**
305     * Render the body
306     *
307     * @return string
308     */
309    public function render()
310    {
311        $content = $this->content;
312
313        if (!$this->isEncoded) {
314            switch ($this->encoding) {
315                case self::BASE64:
316                    $content = base64_encode($this->content);
317                    $this->isEncoded = true;
318                    break;
319                case self::QUOTED:
320                    $content = quoted_printable_encode($this->content);
321                    $this->isEncoded = true;
322                    break;
323                case self::URL:
324                    $content = urlencode($this->content);
325                    $this->isEncoded = true;
326                    break;
327                case self::RAW_URL:
328                    $content = rawurlencode($this->content);
329                    $this->isEncoded = true;
330                    break;
331            }
332        }
333
334        if (null !== $this->split) {
335            $content = ($this->split === true) ? chunk_split($content) : chunk_split($content, (int)$this->split);
336        }
337
338        return (string)$content;
339    }
340
341    /**
342     * Render the body
343     *
344     * @return string
345     */
346    public function __toString()
347    {
348        return $this->render();
349    }
350
351}