mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
Extract classes for CLI rendering
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util\Formatter;
|
||||
|
||||
use Rhumsaa\Uuid\Console\Util\UuidFormatter;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\UuidContentFormatterInterface;
|
||||
|
||||
class V1Formatter implements UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid)
|
||||
{
|
||||
return array(
|
||||
array('', 'content:', 'time: ' . $uuid->getDateTime()->format('c')),
|
||||
array('', '', 'clock: ' . $uuid->getClockSequence() . ' (usually random)'),
|
||||
array('', '', 'node: ' . substr(chunk_split($uuid->getNodeHex(), 2, ':'), 0, -1)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util\Formatter;
|
||||
|
||||
use Rhumsaa\Uuid\Console\Util\UuidFormatter;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\UuidContentFormatterInterface;
|
||||
|
||||
class V2Formatter implements UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid)
|
||||
{
|
||||
return array();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util\Formatter;
|
||||
|
||||
use Rhumsaa\Uuid\Console\Util\UuidFormatter;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\UuidContentFormatterInterface;
|
||||
|
||||
class V3Formatter implements UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid)
|
||||
{
|
||||
return array(
|
||||
array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)),
|
||||
array('', '', '(not decipherable: MD5 message digest only)'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util\Formatter;
|
||||
|
||||
use Rhumsaa\Uuid\Console\Util\UuidFormatter;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\UuidContentFormatterInterface;
|
||||
|
||||
class V4Formatter implements UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid)
|
||||
{
|
||||
return array(
|
||||
array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)),
|
||||
array('', '', '(no semantics: random data only)'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util\Formatter;
|
||||
|
||||
use Rhumsaa\Uuid\Console\Util\UuidFormatter;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\UuidContentFormatterInterface;
|
||||
|
||||
class V5Formatter implements UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid)
|
||||
{
|
||||
return array(
|
||||
array('', 'content:', substr(chunk_split($uuid->getHex(), 2, ':'), 0, -1)),
|
||||
array('', '', '(not decipherable: SHA1 message digest only)'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util;
|
||||
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
|
||||
interface UuidContentFormatterInterface
|
||||
{
|
||||
public function getContent(UuidInterface $uuid);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Rhumsaa\Uuid\Console\Util;
|
||||
|
||||
use Rhumsaa\Uuid\Uuid;
|
||||
use Rhumsaa\Uuid\UuidInterface;
|
||||
use Rhumsaa\Uuid\Console\Util\Formatter\V1Formatter;
|
||||
use Rhumsaa\Uuid\Console\Util\Formatter\V2Formatter;
|
||||
use Rhumsaa\Uuid\Console\Util\Formatter\V3Formatter;
|
||||
use Rhumsaa\Uuid\Console\Util\Formatter\V4Formatter;
|
||||
use Rhumsaa\Uuid\Console\Util\Formatter\V5Formatter;
|
||||
use Symfony\Component\Console\Helper\TableHelper;
|
||||
|
||||
class UuidFormatter
|
||||
{
|
||||
|
||||
private static $versionMap = [
|
||||
1 => '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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user