Permalink
Browse files

Added FilterCollection, made StreamTransmitter use it, and made some …

…doc fixes.
  • Loading branch information...
boenrobot committed Oct 8, 2011
1 parent 802317e commit 7b2f520caa12b38138508bcfaf022c6587fc8b4c
@@ -0,0 +1,203 @@
+<?php
+
+/**
+ * ~~summary~~
+ *
+ * ~~description~~
+ *
+ * PHP version 5
+ *
+ * @category Net
+ * @package PEAR2_Net_Transmitter
+ * @author Vasil Rangelov <boen.robot@gmail.com>
+ * @copyright 2011 Vasil Rangelov
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @version SVN: $WCREV$
+ * @link http://pear2.php.net/PEAR2_Net_Transmitter
+ */
+/**
+ * The namespace declaration.
+ */
+namespace PEAR2\Net\Transmitter;
+
+/**
+ * A filter collection.
+ *
+ * Represents a collection of stream filters.
+ *
+ * @category Net
+ * @package PEAR2_Net_Transmitter
+ * @author Vasil Rangelov <boen.robot@gmail.com>
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @link http://pear2.php.net/PEAR2_Net_Transmitter
+ * @see Client
+ */
+class FilterCollection implements \SeekableIterator, \Countable
+{
+ /**
+ * @var array The filter collection itself.
+ */
+ protected $filters = array();
+
+ /**
+ * @var int A pointer, as required by SeekableIterator.
+ */
+ protected $position = 0;
+
+ /**
+ * Appends a filter to the collection
+ *
+ * @param string $name The name of the filter.
+ * @param array $params An array of parameters for the filter.
+ *
+ * @return FilterCollection The collection itself.
+ */
+ public function append($name, array $params = array())
+ {
+ $this->filters[] = array((string) $name, $params);
+ return $this;
+ }
+
+ /**
+ * Inserts the filter before another filter at a specified position.
+ *
+ * @param int $position The position before which the filter will be
+ * inserted.
+ * @param string $name The name of the filter.
+ * @param array $params An array of parameters for the filter.
+ *
+ * @return FilterCollection The collection itself.
+ */
+ public function insertBefore($position, $name, array $params = array())
+ {
+ $position = (int) $position;
+ if ($position <= 0) {
+ $this->filters = array_merge(
+ array(0 => array((string) $name, $params)),
+ $this->filters
+ );
+ return $this;
+ }
+ if ($position > count($this->filters)) {
+ return $this->append($name, $params);
+ }
+ $this->filters = array_merge(
+ array_slice($this->filters, 0, $position),
+ array(0 => array((string) $name, $params)),
+ array_slice($this->filters, $position)
+ );
+ return $this;
+ }
+
+ /**
+ * Removes a filter at a specified position.
+ *
+ * @param int $position The position from which to remove a filter.
+ *
+ * @return FilterCollection The collection itself.
+ */
+ public function removeAt($position)
+ {
+ unset($this->filters[$position]);
+ $this->filters = array_values($this->filters);
+ return $this;
+ }
+
+ /**
+ * Clears the collection
+ *
+ * @return FilterCollection The collection itself.
+ */
+ public function clear()
+ {
+ $this->filters = array();
+ return $this;
+ }
+
+ /**
+ * Gets the number of filters in the collection.
+ *
+ * @return int The number of filters in the collection.
+ */
+ public function count()
+ {
+ return count($this->filters);
+ }
+
+ /**
+ * Resets the pointer to 0.
+ *
+ * @return bool TRUE if the collection is not empty, FALSE otherwise.
+ */
+ public function rewind()
+ {
+ return $this->seek(0);
+ }
+
+ /**
+ * Moves the pointer to a specified position.
+ *
+ * @param int $position The position to move to.
+ *
+ * @return bool TRUE if the specified position is valid, FALSE otherwise.
+ */
+ public function seek($position)
+ {
+ $this->position = $position;
+ return $this->current();
+ }
+
+ /**
+ * Moves the pointer forward by 1.
+ *
+ * @return bool TRUE if the new position is valid, FALSE otherwise.
+ */
+ public function next()
+ {
+ ++$this->position;
+ return $this->current();
+ }
+
+ /**
+ * Gets the key at the current pointer position.
+ *
+ * @return string The name of the filter at the current position.
+ */
+ public function key()
+ {
+ return $this->valid() ? $this->filters[$this->position][0] : false;
+ }
+
+ /**
+ * Gets the response at the current pointer position.
+ *
+ * @return array An array of parameters for the filter at the current
+ * position.
+ */
+ public function current()
+ {
+ return $this->valid() ? $this->filters[$this->position][1] : false;
+ }
+
+ /**
+ * Moves the pointer to the last valid position.
+ *
+ * @return bool TRUE if the collection is not empty, FALSE otherwise.
+ */
+ public function end()
+ {
+ $this->position = count($this->filters) - 1;
+ return $this->valid();
+ }
+
+ /**
+ * Checks if the pointer is still pointing to an existing offset.
+ *
+ * @return bool TRUE if the pointer is valid, FALSE otherwise.
+ */
+ public function valid()
+ {
+ return array_key_exists($this->position, $this->filters);
+ }
+
+}
@@ -31,7 +31,6 @@
* @author Vasil Rangelov <boen.robot@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear2.php.net/PEAR2_Net_Transmitter
- * @see Client
*/
class SocketClientTransmitter extends StreamTransmitter
{
@@ -33,7 +33,6 @@
* @author Vasil Rangelov <boen.robot@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear2.php.net/PEAR2_Net_Transmitter
- * @see Client
*/
class SocketServerConnectionTransmitter extends StreamTransmitter
{
@@ -32,7 +32,6 @@
* @author Vasil Rangelov <boen.robot@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear2.php.net/PEAR2_Net_Transmitter
- * @see Client
*/
class StreamTransmitter
{
@@ -224,24 +223,25 @@ public function receive($length, $what = 'data')
*
* Reads from the wrapped stream to receive content as a stream.
*
- * @param int $length The number of bytes to read.
- * @param array $filters An array of filters to apply to the stream while
- * receiving. Key is the filter name, value is an array of parameters for
- * the filter.
- * @param string $what Descriptive string about what is being received
- * (used in exception messages).
+ * @param int $length The number of bytes to read.
+ * @param FilterCollection $filters A collection of filters to apply to the
+ * stream while receiving.
+ * @param string $what Descriptive string about what is being
+ * received (used in exception messages).
*
* @return resource The received content.
*/
public function receiveStream(
- $length, array $filters = array(), $what = 'stream data'
+ $length, FilterCollection $filters = null, $what = 'stream data'
) {
$result = fopen('php://temp', 'r+b');
$appliedFilters = array();
- foreach ($filters as $filtername => $params) {
- $appliedFilters[] = stream_filter_append(
- $result, $filtername, STREAM_FILTER_WRITE, $params
- );
+ if (null !== $filters) {
+ foreach ($filters as $filtername => $params) {
+ $appliedFilters[] = stream_filter_append(
+ $result, $filtername, STREAM_FILTER_WRITE, $params
+ );
+ }
}
while ($length > 0) {

0 comments on commit 7b2f520

Please sign in to comment.