Erebot  latest
A modular IRC bot for PHP 7.0+
TextWrapper.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Erebot is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Erebot;
22 
28 class TextWrapper implements \Erebot\Interfaces\TextWrapper
29 {
31  protected $text;
32 
34  protected $position;
35 
42  public function __construct($text)
43  {
44  $this->text = $text;
45  $this->position = 0;
46  }
47 
48  public function getTokens($start, $length = 0, $separator = ' ')
49  {
50  $string = preg_replace('/\\s+/', ' ', trim($this->text, $separator));
51  $parts = explode($separator, $string);
52 
53  if (!$length) {
54  $parts = array_slice($parts, $start);
55  } else {
56  $parts = array_slice($parts, $start, $length);
57  }
58 
59  if (!count($parts)) {
60  return "";
61  }
62 
63  return implode($separator, $parts);
64  }
65 
66  public function countTokens($separator = ' ')
67  {
68  $string = preg_replace('/\\s+/', ' ', trim($this->text, $separator));
69  return count(explode($separator, $string));
70  }
71 
72  public function __toString()
73  {
74  return $this->text;
75  }
76 
78  public function count()
79  {
80  return $this->countTokens();
81  }
82 
84  public function current()
85  {
86  return $this->getTokens($this->position, 1);
87  }
88 
90  public function key()
91  {
92  return $this->position;
93  }
94 
96  public function next()
97  {
98  $this->position++;
99  }
100 
102  public function rewind()
103  {
104  $this->position = 0;
105  }
106 
108  public function valid()
109  {
110  return ($this->position < $this->countTokens());
111  }
112 
114  public function offsetExists($offset)
115  {
116  return (
117  is_int($offset) &&
118  $offset >= 0 &&
119  $offset < $this->countTokens()
120  );
121  }
122 
124  public function offsetGet($offset)
125  {
126  if (!is_int($offset)) {
127  return null;
128  }
129  return $this->getTokens($offset, 1);
130  }
131 
133  public function offsetSet($offset, $value)
134  {
135  throw new \RuntimeException('The wrapped text is read-only');
136  }
137 
139  public function offsetUnset($offset)
140  {
141  throw new \RuntimeException('The wrapped text is read-only');
142  }
143 }
offsetSet($offset, $value)
A wrapper to easily split a string using a separator and deal with other operations related to separa...
Definition: TextWrapper.php:28
$position
Position in the text.
Definition: TextWrapper.php:34
$text
The text wrapped by this instance.
Definition: TextWrapper.php:31