diff --git a/src/Console/Command/DecodeCommand.php b/src/Console/Command/DecodeCommand.php index f1ed843..3cd0f26 100644 --- a/src/Console/Command/DecodeCommand.php +++ b/src/Console/Command/DecodeCommand.php @@ -18,6 +18,8 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Rhumsaa\Uuid\Console\Exception; use Rhumsaa\Uuid\Uuid; +use Rhumsaa\Uuid\Console\Util\UuidFormatter; +use Symfony\Component\Console\Helper\Table; /** * Provides the console command to decode UUIDs and dump information about them @@ -57,69 +59,8 @@ class DecodeCommand extends Command $table = $this->getHelperSet()->get('table'); $table->setLayout(TableHelper::LAYOUT_BORDERLESS); - $table->addRows(array( - array('encode:', 'STR:', (string) $uuid), - array('', 'INT:', (string) $uuid->getInteger()), - )); - - if ($uuid->getVariant() != Uuid::RFC_4122) { - $table->addRows(array( - array('decode:', 'variant:', 'Not an RFC 4122 UUID'), - )); - } else { - $this->dumpUuid($table, $uuid); - } + (new UuidFormatter())->write($table, $uuid); $table->render($output); } - - protected function dumpUuid($table, $uuid) - { - $content = null; - $version = 'Invalid or unknown UUID version'; - - switch ($uuid->getVersion()) { - case 1: - $version = '1 (time and node based)'; - $content = array( - array('', 'content:', 'time: ' . $uuid->getDateTime()->format('c')), - array('', '', 'clock: ' . $uuid->getClockSequence() . ' (usually random)'), - array('', '', 'node: ' . substr(chunk_split($uuid->getNodeHex(), 2, ':'), 0, -1)), - ); - break; - case 2: - $version = '2 (DCE security based)'; - break; - case 3: - $version = '3 (name based, MD5)'; - $content = array( - array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)), - array('', '', '(not decipherable: MD5 message digest only)'), - ); - break; - case 4: - $version = '4 (random data based)'; - $content = array( - array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)), - array('', '', '(no semantics: random data only)'), - ); - break; - case 5: - $version = '5 (name based, SHA-1)'; - $content = array( - array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)), - array('', '', '(not decipherable: SHA1 message digest only)'), - ); - break; - } - - $table->addRows(array( - array('decode:', 'variant:', 'RFC 4122'), - array('', 'version:', $version), - )); - - if ($content) { - $table->addRows($content); - } - } } diff --git a/src/Console/Util/Formatter/V1Formatter.php b/src/Console/Util/Formatter/V1Formatter.php new file mode 100644 index 0000000..4c27c00 --- /dev/null +++ b/src/Console/Util/Formatter/V1Formatter.php @@ -0,0 +1,19 @@ +getDateTime()->format('c')), + array('', '', 'clock: ' . $uuid->getClockSequence() . ' (usually random)'), + array('', '', 'node: ' . substr(chunk_split($uuid->getNodeHex(), 2, ':'), 0, -1)), + ); + } +} diff --git a/src/Console/Util/Formatter/V2Formatter.php b/src/Console/Util/Formatter/V2Formatter.php new file mode 100644 index 0000000..3362028 --- /dev/null +++ b/src/Console/Util/Formatter/V2Formatter.php @@ -0,0 +1,15 @@ +getHex(), 2, ':'), 0, -1)), + array('', '', '(not decipherable: MD5 message digest only)'), + ); + } +} diff --git a/src/Console/Util/Formatter/V4Formatter.php b/src/Console/Util/Formatter/V4Formatter.php new file mode 100644 index 0000000..4bce8ca --- /dev/null +++ b/src/Console/Util/Formatter/V4Formatter.php @@ -0,0 +1,18 @@ +getHex(), 2, ':'), 0, -1)), + array('', '', '(no semantics: random data only)'), + ); + } +} diff --git a/src/Console/Util/Formatter/V5Formatter.php b/src/Console/Util/Formatter/V5Formatter.php new file mode 100644 index 0000000..bf99c78 --- /dev/null +++ b/src/Console/Util/Formatter/V5Formatter.php @@ -0,0 +1,18 @@ +getHex(), 2, ':'), 0, -1)), + array('', '', '(not decipherable: SHA1 message digest only)'), + ); + } +} diff --git a/src/Console/Util/UuidContentFormatterInterface.php b/src/Console/Util/UuidContentFormatterInterface.php new file mode 100644 index 0000000..ae74ca6 --- /dev/null +++ b/src/Console/Util/UuidContentFormatterInterface.php @@ -0,0 +1,10 @@ + '1 (time and node based)', + 2 => '2 (DCE security based)', + 3 => '3 (name based, MD5)', + 4 => '4 (random data based)', + 5 => '5 (name based, SHA-1)' + ]; + + private static $variantMap = [ + Uuid::RESERVED_NCS => 'Reserved', + Uuid::RFC_4122 => 'RFC 4122', + Uuid::RESERVED_MICROSOFT => 'Reserved for Microsoft use.', + Uuid::RESERVED_FUTURE => 'Reserved for future use.' + ]; + + private static $formatters; + + public function __construct() + { + if (self::$formatters == null) { + self::$formatters = [ + 1 => new V1Formatter(), + 2 => new V2Formatter(), + 3 => new V3Formatter(), + 4 => new V4Formatter(), + 5 => new V5Formatter() + ]; + } + } + + public function write(TableHelper $table, UuidInterface $uuid) + { + $table->addRows(array( + array('encode:', 'STR:', (string) $uuid), + array('', 'INT:', (string) $uuid->getInteger()), + )); + + if ($uuid->getVariant() == Uuid::RFC_4122) { + $table->addRows(array( + array('decode:', 'variant:',$this->getFormattedVariant($uuid)), + array('', 'version:', $this->getFormattedVersion($uuid)), + )); + + $table->addRows($this->getContent($uuid)); + } + else { + $table->addRows(array( + array('decode:', 'variant:', 'Not an RFC 4122 UUID'), + )); + } + } + + public function getFormattedVersion(UuidInterface $uuid) + { + return self::$versionMap[$uuid->getVersion()]; + } + + public function getFormattedVariant(UuidInterface $uuid) + { + return self::$variantMap[$uuid->getVariant()]; + } + + /** + * Returns content as an array of rows, each row being an array containing column values. + */ + public function getContent(UuidInterface $uuid) + { + $formatter = self::$formatters[$uuid->getVersion()]; + + return $formatter->getContent($uuid); + } +}