Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractPart
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
9 / 9
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
20 / 20
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
 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-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\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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    3.6.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 $content = null;
46
47    /**
48     * Message part encoding
49     * @var string
50     */
51    protected $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 boolean $chunk
62     */
63    public function __construct($content, $contentType = 'text/plain', $encoding = null, $chunk = false)
64    {
65        parent::__construct();
66
67        if (null !== $encoding) {
68            $this->setEncoding($encoding);
69
70            switch ($this->encoding) {
71                case self::BASE64:
72                    $content = base64_encode($content);
73                    $this->addHeader('Content-Transfer-Encoding', 'base64');
74                    break;
75                case self::QUOTED_PRINTABLE:
76                    $content = quoted_printable_encode($content);
77                    $this->addHeader('Content-Transfer-Encoding', 'quoted-printable');
78                    break;
79                case self::BINARY:
80                    $this->addHeader('Content-Transfer-Encoding', 'binary');
81                    break;
82                case self::_7BIT:
83                    $this->addHeader('Content-Transfer-Encoding', '7bit');
84                    break;
85                case self::_8BIT:
86                    $this->addHeader('Content-Transfer-Encoding', '8bit');
87                    break;
88            }
89        }
90
91        if ($chunk) {
92            $content = chunk_split($content);
93        }
94
95        $this->setContent($content);
96        $this->setContentType($contentType);
97    }
98
99    /**
100     * Get message part content
101     *
102     * @return string
103     */
104    public function getContent()
105    {
106        return $this->content;
107    }
108
109    /**
110     * Set message part content
111     *
112     * @param  string $content
113     * @return AbstractPart
114     */
115    public function setContent($content)
116    {
117        $this->content = $content;
118        return $this;
119    }
120
121    /**
122     * Get message part encoding
123     *
124     * @return string
125     */
126    public function getEncoding()
127    {
128        return $this->encoding;
129    }
130
131    /**
132     * Set message part encoding
133     *
134     * @param  string $encoding
135     * @return AbstractPart
136     */
137    public function setEncoding($encoding)
138    {
139        $this->encoding = $encoding;
140        return $this;
141    }
142
143    /**
144     * Check if encoding in base-64
145     *
146     * @return string
147     */
148    public function isBase64()
149    {
150        return ($this->encoding == self::BASE64);
151    }
152
153    /**
154     * Get message body
155     *
156     * @return string
157     */
158    public function getBody()
159    {
160        return $this->getContent();
161    }
162
163    /**
164     * Render message
165     *
166     * @param  array $omitHeaders
167     * @return string
168     */
169    public function render(array $omitHeaders = [])
170    {
171        return $this->getHeadersAsString($omitHeaders) . Message::CRLF . $this->getBody() . Message::CRLF . Message::CRLF;
172    }
173
174    /**
175     * Render as an array of lines
176     *
177     * @param  array $omitHeaders
178     * @return array
179     */
180    public function renderAsLines(array $omitHeaders = [])
181    {
182        $lines = [];
183
184        $headers = explode(Message::CRLF, $this->getHeadersAsString($omitHeaders) . Message::CRLF);
185        $body    = explode("\n", $this->getContent());
186
187        foreach ($headers as $header) {
188            $lines[] = trim($header);
189        }
190
191        foreach ($body as $line) {
192            $lines[] = trim($line);
193        }
194
195        $lines[] = Message::CRLF;
196        $lines[] = Message::CRLF;
197
198        return $lines;
199    }
200
201}