Skip to content
This repository was archived by the owner on Apr 7, 2019. It is now read-only.

Commit b4f2960

Browse files
Initial commit
0 parents  commit b4f2960

11 files changed

Lines changed: 446 additions & 0 deletions

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/.travis.yml export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/CONTRIBUTING.md export-ignore
9+
/README.md export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor

.travis.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
language: php
2+
dist: trusty
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- php: 7.1
8+
env:
9+
- SYMFONY_VERSION=^3.0
10+
- PHPUNIT_VERSION=^6.5
11+
- php: 7.1
12+
env:
13+
- SYMFONY_VERSION=^4.0
14+
- PHPUNIT_VERSION=^7.0
15+
- php: 7.2
16+
env:
17+
- SYMFONY_VERSION=^3.0
18+
- PHPUNIT_VERSION=^6.5
19+
- php: 7.2
20+
env:
21+
- SYMFONY_VERSION=^4.0
22+
- PHPUNIT_VERSION=^7.0
23+
24+
install:
25+
- composer require "symfony/lts:${SYMFONY_VERSION}" --no-update -n
26+
- composer require "phpunit/phpunit:${PHPUNIT_VERSION}" --dev --no-update -n
27+
- travis_retry composer install --no-suggest --prefer-dist -n -o
28+
29+
script:
30+
- if [ "$TRAVIS_PHP_VERSION" != "7.1" ]; then vendor/bin/phpunit; fi
31+
- if [ "$TRAVIS_PHP_VERSION" == "7.1" ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; fi
32+
33+
after_script:
34+
- if [ "$TRAVIS_PHP_VERSION" == "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
35+
- if [ "$TRAVIS_PHP_VERSION" == "7.1" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi

CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CONTRIBUTING
2+
============
3+
4+
5+
Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.
6+
7+
8+
## Guidelines
9+
10+
* Please follow the [PSR-2 Coding Style Guide](http://www.php-fig.org/psr/psr-2/), enforced by [StyleCI](https://styleci.io/).
11+
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant.
12+
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
13+
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
14+
* If you are changing the behavior, or the public api, you may need to update the docs.
15+
* Please remember that we follow [SemVer](http://semver.org/).
16+
17+
18+
## Running Tests
19+
20+
You will need an install of [Composer](https://getcomposer.org/) before continuing.
21+
22+
First, install the dependencies:
23+
24+
```bash
25+
$ composer install
26+
```
27+
28+
Then run PHPUnit:
29+
30+
```bash
31+
$ vendor/bin/phpunit
32+
```
33+
34+
If the test suite passes on your local machine you should be good to go.
35+
36+
When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/).
37+
38+
We also have [StyleCI](https://styleci.io/) setup to automatically fix any code style issues.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Alt Three Services Limited
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Alt Three UUID
2+
3+
A UUID to 64-bit signed integer converter.
4+
5+
This is a lossy conversion, performed by xoring the both halves.
6+
7+
8+
## Installation
9+
10+
This version requires 64-bit [PHP](https://php.net) 7.1 or 7.2.
11+
12+
To get the latest version, simply require the project using [Composer](https://getcomposer.org):
13+
14+
```bash
15+
$ composer require alt-three/uuid
16+
```
17+
18+
19+
## Security
20+
21+
If you discover a security vulnerability within this package, please e-mail us at support@alt-three.com. All security vulnerabilities will be promptly addressed.
22+
23+
24+
## License
25+
26+
Alt Three Storage is licensed under [The MIT License (MIT)](LICENSE).

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "alt-three/uuid",
3+
"description": "A UUID To Integer Converter",
4+
"keywords": ["UUID", "Alt Three"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Alt Three",
9+
"email": "support@alt-three.com"
10+
}
11+
],
12+
"require": {
13+
"php-64bit": "^7.1.3"
14+
},
15+
"require-dev": {
16+
"graham-campbell/analyzer": "^2.0",
17+
"phpunit/phpunit": "^6.5|^7.0"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"AltThree\\Uuid\\": "src/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"AltThree\\Tests\\Uuid\\": "tests/"
27+
}
28+
},
29+
"config": {
30+
"preferred-install": "dist"
31+
},
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.0-dev"
35+
}
36+
},
37+
"minimum-stability": "dev",
38+
"prefer-stable": true
39+
}

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
processIsolation="false"
14+
stopOnError="false"
15+
stopOnFailure="false"
16+
verbose="true"
17+
>
18+
<testsuites>
19+
<testsuite name="Alt Three UUID Test Suite">
20+
<directory suffix="Test.php">./tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
<filter>
24+
<whitelist processUncoveredFilesFromWhitelist="true">
25+
<directory suffix=".php">./src</directory>
26+
</whitelist>
27+
</filter>
28+
</phpunit>

src/UuidConverter.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Alt Three UUID.
7+
*
8+
* (c) Alt Three Services Limited
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace AltThree\Uuid;
15+
16+
/**
17+
* This is the uuid converter class.
18+
*
19+
* @author Graham Campbell <graham@alt-three.com>
20+
*/
21+
final class UuidConverter
22+
{
23+
/**
24+
* Convert a uuid to a 64-bit signed integer.
25+
*
26+
* This is a lossy conversion, performed by xoring the both halves.
27+
*
28+
* @param string $uuid
29+
*
30+
* @return int
31+
*/
32+
public static function convert(string $uuid)
33+
{
34+
// convert to 16 byte binary represention
35+
$bin = hex2bin(str_replace(['{', '-', '}'], '', $uuid));
36+
37+
// xor first half with second to give 8 bytes
38+
$xor = self::xor(...str_split($bin, 8));
39+
40+
// convert to a 64-bit signed integer
41+
return unpack('q', $xor)[1];
42+
}
43+
44+
/**
45+
* Bitwise xor both strings.
46+
*
47+
* @param string $a
48+
* @param string $b
49+
*
50+
* @return string
51+
*/
52+
private static function xor(string $a, string $b)
53+
{
54+
return $a ^ $b;
55+
}
56+
}

tests/AnalysisTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Alt Three UUID.
7+
*
8+
* (c) Alt Three Services Limited
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace AltThree\Tests\Uuid;
15+
16+
use GrahamCampbell\Analyzer\AnalysisTrait;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* This is the analysis test class.
21+
*
22+
* @author Graham Campbell <graham@alt-three.com>
23+
*/
24+
class AnalysisTest extends TestCase
25+
{
26+
use AnalysisTrait;
27+
28+
/**
29+
* Get the code paths to analyze.
30+
*
31+
* @return string[]
32+
*/
33+
protected function getPaths()
34+
{
35+
return [
36+
realpath(__DIR__.'/../src'),
37+
realpath(__DIR__),
38+
];
39+
}
40+
}

0 commit comments

Comments
 (0)