Skip to content

Commit

Permalink
less arrogant namespacing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Smith-Hayes committed Oct 13, 2015
1 parent 956b8d0 commit 6f53429
Show file tree
Hide file tree
Showing 23 changed files with 84 additions and 68 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A simple bencoding library for PHP.",
"homepage": "https://github.com/dsmithhayes/bencode",
"type": "Library",
"version": "v0.1.2",
"version": "v0.1.3",
"license": "BSD 3.0",
"keywords": ["bencode", "torrent"],
"authors": [
Expand All @@ -17,7 +17,7 @@
},
"autoload": {
"psr-4": {
"DSH\\Bencode\\": "src/"
"Bencode\\": "src/"
}
}
}
30 changes: 23 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ prefixed with an `i`, and suffixed with an `e`.

<?php

use DSH\Bencode\Integer;
use Bencode\Integer;

$int = new Integer(45);
echo $int->encode(); // i45e
Expand All @@ -51,7 +51,7 @@ binary value of the integers.

<?php

use DSH\Bencode\Byte;
use Bencode\Byte;

$byte = new Byte('hello');
echo $byte->encode(); // 5:hello
Expand All @@ -73,7 +73,7 @@ The class is named `BList` because `List` is a reserved keyword in PHP.

<?php

use DSH\Bencode\Collection\BList
use Bencode\Collection\BList

$list = new BList([45, 'hello', 'world', -45]);
echo $list->encode(); // li45e5:hello5:worldi-45e
Expand All @@ -83,25 +83,41 @@ The class is named `BList` because `List` is a reserved keyword in PHP.

### Dictionary

Dictionaries are key-value lists of bytes. The first element in the list
is the key, and the second is the value. The Dictionary is prefixed with
a `d` and suffixed with an `e`.
Dictionaries are key-value lists. The first element in the list is the key, and
the second is the value. The Dictionary is prefixed with a `d` and suffixed with
an `e`.

d3:key5:valuee

You can also use elements as values in the list.

d3:keyli3ei5eee
d3:keyd3:key5:valueee

#### Usage

<?php

use DSH\Bencode\Collection\Dictionary;
use Bencode\Collection\Dictionary;

$dictionary = new Dictionary(['key' => 'value']);
echo $dictionary->encode(); // d3:key5:valuee

$dictionary->decode('d3:new6:valuese');
print_r($dictionary->write()); // ['new' => 'values']

Dictionaries are what your `.torrent` files are encoded with. You can easily
build objects to manipulate data of a torrent file.

<?php

use Bencode\Collection\Dictionary;

$dictionary = new Dictionary();
$torrent = file_get_contents('example.torrent');

$ditionary->decode($torrent);

$buffer = $dictionary->write();

echo $buffer['comment'];
18 changes: 9 additions & 9 deletions src/Bencode.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace DSH\Bencode;
namespace Bencode;

use DSH\Bencode\Integer;
use DSH\Bencode\Byte;
use DSH\Bencode\Collection\BList;
use DSH\Bencode\Collection\Dictionary;
use DSH\Bencode\Core\Traits\FactoryBuild;
use Bencode\Integer;
use Bencode\Byte;
use Bencode\Collection\BList;
use Bencode\Collection\Dictionary;
use Bencode\Core\Traits\FactoryBuild;

