pssht  latest
SSH server library written in PHP
Buffer.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12 namespace fpoirotte\Pssht;
13 
17 class Buffer implements \Countable
18 {
20  protected $data;
21 
29  public function __construct($data = '')
30  {
31  if (!is_string($data)) {
32  throw new \InvalidArgumentException();
33  }
34 
35  $this->data = $data;
36  }
37 
45  public function count()
46  {
47  return strlen($this->data);
48  }
49 
63  protected function getLength($limit)
64  {
65  $size = strlen($this->data);
66  if ($limit <= 0) {
67  $limit += $size;
68  }
69 
70  if ($limit > $size) {
71  return null;
72  }
73 
74  $res = (string) substr($this->data, 0, $limit);
75  $this->data = (string) substr($this->data, $limit);
76  return $res;
77  }
78 
93  protected function getDelimiter($limit)
94  {
95  if ($limit === '') {
96  throw new \InvalidArgumentException();
97  }
98 
99  $pos = strpos($this->data, $limit);
100  if ($pos === false) {
101  return null;
102  }
103 
104  $pos += strlen($limit);
105  $res = substr($this->data, 0, $pos);
106  $this->data = (string) substr($this->data, $pos);
107  return $res;
108  }
109 
123  public function get($limit)
124  {
125  if (is_int($limit)) {
126  return $this->getLength($limit);
127  }
128 
129  if (is_string($limit)) {
130  return $this->getDelimiter($limit);
131  }
132 
133  throw new \InvalidArgumentException();
134  }
135 
145  public function unget($data)
146  {
147  if (!is_string($data)) {
148  throw new \InvalidArgumentException();
149  }
150 
151  $this->data = $data . $this->data;
152  return $this;
153  }
154 
164  public function push($data)
165  {
166  if (!is_string($data)) {
167  throw new \InvalidArgumentException();
168  }
169 
170  $this->data .= $data;
171  return $this;
172  }
173 }
__construct($data= '')
Definition: Buffer.php:29
getDelimiter($limit)
Definition: Buffer.php:93
$data
The buffer&#39;s current data.
Definition: Buffer.php:20
Classes implementing Countable can be used with the count() function.
Definition: Countable.php:11