Esempio n. 1
0
 /**
  * Returns an encoded stream of a Dictionary element.
  *
  * @return string Encoded Dictionary
  */
 public function encode()
 {
     $buffer = 'd';
     foreach ($this->_buffer as $k => $v) {
         $key = new Byte();
         $key->read($k);
         if (is_array($v)) {
             $dictionary = false;
             foreach (array_keys($v) as $ak) {
                 if (!is_numeric($ak)) {
                     $dictionary = true;
                 }
             }
             if ($dictionary) {
                 $value = new Dictionary();
             } else {
                 $value = new BList();
             }
         } elseif (is_numeric($v)) {
             $value = new Integer();
         } else {
             $value = new Byte();
         }
         $value->read($v);
         $buffer .= $key->encode() . $value->encode();
     }
     $buffer .= 'e';
     return $buffer;
 }
Esempio n. 2
0
 /**
  * Takes the List object's buffer and returns an encoded stream that
  * represents the byte.
  *
  * @return string Encoded Bencode list
  */
 public function encode()
 {
     $buffer = '';
     foreach ($this->_buffer as $b) {
         if (is_numeric($b)) {
             $element = new Integer($b);
         } else {
             $element = new Byte($b);
         }
         $buffer .= $element->encode();
     }
     return 'l' . $buffer . 'e';
 }
Esempio n. 3
0
 public function testZeroIntStreamEncoding()
 {
     $byte = new Byte('0');
     $this->assertEquals('1:0', $byte->encode());
 }