2017 © Pedro PelĂĄez
 

library unicity

Library to generate DB-friendly GUIDs

image

unicity/unicity

Library to generate DB-friendly GUIDs

  • Monday, September 4, 2017
  • by castarco
  • Repository
  • 1 Watchers
  • 4 Stars
  • 4,145 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

Unicity Library

Build Status Scrutinizer Code Quality Code Coverage, (*1)

Introduction

The Unicity library serves to the purpose of generating globally unique IDs ensuring some properties:, (*2)

  • High (but controllable) degree of entropy/randomness:
    • Required to avoid ID guessing attacks.
  • Time-related ID (with controllable precision, and proper internal representation):
    • Time-related IDs (using a proper internal representation: Big-Endian) are a good helper to preserve the natural order of insertion in our DBs.
    • In addition, using time-related IDs allows us to use B-Tree indexes without having to recreate the index with almost every insertion due to randomness.
  • The possibility of representing the ID using multiple formats, including compact binary strings:
    • Base64
    • Base64 with an adaptation to be used in URLs
    • Hexadecimal
    • Binary string (the most compact form, ideal to ensure small DB indexes)

Setup

Install it through composer, that's it:, (*3)

composer require unicity/unicity

Code examples

The Factory

<?php

use Unicity\GUIDFactory;

// A good place to start is to instantiate a factory that will help us to create
// or to unserialize GUIDs following the specified constraints.
// 

// The first parameter tells us how many timestamp-related bytes we want to use
//   (from 4 to 7 bytes, by default 7)
// The second parameter tells us how many randomness bytes we want to use
//   (from 2 to 9 bytes, by default 9)
$guidFactory = new \Unicity\GUIDFactory(5, 7);

An important point to take into account is that, for testing and dependency injection purposes, you should type hint using the Unicity\Interfaces\GUIDFactory interface., (*4)

The same point applies to GUID instances, its better to type hint using the Unicity\Interfaces\GUID interface., (*5)

Creating new GUID instances

<?php

// This will create a completely new GUID following the constraints specified in the factory constructor
$newGUID = $guidFactory->create();

Unserializing GUIDs

<?php

// This will unserialize (and validate) a GUID from an hexadecimal string
$recoveredGUID = $guidFactory->fromHexString('1234567890ab1234567890ab');

// This will unserialize (and validate) a GUID from a base64 string
$recoveredGUID = $guidFactory->fromBase64String('EjRWeJCrEjRWeJCr');

// This will unserialize (and validate) a GUID from a base64 (modified for URLs) string
$recoveredGUID = $guidFactory->fromBase64UrlString('EjRWeJCrEjRWeJCr');

// This will unserialize (and validate) a GUID from a binary bytes stream
$recoveredGUID = $guidFactory->fromBinaryString('1234567890ab');

Serializing GUIDs

<?php

$hexString = $newGUID->asHexString();
$base64String = $newGUID->asBase64String();
$base64urlString = $newGUID->asBase64UrlString();
$bytesStream = $newGUID->asBinaryString();

Comparing GUIDs

<?php

if (!$requestGUID->equals($dbGUID)) {
    // block request
}

The Versions

04/09 2017

dev-master

9999999-dev

Library to generate DB-friendly GUIDs

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Andrés Correa Casablanca

30/08 2017

0.1.3

0.1.3.0

Library to generate DB-friendly GUIDs

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Andrés Correa Casablanca

25/02 2017

0.1.2

0.1.2.0

Library to generate DB-friendly GUIDs

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Andrés Correa Casablanca

17/02 2017

0.1.1

0.1.1.0

Library to generate DB-friendly GUIDs

  Sources   Download

MIT

The Development Requires

by Andrés Correa Casablanca

16/02 2017

0.1.0

0.1.0.0

Library to generate DB-friendly GUIDs

  Sources   Download

MIT

The Development Requires

by Andrés Correa Casablanca