Completed
Push — 5.x ( ef5d14...158fb7 )
by Lars
09:14
created

Swift_ByteStream_ArrayByteStream::setReadPointer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 3.0175
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
 * Allows reading and writing of bytes to and from an array.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_ByteStream_ArrayByteStream implements Swift_InputByteStream, Swift_OutputByteStream
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
     * The internal stack of bytes.
20
     *
21
     * @var string[]
22
     */
23
    private $_array = array();
24
25
    /**
26
     * The size of the stack.
27
     *
28
     * @var int
29
     */
30
    private $_arraySize = 0;
31
32
    /**
33
     * The internal pointer offset.
34
     *
35
     * @var int
36
     */
37
    private $_offset = 0;
38
39
    /**
40
     * Bound streams.
41
     *
42
     * @var Swift_InputByteStream[]
43
     */
44
    private $_mirrors = array();
45
46
    /**
47
     * Create a new ArrayByteStream.
48
     *
49
     * If $stack is given the stream will be populated with the bytes it contains.
50
     *
51
     * @param mixed $stack of bytes in string or array form, optional
52
     */
53 32
    public function __construct($stack = null)
54
    {
55 32
        if (is_array($stack)) {
56 9
            $this->_array = $stack;
57 9
            $this->_arraySize = count($stack);
58 32
        } elseif (is_string($stack)) {
59 4
            $this->write($stack);
60 4
        } else {
61 19
            $this->_array = array();
62
        }
63 32
    }
64
65
    /**
66
     * Reads $length bytes from the stream into a string and moves the pointer
67
     * through the stream by $length.
68
     *
69
     * If less bytes exist than are requested the
70
     * remaining bytes are given instead. If no bytes are remaining at all, boolean
71
     * false is returned.
72
     *
73
     * @param int $length
74
     *
75
     * @return string
76
     */
77 29
    public function read($length)
78
    {
79 29
        if ($this->_offset === $this->_arraySize) {
80 27
            return false;
81
        }
82
83
        // Don't use array slice
84 27
        $end = $length + $this->_offset;
85 27
        $end = $this->_arraySize < $end ? $this->_arraySize : $end;
86 27
        $ret = '';
87 27
        for (; $this->_offset < $end; ++$this->_offset) {
88 27
            $ret .= $this->_array[$this->_offset];
89 27
        }
90
91 27
        return $ret;
92
    }
93
94
    /**
95
     * Writes $bytes to the end of the stream.
96
     *
97
     * @param string $bytes
98
     *
99
     * @return void
100
     */
101 24
    public function write($bytes)
102
    {
103 24
        $to_add = str_split($bytes);
104 24
        foreach ($to_add as $value) {
105 24
            $this->_array[] = $value;
106 24
        }
107
108 24
        $this->_arraySize = count($this->_array);
109
110 24
        foreach ($this->_mirrors as $stream) {
111 6
            $stream->write($bytes);
112 24
        }
113 24
    }
114
115
    /**
116
     * Not used.
117
     */
118 4
    public function commit()
119
    {
120 4
    }
121
122
    /**
123
     * Attach $is to this stream.
124
     *
125
     * The stream acts as an observer, receiving all data that is written.
126
     * All {@link write()} and {@link flushBuffers()} operations will be mirrored.
127
     *
128
     * @param Swift_InputByteStream $is
129
     */
130 7
    public function bind(Swift_InputByteStream $is)
131
    {
132 7
        $this->_mirrors[] = $is;
133 7
    }
134
135
    /**
136
     * Remove an already bound stream.
137
     *
138
     * If $is is not bound, no errors will be raised.
139
     * If the stream currently has any buffered data it will be written to $is
140
     * before unbinding occurs.
141
     *
142
     * @param Swift_InputByteStream $is
143
     */
144 5
    public function unbind(Swift_InputByteStream $is)
145
    {
146 5
        foreach ($this->_mirrors as $k => $stream) {
147 5
            if ($is === $stream) {
148 5
                unset($this->_mirrors[$k]);
149 5
            }
150 5
        }
151 5
    }
152
153
    /**
154
     * Move the internal read pointer to $byteOffset in the stream.
155
     *
156
     * @param int $byteOffset
157
     *
158
     * @return bool
159
     */
160 8
    public function setReadPointer($byteOffset)
161
    {
162 8
        if ($byteOffset > $this->_arraySize) {
163
            $byteOffset = $this->_arraySize;
164 8
        } elseif ($byteOffset < 0) {
165 1
            $byteOffset = 0;
166 1
        }
167
168 8
        $this->_offset = $byteOffset;
169 8
    }
170
171
    /**
172
     * Flush the contents of the stream (empty it) and set the internal pointer
173
     * to the beginning.
174
     */
175 2
    public function flushBuffers()
176
    {
177 2
        $this->_offset = 0;
178 2
        $this->_array = array();
179 2
        $this->_arraySize = 0;
180
181 2
        foreach ($this->_mirrors as $stream) {
182 1
            $stream->flushBuffers();
183 2
        }
184 2
    }
185
}
186