/**
* Each Factory in this object can create an instance of the Element you
Expand All @@ -23,7 +23,7 @@ class Bencode
*
* @param mixed $value Integer or closure
* @param boolean $decode If true, uses `decode` method from Element
* @return \DSH\Bencode\Integer
* @return \Bencode\Integer
*/
public static function integerFactory($value = null, $decode = false)
{
Expand All @@ -41,7 +41,7 @@ public static function integerFactory($value = null, $decode = false)
*
* @param mixed|null $value String or closure
* @param boolean $decode If true, uses `decode` method from Element
* @return \DSH\Bencode\Byte
* @return \Bencode\Byte
*/
public static function byteFactory($value = null, $decode = false)
{
Expand Down Expand Up @@ -77,7 +77,7 @@ public static function blistFactory($value = null, $decode = false)
*
* @param mixed|null $value A value, array, or closure
* @param boolean $decode If true, uses `decode` method from Element
* @return \DSH\Bencode\Collection\Dictionary
* @return \Bencode\Collection\Dictionary
*/
public static function dictionaryFactory($value = null, $decode = false)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Byte.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace DSH\Bencode;
namespace Bencode;

use DSH\Bencode\Core\Element;
use DSH\Bencode\Core\Buffer;
use DSH\Bencode\Exception\ByteException;
use Bencode\Core\Element;
use Bencode\Core\Buffer;
use Bencode\Exception\ByteException;

class Byte implements Element, Buffer
{
Expand Down
18 changes: 9 additions & 9 deletions src/Collection/BList.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace DSH\Bencode\Collection;

use DSH\Bencode\Core\Element;
use DSH\Bencode\Core\Buffer;
use DSH\Bencode\Core\Json;
use DSH\Bencode\Core\Traits\Pattern;
use DSH\Bencode\Integer;
use DSH\Bencode\Byte;
use DSH\Bencode\Exception\BListException;
namespace Bencode\Collection;

use Bencode\Core\Element;
use Bencode\Core\Buffer;
use Bencode\Core\Json;
use Bencode\Core\Traits\Pattern;
use Bencode\Integer;
use Bencode\Byte;
use Bencode\Exception\BListException;

/**
* The element list is a stream of encoded elements in a sequence.
Expand Down
16 changes: 8 additions & 8 deletions src/Collection/Dictionary.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace DSH\Bencode\Collection;
namespace Bencode\Collection;

use DSH\Bencode\Core\Element;
use DSH\Bencode\Core\Buffer;
use DSH\Bencode\Core\Json;
use DSH\Bencode\Core\Traits\Pattern;
use DSH\Bencode\Byte;
use DSH\Bencode\Integer;
use DSH\Bencode\Exception\DictionaryException;
use Bencode\Core\Element;
use Bencode\Core\Buffer;
use Bencode\Core\Json;
use Bencode\Core\Traits\Pattern;
use Bencode\Byte;
use Bencode\Integer;
use Bencode\Exception\DictionaryException;

/**
* The Dictionary class comes with some interesting rules. It is basically a
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Buffer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Core;
namespace Bencode\Core;

interface Buffer
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Element.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Core;
namespace Bencode\Core;

interface Element
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Json.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Core;
namespace Bencode\Core;

/**
* Used with the Collection type Elements, this will return a JSON enocded
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Traits/FactoryBuild.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Core\Traits;
namespace Bencode\Core\Traits;

/**
* Primarily used in the Bencode factory object, this Trait defines a method
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Traits/Pattern.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Core\Traits;
namespace Bencode\Core\Traits;

/**
* Consider that every Element has a REGEX pattern that can be matched
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/BListException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DSH\Bencode\Exception;
namespace Bencode\Exception;

/**
* The BListException class. Its an alias really. Maybe I'll give it and error
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/BencodeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace DSH\Bencode\Exception;
namespace Bencode\Exception;

class BencodeException extends \Exception { }
2 changes: 1 addition & 1 deletion src/Exception/ByteException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace DSH\Bencode\Exception;
namespace Bencode\Exception;

class ByteException extends \Exception { }
2 changes: 1 addition & 1 deletion src/Exception/DictionaryException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace DSH\Bencode\Exception;
namespace Bencode\Exception;

class DictionaryException extends \Exception { }
2 changes: 1 addition & 1 deletion src/Exception/IntegerException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

namespace DSH\Bencode\Exception;
namespace Bencode\Exception;

class IntegerException extends \Exception { }
12 changes: 6 additions & 6 deletions src/Integer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace DSH\Bencode;
namespace Bencode;

use DSH\Bencode\Core\Element;
use DSH\Bencode\Core\Buffer;
use DSH\Bencode\Exception\IntegerException;
use Bencode\Core\Element;
use Bencode\Core\Buffer;
use Bencode\Exception\IntegerException;

/**
* An integer is encoded with an 'i' prefix and an 'e' suffix. A good example
Expand All @@ -25,7 +25,7 @@ class Integer implements Element, Buffer
/**
* @param int $in An integer value to be represented.
*
* @throws \DSH\Bencode\Exception\IntegerException
* @throws Bencode\Exception\IntegerException
*/
public function __construct($in = null)
{
Expand All @@ -50,7 +50,7 @@ public function encode()
*
* @param string $stream Reads the stream into the buffer.
*
* @throws \DSH\Bencode\Exceptions\IntegerException
* @throws Bencode\Exceptions\IntegerException
*/
public function decode($stream)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/BListTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DSH\Bencode\Collection\BList;
use Bencode\Collection\BList;

class BListTestCase extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testArrayLength(BList $list)
}

/**
* @expectedException \DSH\Bencode\Exception\BListException
* @expectedException \Bencode\Exception\BListException
*/
public function testBListException()
{
Expand Down
10 changes: 5 additions & 5 deletions tests/BencodeIOTestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use DSH\Bencode\Bencode;
use DSH\Bencode\Integer;
use DSH\Bencode\Byte;
use DSH\Bencode\Collection\BList;
use DSH\Bencode\Collection\Dictionary;
use Bencode\Bencode;
use Bencode\Integer;
use Bencode\Byte;
use Bencode\Collection\BList;
use Bencode\Collection\Dictionary;

class BencodeIOTestCase extends PHPUnit_Framework_TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/BencodeTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DSH\Bencode\Bencode;
use Bencode\Bencode;

class BencodeTestCase extends PHPUnit_Framework_TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ByteTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DSH\Bencode\Byte;
use Bencode\Byte;

class ByteTestCase extends PHPUnit_Framework_TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/DictionaryTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DSH\Bencode\Collection\Dictionary;
use Bencode\Collection\Dictionary;

class DictionaryTestCase extends PHPUnit_Framework_TestCase
{
Expand Down
6 changes: 3 additions & 3 deletions tests/IntegerTestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DSH\Bencode\Integer;
use Bencode\Integer;

class IntegerTestCase extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -57,7 +57,7 @@ public function testFloatConstruction()
}

/**
* @expectedException \DSH\Bencode\Exception\IntegerException
* @expectedException \Bencode\Exception\IntegerException
*/
public function testInvalidConstruction()
{
Expand All @@ -83,7 +83,7 @@ public function testNegativeEncoding(Integer $int)

/**
* @depends testBasicConstruction
* @expectedException \DSH\Bencode\Exception\IntegerException
* @expectedException \Bencode\Exception\IntegerException
*/
public function testInvalidEncoding(Integer $int)
{
Expand Down

0 comments on commit 6f53429

Please sign in to comment.