Skip to content

Commit

Permalink
Merge bd86ab7 into 94ea593
Browse files Browse the repository at this point in the history
  • Loading branch information
aztech-dev committed Jun 23, 2015
2 parents 94ea593 + bd86ab7 commit 9d55855
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 3 deletions.
245 changes: 245 additions & 0 deletions src/LazyUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php

namespace Ramsey\Uuid;

use Ramsey\Uuid\Converter\NumberConverterInterface;

class LazyUuid extends Uuid
{

private $isParsed = false;

private $uuidString;

public function __construct($uuidString, NumberConverterInterface $numberConverter, CodecInterface $codec)
{
$this->uuidString = $uuidString;

parent::__construct(array(), $numberConverter, $codec);
}

private function parse()
{
if ($this->isParsed) {
return;
}

$this->fields = $this->codec->decode($this->uuidString)->getFieldsHex();
$this->isParsed = true;
}

public function compareTo(UuidInterface $uuid)
{
$this->parse();

return parent::compareTo($uuid);
}

public function getBytes()
{
$this->parse();

return parent::getBytes();
}

public function getClockSeqHiAndReserved()
{
$this->parse();

return parent::getClockSeqHiAndReserved();
}

public function getClockSeqHiAndReservedHex()
{
$this->parse();

return parent::getClockSeqHiAndReservedHex();
}

public function getClockSeqLow()
{
$this->parse();

return parent::getClockSeqLow();
}

public function getClockSeqLowHex()
{
$this->parse();

return parent::getClockSeqLowHex();
}

public function getClockSequence()
{
$this->parse();

return parent::getClockSequence();
}

public function getClockSequenceHex()
{
$this->parse();

return parent::getClockSequenceHex();
}

public function getDateTime()
{
$this->parse();

return parent::getDateTime();
}

public function getFields()
{
$this->parse();

return parent::getFields();
}

public function getFieldsHex()
{
$this->parse();

return parent::getFieldsHex();
}

public function getHex()
{
$this->parse();

return parent::getHex();
}

public function getInteger()
{
$this->parse();

return parent::getInteger();
}

public function getLeastSignificantBits()
{
$this->parse();

return parent::getLeastSignificantBits();
}

public function getLeastSignificantBitsHex()
{
$this->parse();

return parent::getLeastSignificantBitsHex();
}

public function getMostSignificantBits()
{
$this->parse();

return parent::getMostSignificantBits();
}

public function getMostSignificantBitsHex()
{
$this->parse();

return parent::getMostSignificantBitsHex();
}

public function getNode()
{
$this->parse();

return parent::getNode();
}

public function getNodeHex()
{
$this->parse();

return parent::getNodeHex();
}

public function getTimeHiAndVersion()
{
$this->parse();

return parent::getTimeHiAndVersion();
}

public function getTimeHiAndVersionHex()
{
$this->parse();

return parent::getTimeHiAndVersionHex();
}

public function getTimeLow()
{
$this->parse();

return parent::getTimeLow();
}

public function getTimeLowHex()
{
$this->parse();

return parent::getTimeLowHex();
}

public function getTimeMid()
{
$this->parse();

return parent::getTimeMid();
}

public function getTimeMidHex()
{
$this->parse();

return parent::getTimeMidHex();
}

public function getTimestamp()
{
$this->parse();

return parent::getTimestamp();
}

public function getTimestampHex()
{
$this->parse();

return parent::getTimestampHex();
}

public function getUrn()
{
$this->parse();

return parent::getUrn();
}

public function getVariant()
{
$this->parse();

return parent::getVariant();
}

public function getVersion()
{
$this->parse();

return parent::getVersion();
}

public function toString()
{
return $this->uuidString;
}
}
23 changes: 20 additions & 3 deletions src/PeclUuidFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ramsey\Uuid;

use Ramsey\Uuid\Converter\NumberConverterInterface;

/**
* Factory relying on PECL UUID library whenever possible, otherwise defaulting
* to pure PHP factory.
Expand All @@ -10,6 +12,16 @@
*/
class PeclUuidFactory implements UuidFactoryInterface
{
/**
* @var CodecInterface
*/
private $codec;

/**
* @var NumberConverterInterface
*/
private $converter;

/**
*
* @var UuidFactoryInterface
Expand All @@ -26,10 +38,15 @@ class PeclUuidFactory implements UuidFactoryInterface
*
* @param UuidFactoryInterface $factory
*/
public function __construct(UuidFactoryInterface $factory)
public function __construct(UuidFactoryInterface $factory, FeatureSet $features = null)
{
$this->hasExt = extension_loaded('uuid');
$this->factory = $factory;

$features = $features ?: new FeatureSet();

$this->codec = $features->getCodec();
$this->converter = $features->getNumberConverter();
}

/**
Expand All @@ -50,7 +67,7 @@ public function uuid1($node = null, $clockSeq = null)
return $this->factory->uuid1($node, $clockSeq);
}

return $this->fromString(uuid_create(UUID_TYPE_TIME));
return new LazyUuid(uuid_create(UUID_TYPE_TIME), $this->converter, $this->codec);
}

/**
Expand All @@ -70,7 +87,7 @@ public function uuid4()
return $this->factory->uuid4();
}

return $this->fromString(uuid_create(UUID_TYPE_RANDOM));
return new LazyUuid(uuid_create(UUID_TYPE_RANDOM), $this->converter, $this->codec);
}

/**
Expand Down

0 comments on commit 9d55855

Please sign in to comment.