Completed
Push — 5.x ( f07c3e...50a512 )
by Lars
06:07
created

charsetChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
/**
12
 * Handles Quoted Printable (QP) Transfer Encoding in Swift Mailer using the PHP core function.
13
 *
14
 * @author Lars Strojny
15
 */
16
class Swift_Mime_ContentEncoder_NativeQpContentEncoder implements Swift_Mime_ContentEncoder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * @var null|string
20
     */
21
    private $charset;
22
23
    /**
24
     * @param null|string $charset
25
     */
26 64
    public function __construct($charset = null)
27
    {
28 64
        $this->charset = $charset ? $charset : 'utf-8';
29 64
    }
30
31
    /**
32
     * Notify this observer that the entity's charset has changed.
33
     *
34
     * @param string $charset
35
     */
36 1
    public function charsetChanged($charset)
37
    {
38 1
        $this->charset = $charset;
39 1
    }
40
41
    /**
42
     * Encode $in to $out.
43
     *
44
     * @param Swift_OutputByteStream $os              to read from
45
     * @param Swift_InputByteStream  $is              to write to
46
     * @param int                    $firstLineOffset
47
     * @param int                    $maxLineLength   0 indicates the default length for this encoding
48
     *
49
     * @throws RuntimeException
50
     */
51 1
    public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStream $is, $firstLineOffset = 0, $maxLineLength = 0)
52
    {
53 1
        if ($this->charset !== 'utf-8') {
54
            throw new RuntimeException(
55
                sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
56
        }
57
58 1
        $string = '';
59
60 1
        while (false !== $bytes = $os->read(8192)) {
61 1
            $string .= $bytes;
62
        }
63
64 1
        $is->write($this->encodeString($string));
65 1
    }
66
67
    /**
68
     * Get the MIME name of this content encoding scheme.
69
     *
70
     * @return string
71
     */
72 1
    public function getName()
73
    {
74 1
        return 'quoted-printable';
75
    }
76
77
    /**
78
     * Encode a given string to produce an encoded string.
79
     *
80
     * @param string $string
81
     * @param int    $firstLineOffset if first line needs to be shorter
82
     * @param int    $maxLineLength   0 indicates the default length for this encoding
83
     *
84
     * @throws RuntimeException
85
     *
86
     * @return string
87
     */
88 12
    public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
89
    {
90 12
        if ($this->charset !== 'utf-8') {
91 1
            throw new RuntimeException(
92 1
                sprintf('Charset "%s" not supported. NativeQpContentEncoder only supports "utf-8"', $this->charset));
93
        }
94
95 11
        return $this->_standardize(quoted_printable_encode($string));
96
    }
97
98
    /**
99
     * Make sure CRLF is correct and HT/SPACE are in valid places.
100
     *
101
     * @param string $string
102
     *
103
     * @return string
104
     */
105 11
    protected function _standardize($string)
106
    {
107
        // transform CR or LF to CRLF
108 11
        $string = preg_replace('~=0D(?!=0A)|(?<!=0D)=0A~', '=0D=0A', $string);
109
        // transform =0D=0A to CRLF
110 11
        $string = str_replace(array("\t=0D=0A", ' =0D=0A', '=0D=0A'), array("=09\r\n", "=20\r\n", "\r\n"), $string);
111
112 11
        switch (ord(substr($string, -1))) {
113
            case 0x09:
114
                $string = substr_replace($string, '=09', -1);
115
                break;
116 11
            case 0x20:
117
                $string = substr_replace($string, '=20', -1);
118
                break;
119
        }
120
121 11
        return $string;
122
    }
123
}
124