Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractPart
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
12 / 12
21
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
8
 getContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getEncoding
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
1
 isBase64
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isQuotedPrintable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is8Bit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is7Bit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 renderAsLines
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
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\Mail\Message;
15
16use Pop\Mail\Message;
17
18/**
19 * Abstract mail message part class
20 *
21 * @category   Pop
22 * @package    Pop\Mail
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28abstract class AbstractPart extends AbstractMessage implements PartInterface
29{
30
31    /**
32     * Encoding constants
33     * @var string
34     */
35    const BASE64           = 'BASE64';
36    const QUOTED_PRINTABLE = 'QUOTED_PRINTABLE';
37    const BINARY           = 'BINARY';
38    const _8BIT            = '_8BIT';
39    const _7BIT            = '_7BIT';
40
41    /**
42     * Message part content
43     * @var ?string
44     */
45    protected ?string $content = null;
46
47    /**
48     * Message part encoding
49     * @var ?string
50     */
51    protected ?string $encoding = null;
52
53    /**
54     * Constructor
55     *
56     * Instantiate the message part object
57     *
58     * @param string  $content
59     * @param string  $contentType
60     * @param ?string $encoding
61     * @param bool    $chunk
62     */
63    public function __construct(string $content, string $contentType = 'text/plain', ?string $encoding = null, bool $chunk = false)
64    {
65        if ($encoding !== null) {
66            $this->setEncoding($encoding);
67
68            switch ($this->encoding) {
69                case self::BASE64:
70                    $content = base64_encode($content);
71                    $this->addHeader('Content-Transfer-Encoding', 'base64');
72                    break;
73                case self::QUOTED_PRINTABLE:
74                    $content = quoted_printable_encode($content);
75                    $this->addHeader('Content-Transfer-Encoding', 'quoted-printable');
76                    break;
77                case self::BINARY:
78                    $this->addHeader('Content-Transfer-Encoding', 'binary');
79                    break;
80                case self::_7BIT:
81                    $this->addHeader('Content-Transfer-Encoding', '7bit');
82                    break;
83                case self::_8BIT:
84                    $this->addHeader('Content-Transfer-Encoding', '8bit');
85                    break;
86            }
87        }
88
89        if ($chunk) {
90            $content = chunk_split($content);
91        }
92
93        $this->setContent($content);
94        $this->setContentType($contentType);
95    }
96
97    /**
98     * Get message part content
99     *
100     * @return ?string
101     */
102    public function getContent(): ?string
103    {
104        return $this->content;
105    }
106
107    /**
108     * Set message part content
109     *
110     * @param  string $content
111     * @return AbstractPart
112     */
113    public function setContent(string $content): AbstractPart
114    {
115        $this->content = $content;
116        return $this;
117    }
118
119    /**
120     * Get message part encoding
121     *
122     * @return ?string
123     */
124    public function getEncoding(): ?string
125    {
126        return $this->encoding;
127    }
128
129    /**
130     * Set message part encoding
131     *
132     * @param  string $encoding
133     * @return AbstractPart
134     */
135    public function setEncoding(string $encoding): AbstractPart
136    {
137        $this->encoding = $encoding;
138        return $this;
139    }
140
141    /**
142     * Check if encoding is base64
143     *
144     * @return bool
145     */
146    public function isBase64(): bool
147    {
148        return ($this->encoding == self::BASE64);
149    }
150
151    /**
152     * Check if encoding is quoted printable
153     *
154     * @return bool
155     */
156    public function isQuotedPrintable(): bool
157    {
158        return ($this->encoding == self::QUOTED_PRINTABLE);
159    }
160
161    /**
162     * Check if encoding is 8-bit
163     *
164     * @return bool
165     */
166    public function is8Bit(): bool
167    {
168        return ($this->encoding == self::_8BIT);
169    }
170
171    /**
172     * Check if encoding is 7-bit
173     *
174     * @return bool
175     */
176    public function is7Bit(): bool
177    {
178        return ($this->encoding == self::_7BIT);
179    }
180
181    /**
182     * Get message body
183     *
184     * @return ?string
185     */
186    public function getBody(): ?string
187    {
188        return $this->getContent();
189    }
190
191    /**
192     * Render message
193     *
194     * @param  array $omitHeaders
195     * @return string
196     */
197    public function render(array $omitHeaders = []): string
198    {
199        return $this->getHeadersAsString($omitHeaders) . Message::CRLF . $this->getBody() . Message::CRLF . Message::CRLF;
200    }
201
202    /**
203     * Render as an array of lines
204     *
205     * @param  array $omitHeaders
206     * @return array
207     */
208    public function renderAsLines(array $omitHeaders = []): array
209    {
210        $lines = [];
211
212        $headers = explode(Message::CRLF, $this->getHeadersAsString($omitHeaders) . Message::CRLF);
213        $body    = explode(Message::CRLF, $this->getContent());
214
215        foreach ($headers as $header) {
216            $lines[] = trim($header);
217        }
218
219        foreach ($body as $line) {
220            $lines[] = trim($line);
221        }
222
223        $lines[] = Message::CRLF;
224        $lines[] = Message::CRLF;
225
226        return $lines;
227    }
228
229}