From 77a34348d52d18d6f2224ba7635808d52a7a725f Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Thu, 3 Sep 2015 23:28:07 -0400 Subject: [PATCH] Add docblocks for classes and interface in Ramsey\Uuid\Codec namespace --- src/Codec/CodecInterface.php | 18 +++++++++-- src/Codec/GuidStringCodec.php | 40 +++++++++++++++++++++++-- src/Codec/StringCodec.php | 56 +++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 7 deletions(-) diff --git a/src/Codec/CodecInterface.php b/src/Codec/CodecInterface.php index be0c6df..f0dde95 100644 --- a/src/Codec/CodecInterface.php +++ b/src/Codec/CodecInterface.php @@ -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 */ diff --git a/src/Codec/GuidStringCodec.php b/src/Codec/GuidStringCodec.php index 92e20e9..4e3ddf0 100644 --- a/src/Codec/GuidStringCodec.php +++ b/src/Codec/GuidStringCodec.php @@ -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]; diff --git a/src/Codec/StringCodec.php b/src/Codec/StringCodec.php index 3d2d0f8..70e5041 100644 --- a/src/Codec/StringCodec.php +++ b/src/Codec/StringCodec.php @@ -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(