Erebot  latest
A modular IRC bot for PHP 7.0+
IrcTextWrapper.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 
32 class IrcTextWrapper implements \Erebot\Interfaces\IrcTextWrapper
33 {
35  protected $parts;
36 
38  protected $position;
39 
46  public function __construct($parts)
47  {
48  if (is_array($parts)) {
49  $spaces = 0;
50  foreach ($parts as $part) {
51  if (strpos($part, ' ') !== false) {
52  $spaces++;
53  }
54  }
55  if ($spaces > 1) {
56  throw new \Erebot\InvalidValueException(
57  'Multiple tokens containing spaces'
58  );
59  }
60  if (!count($parts)) {
61  throw new \Erebot\InvalidValueException(
62  'At least one token must be passed'
63  );
64  }
65  } elseif (is_string($parts)) {
66  // Prepend a single space to ease single token handling.
67  $msg = ' '.$parts;
68  $pos = strpos($msg, ' :');
69 
70  // Split the message correctly.
71  // If there is no colon, then the message
72  // has already been split correctly above.
73  if ($pos !== false) {
74  // Single token, just remove the leading ' :'.
75  if (!$pos) {
76  $parts = array((string) substr($msg, 2));
77  } else {
78  // Build up the parts from all words before ' :',
79  // removing the leading space and then add the last
80  // token formed by everything after the first leading ':'.
81  $parts = explode(' ', substr($msg, 1, $pos - 1));
82  $parts[] = (string) substr($msg, $pos + 2);
83  }
84  } else {
85  // No significative colon.
86  $parts = explode(' ', (string) substr($msg, 1));
87  }
88  } else {
89  throw new \Erebot\InvalidValueException(
90  'A string or an array was expected'
91  );
92  }
93 
94  $this->parts = $parts;
95  $this->position = 0;
96  }
97 
98  public function __toString()
99  {
100  $last = count($this->parts) - 1;
101  $text = '';
102  foreach ($this->parts as $index => $part) {
103  if ($index == $last) {
104  if (strpos($part, ' ') !== false || !strncmp($part, ':', 1)) {
105  $text .= ':';
106  }
107  } elseif (!strncmp($part, ':', 1)) {
108  throw new \Exception('Oops!'); // Will trigger a fatal error.
109  }
110  $text .= $part;
111  if ($index != $last) {
112  $text .= ' ';
113  }
114  }
115  return $text;
116  }
117 
119  public function count()
120  {
121  return count($this->parts);
122  }
123 
125  public function current()
126  {
127  return $this->parts[$this->position];
128  }
129 
131  public function key()
132  {
133  return $this->position;
134  }
135 
137  public function next()
138  {
139  $this->position++;
140  }
141 
143  public function rewind()
144  {
145  $this->position = 0;
146  }
147 
149  public function valid()
150  {
151  return ($this->position < count($this->parts));
152  }
153 
155  public function offsetExists($offset)
156  {
157  return isset($this->parts[$offset]);
158  }
159 
161  public function offsetGet($offset)
162  {
163  if (!is_int($offset)) {
164  return null;
165  }
166  if ($offset < 0) {
167  $offset += count($this->parts);
168  }
169  return $this->parts[$offset];
170  }
171 
173  public function offsetSet($offset, $value)
174  {
175  throw new \RuntimeException('The wrapped text is read-only');
176  }
177 
179  public function offsetUnset($offset)
180  {
181  unset($this->parts[$offset]);
182  $this->parts = array_values($this->parts);
183  }
184 }
$parts
The parts wrapped by this instance.
offsetSet($offset, $value)
$position
Position in the text.
A wrapper that correctly splits messages received from an IRC server (data part). ...