Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
56 / 56
100.00% covered (success)
100.00%
21 / 21
CRAP
100.00% covered (success)
100.00%
1 / 1
Body
100.00% covered (success)
100.00%
56 / 56
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%
2 / 2
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-2024 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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    2.0.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 ?string $content = null;
43
44    /**
45     * Encoding
46     * @var ?string
47     */
48    protected ?string $encoding = null;
49
50    /**
51     * Chunk split
52     * @var int|bool|null
53     */
54    protected int|bool|null $split = null;
55
56    /**
57     * Is file flag
58     * @var bool
59     */
60    protected bool $isFile = false;
61
62    /**
63     * Is encoded flag
64     * @var bool
65     */
66    protected bool $isEncoded = false;
67
68    /**
69     * Constructor
70     *
71     * Instantiate the body object
72     *
73     * @param ?string       $content
74     * @param ?string       $encoding
75     * @param int|bool|null $split
76     */
77    public function __construct(?string $content = null, ?string $encoding = null, int|bool|null $split = null)
78    {
79        if ($content !== null) {
80            $this->setContent($content);
81        }
82        if ($encoding !== null) {
83            $this->setEncoding($encoding);
84        }
85        if ($split !== null) {
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(string $content): Body
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  int|bool|null $split
108     * @throws Exception
109     * @return Body
110     */
111    public function setContentFromFile(string $file, ?string $encoding = null, int|bool|null $split = null): Body
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 ($encoding !== null) {
121            $this->setEncoding($encoding);
122        }
123        if ($split !== null) {
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(): string
136    {
137        return $this->content;
138    }
139
140    /**
141     * Has body content
142     *
143     * @return bool
144     */
145    public function hasContent(): bool
146    {
147        return ($this->content !== null);
148    }
149
150    /**
151     * Set the encoding
152     *
153     * @param  string $encoding
154     * @return Body
155     */
156    public function setEncoding(string $encoding): Body
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|null
172     */
173    public function getEncoding(): string|null
174    {
175        return $this->encoding;
176    }
177
178    /**
179     * Has encoding
180     *
181     * @return bool
182     */
183    public function hasEncoding(): bool
184    {
185        return ($this->encoding !== null);
186    }
187
188    /**
189     * Is encoding base64
190     *
191     * @return bool
192     */
193    public function isBase64Encoding(): bool
194    {
195        return ($this->encoding == self::BASE64);
196    }
197
198    /**
199     * Is encoding quoted-printable
200     *
201     * @return bool
202     */
203    public function isQuotedEncoding(): bool
204    {
205        return ($this->encoding == self::QUOTED);
206    }
207
208    /**
209     * Is encoding URL
210     *
211     * @return bool
212     */
213    public function isUrlEncoding(): bool
214    {
215        return ($this->encoding == self::URL);
216    }
217
218    /**
219     * Is encoding raw URL
220     *
221     * @return bool
222     */
223    public function isRawUrlEncoding(): bool
224    {
225        return ($this->encoding == self::RAW_URL);
226    }
227
228    /**
229     * Set the split
230     *
231     * @param  int|bool $split
232     * @return Body
233     */
234    public function setSplit(int|bool $split): Body
235    {
236        $this->split = $split;
237        return $this;
238    }
239
240    /**
241     * Get the split
242     *
243     * @return int|bool
244     */
245    public function getSplit(): int|bool
246    {
247        return $this->split;
248    }
249
250    /**
251     * Has split
252     *
253     * @return bool
254     */
255    public function hasSplit(): bool
256    {
257        return ($this->split !== null);
258    }
259
260    /**
261     * Set as file
262     *
263     * @param  bool $isFile
264     * @return Body
265     */
266    public function setAsFile(bool $isFile): Body
267    {
268        $this->isFile = (bool)$isFile;
269        return $this;
270    }
271
272    /**
273     * Is file
274     *
275     * @return bool
276     */
277    public function isFile(): bool
278    {
279        return $this->isFile;
280    }
281
282    /**
283     * Set as encoded
284     *
285     * @param  bool $isEncoded
286     * @return Body
287     */
288    public function setAsEncoded(bool $isEncoded): body
289    {
290        $this->isEncoded = (bool)$isEncoded;
291        return $this;
292    }
293
294    /**
295     * Is encoded
296     *
297     * @return bool
298     */
299    public function isEncoded(): bool
300    {
301        return $this->isEncoded;
302    }
303
304    /**
305     * Render the body
306     *
307     * @return string
308     */
309    public function render(): string
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 ($this->split !== null) {
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(): string
347    {
348        return $this->render();
349    }
350
351}