-
Notifications
You must be signed in to change notification settings - Fork 102
/
StringBasedFileContent.php
106 lines (95 loc) · 2.27 KB
/
StringBasedFileContent.php
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
<?php
/**
* This file is part of vfsStream.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package org\bovigo\vfs
*/
namespace bovigo\vfs\content;
use function class_alias;
use function str_repeat;
use function strlen;
use function substr;
/**
* Default implementation for file contents based on simple strings.
*
* @since 1.3.0
*/
class StringBasedFileContent extends SeekableFileContent implements FileContent
{
/**
* actual content
*
* @type string
*/
private $content;
/**
* constructor
*
* @param string $content
*/
public function __construct($content)
{
$this->content = $content;
}
/**
* returns actual content
*
* @return string
*/
public function content()
{
return $this->content;
}
/**
* returns size of content
*
* @return int
*/
public function size()
{
return strlen($this->content);
}
/**
* actual reading of length starting at given offset
*
* @param int $offset
* @param int $count
*/
protected function doRead($offset, $count)
{
return (string) substr($this->content, $offset, $count);
}
/**
* actual writing of data with specified length at given offset
*
* @param string $data
* @param int $offset
* @param int $length
*/
protected function doWrite($data, $offset, $length)
{
$this->content = substr($this->content, 0, $offset)
. $data
. substr($this->content, $offset + $length);
}
/**
* Truncates a file to a given length
*
* @param int $size length to truncate file to
* @return bool
*/
public function truncate($size)
{
if ($size > $this->size()) {
// Pad with null-chars if we're "truncating up"
$this->content .= str_repeat("\0", $size - $this->size());
} else {
$this->content = substr($this->content, 0, $size);
}
return true;
}
}
class_alias('bovigo\vfs\content\StringBasedFileContent', 'org\bovigo\vfs\content\StringBasedFileContent');