2c805dd4716a2047fd0c88dce2a0eb903e0397cd
[yaffs-website] / vendor / mikey179 / vfsStream / src / main / php / org / bovigo / vfs / content / StringBasedFileContent.php
1 <?php
2 /**
3  * This file is part of vfsStream.
4  *
5  * For the full copyright and license information, please view the LICENSE
6  * file that was distributed with this source code.
7  *
8  * @package  org\bovigo\vfs
9  */
10 namespace org\bovigo\vfs\content;
11 /**
12  * Default implementation for file contents based on simple strings.
13  *
14  * @since  1.3.0
15  */
16 class StringBasedFileContent extends SeekableFileContent implements FileContent
17 {
18     /**
19      * actual content
20      *
21      * @type  string
22      */
23     private $content;
24
25     /**
26      * constructor
27      *
28      * @param  string  $content
29      */
30     public function __construct($content)
31     {
32         $this->content = $content;
33     }
34
35     /**
36      * returns actual content
37      *
38      * @return  string
39      */
40     public function content()
41     {
42         return $this->content;
43     }
44
45     /**
46      * returns size of content
47      *
48      * @return  int
49      */
50     public function size()
51     {
52         return strlen($this->content);
53     }
54
55     /**
56      * actual reading of length starting at given offset
57      *
58      * @param  int  $offset
59      * @param  int  $count
60      */
61     protected function doRead($offset, $count)
62     {
63         return substr($this->content, $offset, $count);
64     }
65
66     /**
67      * actual writing of data with specified length at given offset
68      *
69      * @param   string  $data
70      * @param   int     $offset
71      * @param   int     $length
72      */
73     protected function doWrite($data, $offset, $length)
74     {
75         $this->content = substr($this->content, 0, $offset)
76                        . $data
77                        . substr($this->content, $offset + $length);
78     }
79
80     /**
81      * Truncates a file to a given length
82      *
83      * @param   int  $size length to truncate file to
84      * @return  bool
85      */
86     public function truncate($size)
87     {
88         if ($size > $this->size()) {
89             // Pad with null-chars if we're "truncating up"
90             $this->content .= str_repeat("\0", $size - $this->size());
91         } else {
92             $this->content = substr($this->content, 0, $size);
93         }
94
95         return true;
96     }
97 }