|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use drupol\phpvfs\Node\FileInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class File. |
|
11
|
|
|
*/ |
|
12
|
|
|
class File |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \drupol\phpvfs\Node\FileInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $file; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $mode; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
private $position; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* File constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \drupol\phpvfs\Node\FileInterface $file |
|
33
|
|
|
* @param string $mode |
|
34
|
|
|
* @param int $position |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function __construct(FileInterface $file, string $mode, int $position = 0) |
|
37
|
|
|
{ |
|
38
|
5 |
|
$this->file = $file; |
|
39
|
5 |
|
$this->mode = $mode; |
|
40
|
5 |
|
$this->position = $position; |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
public function getFile(): FileInterface |
|
44
|
|
|
{ |
|
45
|
5 |
|
return $this->file; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
5 |
|
public function getMode(): string |
|
49
|
|
|
{ |
|
50
|
5 |
|
return $this->mode; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
5 |
|
public function getPosition(): int |
|
54
|
|
|
{ |
|
55
|
5 |
|
return $this->position; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function isAppendable(): bool |
|
59
|
|
|
{ |
|
60
|
|
|
$modeSplit = \str_split(\str_replace('b', '', $this->getMode())); |
|
61
|
|
|
|
|
62
|
|
|
return \in_array('a', $modeSplit, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function isExtended(): bool |
|
66
|
|
|
{ |
|
67
|
|
|
$modeSplit = \str_split(\str_replace('b', '', $this->getMode())); |
|
68
|
|
|
|
|
69
|
|
|
return \in_array('+', $modeSplit, true); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function isReadable(): bool |
|
73
|
|
|
{ |
|
74
|
|
|
$modeSplit = \str_split(\str_replace('b', '', $this->getMode())); |
|
75
|
|
|
|
|
76
|
|
|
return \in_array('r', $modeSplit, true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
public function isWritable(): bool |
|
80
|
|
|
{ |
|
81
|
5 |
|
$modeSplit = \str_split(\str_replace('b', '', $this->getMode())); |
|
82
|
|
|
|
|
83
|
5 |
|
return \in_array('w', $modeSplit, true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param int $bytes |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public function read(int $bytes): string |
|
92
|
|
|
{ |
|
93
|
|
|
$data = \substr( |
|
94
|
|
|
$this->getFile()->getAttribute('content'), |
|
95
|
|
|
$this->getPosition(), |
|
96
|
|
|
$bytes |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$this->offsetPosition($bytes); |
|
100
|
|
|
|
|
101
|
|
|
return $data; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function seekToEnd() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->setPosition( |
|
107
|
|
|
\strlen($this->getFile()->getAttribute('content')) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setMode(string $mode): File |
|
112
|
|
|
{ |
|
113
|
|
|
$this->mode = $mode; |
|
114
|
|
|
|
|
115
|
|
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
5 |
|
public function setPosition(int $bytes): File |
|
119
|
|
|
{ |
|
120
|
5 |
|
$this->position = $bytes; |
|
121
|
|
|
|
|
122
|
5 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
5 |
|
public function size(): int |
|
129
|
|
|
{ |
|
130
|
5 |
|
return \strlen($this->getFile()->getAttribute('content')); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param int $bytes |
|
135
|
|
|
*/ |
|
136
|
5 |
|
public function truncate(int $bytes = 0) |
|
137
|
|
|
{ |
|
138
|
5 |
|
$this->setPosition(0); |
|
139
|
5 |
|
$newData = \substr($this->getFile()->getAttribute('content'), 0, $bytes); |
|
140
|
|
|
|
|
141
|
5 |
|
if (\is_string($newData)) { |
|
|
|
|
|
|
142
|
5 |
|
$this->getFile()->setAttribute('content', $newData); |
|
143
|
|
|
} |
|
144
|
5 |
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $data |
|
148
|
|
|
* |
|
149
|
|
|
* @return int |
|
150
|
|
|
*/ |
|
151
|
5 |
|
public function write(string $data): int |
|
152
|
|
|
{ |
|
153
|
5 |
|
$content = $this->getFile()->getAttribute('content'); |
|
154
|
5 |
|
$content = \substr($content, 0, $this->getPosition()); |
|
155
|
|
|
|
|
156
|
5 |
|
$content .= $data; |
|
157
|
5 |
|
$this->getFile()->setAttribute('content', $content); |
|
158
|
|
|
|
|
159
|
5 |
|
$written = \strlen($data); |
|
160
|
5 |
|
$this->offsetPosition($written); |
|
161
|
|
|
|
|
162
|
5 |
|
return $written; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param int $offset |
|
167
|
|
|
* |
|
168
|
|
|
* @return int |
|
169
|
|
|
*/ |
|
170
|
5 |
|
protected function offsetPosition($offset): int |
|
171
|
|
|
{ |
|
172
|
5 |
|
$contentSize = $this->size(); |
|
173
|
5 |
|
$newPosition = $this->getPosition() + $offset; |
|
174
|
|
|
|
|
175
|
5 |
|
$newPosition = $contentSize < $newPosition ? |
|
176
|
|
|
$contentSize : |
|
177
|
5 |
|
$newPosition; |
|
178
|
|
|
|
|
179
|
5 |
|
$this->setPosition($newPosition); |
|
180
|
|
|
|
|
181
|
5 |
|
return $newPosition; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|