예제 #1
0
 /**
  * Decoding a Dictionary is a lot like decoding a List, however we are
  * looking for the firs two Elements and setting them as the key and value
  * of the array.
  * 
  * A key must be an encoded byte, however the value can be any of the other
  * elements, such as an Integer, List or another Dictionary.
  * 
  * @param string $stream Encoded Dictionary
  * 
  * @return string the remainder of the stream, if anything
  */
 public function decode($stream)
 {
     $stream = $this->dropEncoding($stream, self::PATTERN);
     if (is_numeric($stream[0])) {
         $key = new Byte();
     } elseif ($stream[0] === 'i') {
         $key = new Integer();
     } else {
         return $stream;
     }
     // always read the key first, then onto the value
     $stream = $key->decode($stream);
     if (is_numeric($stream[0])) {
         $value = new Byte();
     } elseif ($stream[0] === 'i') {
         $value = new Integer();
     } elseif ($stream[0] === 'l') {
         $value = new BList();
     } elseif ($stream[0] === 'd') {
         $value = new Dictionary();
     } else {
         throw new DictionaryException('Improper dictionary value encoding: ' . $stream);
     }
     $stream = $value->decode($stream);
     // assign the key and value.
     $this->_buffer[$key->write()] = $value->write();
     if (strlen($stream) > 0) {
         return $this->decode($stream);
     }
     return $stream;
 }
예제 #2
0
 public function testStreamReturn()
 {
     $int = new Integer();
     $this->assertEquals('5:hello', $int->decode('i35e5:hello'));
 }