3 * This file is part of vfsStream.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
8 * @package org\bovigo\vfs
10 namespace org\bovigo\vfs\content;
12 * Default implementation for file contents based on simple strings.
16 class StringBasedFileContent extends SeekableFileContent implements FileContent
28 * @param string $content
30 public function __construct($content)
32 $this->content = $content;
36 * returns actual content
40 public function content()
42 return $this->content;
46 * returns size of content
50 public function size()
52 return strlen($this->content);
56 * actual reading of length starting at given offset
61 protected function doRead($offset, $count)
63 return substr($this->content, $offset, $count);
67 * actual writing of data with specified length at given offset
73 protected function doWrite($data, $offset, $length)
75 $this->content = substr($this->content, 0, $offset)
77 . substr($this->content, $offset + $length);
81 * Truncates a file to a given length
83 * @param int $size length to truncate file to
86 public function truncate($size)
88 if ($size > $this->size()) {
89 // Pad with null-chars if we're "truncating up"
90 $this->content .= str_repeat("\0", $size - $this->size());
92 $this->content = substr($this->content, 0, $size);