-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAsciiEncodedStream.php
More file actions
167 lines (145 loc) · 3.07 KB
/
Copy pathAsciiEncodedStream.php
File metadata and controls
167 lines (145 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Genkgo\Mail\Stream;
use Genkgo\Mail\StreamInterface;
final class AsciiEncodedStream implements StreamInterface
{
/**
* @var string
*/
private $text;
/**
* @var int
*/
private $position = 0;
/**
* AsciiEncodedStream constructor.
* @param string $text
* @param int $lineLength
* @param string $lineBreak
*/
public function __construct(string $text, int $lineLength = 78, string $lineBreak = "\r\n")
{
$this->text = \wordwrap($text, $lineLength, $lineBreak);
}
/**
* @return string
*/
public function __toString(): string
{
return $this->text;
}
public function close(): void
{
return;
}
/**
* @return mixed
*/
public function detach()
{
$handle = \fopen('php://memory', 'r+');
if ($handle === false) {
throw new \UnexpectedValueException('Cannot open php://memory for writing');
}
\fwrite($handle, $this->text);
return $handle;
}
public function getSize(): int
{
return \strlen($this->text);
}
/**
* @return int
*/
public function tell(): int
{
return $this->position;
}
/**
* @return bool
*/
public function eof(): bool
{
return $this->position >= \strlen($this->text);
}
/**
* @return bool
*/
public function isSeekable(): bool
{
return true;
}
/**
* @param int $offset
* @param int $whence
* @return int
*/
public function seek(int $offset, int $whence = SEEK_SET): int
{
$length = \strlen($this->text);
if ($length < $offset) {
$offset = $length;
}
$this->position = $offset;
return 0;
}
/**
* @return bool
*/
public function rewind(): bool
{
$this->position = 0;
return true;
}
/**
* @return bool
*/
public function isWritable(): bool
{
return true;
}
/**
* @param string $string
* @return int
*/
public function write($string): int
{
$this->text = \substr_replace($this->text, $string, $this->position, \strlen($string));
$bytesWritten = \strlen($string);
$this->position += $bytesWritten;
return $bytesWritten;
}
/**
* @return bool
*/
public function isReadable(): bool
{
return true;
}
/**
* @param int $length
* @return string
*/
public function read(int $length): string
{
$result = \substr($this->text, $this->position, $length);
$this->position += \strlen($result);
return $result;
}
/**
* @return string
*/
public function getContents(): string
{
return \substr($this->text, $this->position);
}
/**
* @param array<int, string> $keys
* @return array<string, mixed>
*/
public function getMetadata(array $keys = []): array
{
return [];
}
}