johnnymast /
rivescript-php
| 1 | <?php |
||||
| 2 | /* |
||||
| 3 | * This file is part of Rivescript-php |
||||
| 4 | * |
||||
| 5 | * (c) Johnny Mast <[email protected]> |
||||
| 6 | * |
||||
| 7 | * For the full copyright and license information, please view the LICENSE |
||||
| 8 | * file that was distributed with this source code. |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace Axiom\Rivescript\Cortex\ContentLoader; |
||||
| 12 | |||||
| 13 | /** |
||||
| 14 | * ContentStream class |
||||
| 15 | * |
||||
| 16 | * This class is a helper class for reading from and writing to |
||||
| 17 | * io streams. |
||||
| 18 | * |
||||
| 19 | * Note: This class taken from php.net and changed to |
||||
| 20 | * be workable for this project. |
||||
| 21 | * |
||||
| 22 | * PHP version 7.4 and higher. |
||||
| 23 | * |
||||
| 24 | * @see https://www.php.net/manual/en/stream.streamwrapper.example-1.php |
||||
| 25 | * @see https://www.php.net/manual/en/class.streamwrapper |
||||
| 26 | * |
||||
| 27 | * @category Core |
||||
| 28 | * @package Cortext\ContentLoader |
||||
| 29 | * @author Johnny Mast <[email protected]> |
||||
| 30 | * @license https://opensource.org/licenses/MIT MIT |
||||
| 31 | * @link https://github.com/axiom-labs/rivescript-php |
||||
| 32 | * @since 0.4.0 |
||||
| 33 | */ |
||||
| 34 | class ContentStream |
||||
| 35 | { |
||||
| 36 | /** |
||||
| 37 | * The cursor position inside the stream. |
||||
| 38 | * |
||||
| 39 | * @var int |
||||
| 40 | */ |
||||
| 41 | private int $position = 0; |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * The variable that holds |
||||
| 45 | * the contents of the stream. |
||||
| 46 | * |
||||
| 47 | * @var string |
||||
| 48 | */ |
||||
| 49 | protected string $content = ''; |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * Specifies the URL that was passed to the original function. |
||||
| 53 | * This is currently not used. |
||||
| 54 | * |
||||
| 55 | * @var string |
||||
| 56 | */ |
||||
| 57 | protected string $path = ''; |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * The mode to operate in. |
||||
| 61 | * This is currently not used. |
||||
| 62 | * |
||||
| 63 | * @var string |
||||
| 64 | */ |
||||
| 65 | protected string $mode = ''; |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * (Re)set default values when opening a stream. |
||||
| 69 | * |
||||
| 70 | * @see https://www.php.net/manual/en/function.fopen.php |
||||
| 71 | * |
||||
| 72 | * @param string $mode The mode used to open the file, as detailed for fopen(). |
||||
| 73 | * @param string $path Specifies the URL that was passed to the original function. |
||||
| 74 | * |
||||
| 75 | * @return bool |
||||
| 76 | */ |
||||
| 77 | public function stream_open(string $path, string $mode): bool |
||||
|
0 ignored issues
–
show
The parameter
$mode is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 78 | { |
||||
| 79 | $this->content = ''; |
||||
| 80 | $this->position = 0; |
||||
| 81 | return true; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | /** |
||||
| 85 | * Read data from the stream. |
||||
| 86 | * |
||||
| 87 | * @param int $count |
||||
| 88 | * |
||||
| 89 | * @return string |
||||
| 90 | */ |
||||
| 91 | public function stream_read(int $count): string |
||||
| 92 | { |
||||
| 93 | $p =& $this->position; |
||||
| 94 | $ret = substr($this->content, $p, $count); |
||||
| 95 | $p += strlen($ret); |
||||
| 96 | return $ret; |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * Write to the content. |
||||
| 101 | * |
||||
| 102 | * @param string $data The data to add to the stream. |
||||
| 103 | * |
||||
| 104 | * @return int |
||||
| 105 | */ |
||||
| 106 | public function stream_write(string $data): int |
||||
| 107 | { |
||||
| 108 | $v =& $this->content; |
||||
| 109 | $l = strlen($data); |
||||
| 110 | $p =& $this->position; |
||||
| 111 | $v = substr($v, 0, $p) . $data . substr($v, $p += $l); |
||||
| 112 | return $l; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Return the current position |
||||
| 117 | * in the stream. |
||||
| 118 | * |
||||
| 119 | * @return int |
||||
| 120 | */ |
||||
| 121 | public function stream_tell(): int |
||||
| 122 | { |
||||
| 123 | return $this->position; |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | /** |
||||
| 127 | * Tests for end-of-stream on a pointer position. |
||||
| 128 | * |
||||
| 129 | * @return bool |
||||
| 130 | */ |
||||
| 131 | public function stream_eof(): bool |
||||
| 132 | { |
||||
| 133 | return ($this->position >= strlen($this->content)); |
||||
| 134 | } |
||||
| 135 | |||||
| 136 | /** |
||||
| 137 | * Support for fstat(). |
||||
| 138 | * |
||||
| 139 | * An array with file status, or FALSE in case of an error - see fstat() |
||||
| 140 | * for a description of this array. |
||||
| 141 | * |
||||
| 142 | * @see http://php.net/manual/streamwrapper.stream-stat.php |
||||
| 143 | * |
||||
| 144 | * @return array |
||||
| 145 | */ |
||||
| 146 | public function stream_stat(): array |
||||
| 147 | { |
||||
| 148 | return (array)$this->content; |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | /** |
||||
| 152 | * Move the cursor inside the stream to this position. |
||||
| 153 | * |
||||
| 154 | * @see https://www.php.net/fseek for more information. |
||||
| 155 | * |
||||
| 156 | * @param int $whence The seek type. |
||||
| 157 | * @param int $offset Seek this position in the stream. |
||||
| 158 | * |
||||
| 159 | * @return bool |
||||
| 160 | */ |
||||
| 161 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
||||
| 162 | { |
||||
| 163 | $l = strlen($this->content); |
||||
| 164 | $p =& $this->position; |
||||
| 165 | switch ($whence) { |
||||
| 166 | case SEEK_SET: |
||||
| 167 | $newPos = $offset; |
||||
| 168 | break; |
||||
| 169 | case SEEK_CUR: |
||||
| 170 | $newPos = $p + $offset; |
||||
| 171 | break; |
||||
| 172 | case SEEK_END: |
||||
| 173 | $newPos = $l + $offset; |
||||
| 174 | break; |
||||
| 175 | default: |
||||
| 176 | return false; |
||||
| 177 | } |
||||
| 178 | $ret = ($newPos >= 0 && $newPos <= $l); |
||||
| 179 | if ($ret) { |
||||
| 180 | $p = $newPos; |
||||
| 181 | } |
||||
| 182 | return $ret; |
||||
| 183 | } |
||||
| 184 | } |
||||
| 185 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.