Add docblocks for classes and interface in Ramsey\Uuid\Codec namespace

This commit is contained in:
Ben Ramsey
2015-09-03 23:28:07 -04:00
parent 67fe80dc1b
commit 77a34348d5
3 changed files with 107 additions and 7 deletions
+16 -2
View File
@@ -16,24 +16,38 @@ namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\UuidInterface;
/**
* CodecInterface represents a UUID coder-decoder
*/
interface CodecInterface
{
/**
* @return string
* Encodes a UuidInterface as a string representation of a UUID
*
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a UUID
*/
public function encode(UuidInterface $uuid);
/**
* @return string
* Encodes a UuidInterface as a binary representation of a UUID
*
* @param UuidInterface $uuid
* @return string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid);
/**
* Decodes a string representation of a UUID into a UuidInterface object instance
*
* @param string $encodedUuid
* @return UuidInterface
*/
public function decode($encodedUuid);
/**
* Decodes a binary representation of a UUID into a UuidInterface object instance
*
* @param string $bytes
* @return UuidInterface
*/
+37 -3
View File
@@ -14,12 +14,22 @@
namespace Ramsey\Uuid\Codec;
use Ramsey\Uuid\UuidInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* GuidStringCodec encodes and decodes globally unique identifiers (GUID)
*
* @link https://en.wikipedia.org/wiki/Globally_unique_identifier
*/
class GuidStringCodec extends StringCodec
{
/**
* Encodes a UuidInterface as a string representation of a GUID
*
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a GUID
*/
public function encode(UuidInterface $uuid)
{
$components = array_values($uuid->getFieldsHex());
@@ -33,6 +43,12 @@ class GuidStringCodec extends StringCodec
);
}
/**
* Encodes a UuidInterface as a binary representation of a GUID
*
* @param UuidInterface $uuid
* @return string Binary string representation of a GUID
*/
public function encodeBinary(UuidInterface $uuid)
{
$components = array_values($uuid->getFieldsHex());
@@ -40,6 +56,12 @@ class GuidStringCodec extends StringCodec
return hex2bin(implode('', $components));
}
/**
* Decodes a string representation of a GUID into a UuidInterface object instance
*
* @param string $encodedUuid
* @return UuidInterface
*/
public function decode($encodedUuid)
{
$components = $this->extractComponents($encodedUuid);
@@ -49,13 +71,25 @@ class GuidStringCodec extends StringCodec
return $this->getBuilder()->build($this, $this->getFields($components));
}
/**
* Decodes a binary representation of a GUID into a UuidInterface object instance
*
* @param string $bytes
* @return UuidInterface
*/
public function decodeBytes($bytes)
{
// Specifically call parent::decode to preserve correct byte order
return parent::decode(bin2hex($bytes));
}
protected function swapFields(array & $components)
/**
* Swaps fields to support GUID byte order
*
* @param array $components An array of UUID components (the UUID exploded on its dashes)
* @return void
*/
protected function swapFields(array &$components)
{
$hex = unpack('H*', pack('L', hexdec($components[0])));
$components[0] = $hex[1];
+54 -2
View File
@@ -15,20 +15,38 @@
namespace Ramsey\Uuid\Codec;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Builder\UuidBuilderInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* StringCodec encodes and decodes RFC 4122 UUIDs
*
* @link http://tools.ietf.org/html/rfc4122
*/
class StringCodec implements CodecInterface
{
/**
* @var UuidBuilderInterface
*/
private $builder;
/**
* Constructs a StringCodec for use encoding and decoding UUIDs
*
* @param UuidBuilderInterface $builder The UUID builder to use when encoding UUIDs
*/
public function __construct(UuidBuilderInterface $builder)
{
$this->builder = $builder;
}
/**
* Encodes a UuidInterface as a string representation of a UUID
*
* @param UuidInterface $uuid
* @return string Hexadecimal string representation of a UUID
*/
public function encode(UuidInterface $uuid)
{
$fields = array_values($uuid->getFieldsHex());
@@ -39,11 +57,23 @@ class StringCodec implements CodecInterface
);
}
/**
* Encodes a UuidInterface as a binary representation of a UUID
*
* @param UuidInterface $uuid
* @return string Binary string representation of a UUID
*/
public function encodeBinary(UuidInterface $uuid)
{
return hex2bin($uuid->getHex());
}
/**
* Decodes a string representation of a UUID into a UuidInterface object instance
*
* @param string $encodedUuid
* @return UuidInterface
*/
public function decode($encodedUuid)
{
$components = $this->extractComponents($encodedUuid);
@@ -52,6 +82,12 @@ class StringCodec implements CodecInterface
return $this->builder->build($this, $fields);
}
/**
* Decodes a binary representation of a UUID into a UuidInterface object instance
*
* @param string $bytes
* @return UuidInterface
*/
public function decodeBytes($bytes)
{
if (strlen($bytes) !== 16) {
@@ -63,11 +99,21 @@ class StringCodec implements CodecInterface
return $this->decode($hexUuid[1]);
}
/**
* Returns the UUID builder
*
* @return UuidBuilderInterface
*/
protected function getBuilder()
{
return $this->builder;
}
/**
* Returns an array of UUID components (the UUID exploded on its dashes)
*
* @return array
*/
protected function extractComponents($encodedUuid)
{
$nameParsed = str_replace(array(
@@ -98,6 +144,12 @@ class StringCodec implements CodecInterface
return $components;
}
/**
* Returns the fields that make up this UUID
*
* @see \Ramsey\Uuid\Uuid::getFields()
* @return array
*/
protected function getFields(array $components)
{
return array(