Skip to content

Commit 8a0f318

Browse files
committed
v0.1 implementation
0 parents  commit 8a0f318

18 files changed

+481
-0
lines changed

.gitignore

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

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Komar Stanislau
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 all
13+
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 THE
21+
SOFTWARE.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "micro/plugin-uuid",
3+
"description": "Micro Framework - plugin for UUID support",
4+
"type": "library",
5+
"license": "MIT",
6+
"version": "0.1",
7+
"autoload": {
8+
"psr-4": {
9+
"Micro\\Plugin\\Uuid\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Stanislau.Komar",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"minimum-stability": "stable",
19+
"require": {
20+
"ramsey/uuid": "^4.2"
21+
}
22+
}

src/Business/UuidExtractor.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
use Ramsey\Uuid\Uuid;
6+
7+
class UuidExtractor implements UuidExtractorInterface
8+
{
9+
/**
10+
* {@inheritDoc}
11+
*/
12+
public function fromString(string $uuid): string
13+
{
14+
return Uuid::fromString($uuid)->toString();
15+
}
16+
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function fromBytes(string $bytes): string
21+
{
22+
return Uuid::fromBytes($bytes)->toString();
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function fromInteger(string $integer): string
29+
{
30+
return Uuid::fromInteger($integer)->toString();
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function fromDatetime(\DateTimeInterface $dateTime): string
37+
{
38+
return Uuid::fromDateTime($dateTime)->toString();
39+
}
40+
}

src/Business/UuidExtractorFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
class UuidExtractorFactory implements UuidExtractorFactoryInterface
6+
{
7+
/**
8+
* {@inheritDoc}
9+
*/
10+
public function create(): UuidExtractorInterface
11+
{
12+
return new UuidExtractor();
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
interface UuidExtractorFactoryInterface
6+
{
7+
/**
8+
* @return UuidExtractorInterface
9+
*/
10+
public function create(): UuidExtractorInterface;
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
interface UuidExtractorInterface
6+
{
7+
/**
8+
* Creates a UUID from a string UUID.
9+
*
10+
* @param string $source
11+
*
12+
* @return string
13+
*/
14+
public function fromString(string $source): string;
15+
16+
/**
17+
* Creates a UUID from a 16-byte string.
18+
*
19+
* @param string $bytes
20+
*
21+
* @return string
22+
*/
23+
public function fromBytes(string $bytes): string;
24+
25+
/**
26+
* Creates a UUID from a string integer.
27+
*
28+
* @param string $integer
29+
*
30+
* @return string
31+
*/
32+
public function fromInteger(string $integer): string;
33+
34+
/**
35+
* Creates a UUID from a DateTimeInterface instance
36+
*
37+
* @param \DateTimeInterface $dateTime
38+
*
39+
* @return string
40+
*/
41+
public function fromDatetime(\DateTimeInterface $dateTime): string;
42+
}

src/Business/UuidGenerator.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
use Ramsey\Uuid\Uuid;
6+
7+
class UuidGenerator implements UuidGeneratorInterface
8+
{
9+
/**
10+
* {@inheritDoc}
11+
*/
12+
public function v1(): string
13+
{
14+
return Uuid::uuid1()->toString();
15+
}
16+
17+
/**
18+
* {@inheritDoc}
19+
*/
20+
public function v3(string $namespace): string
21+
{
22+
return Uuid::uuid3($this->getUuidConstant($namespace), php_uname('n'))->toString();
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function v4(): string
29+
{
30+
return Uuid::uuid4()->toString();
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
*/
36+
public function v5(string $namespace): string
37+
{
38+
return Uuid::uuid5($this->getUuidConstant($namespace), php_uname('n'))->toString();
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function v6(): string
45+
{
46+
return Uuid::uuid6()->toString();
47+
}
48+
49+
/**
50+
* @param string $constName
51+
*
52+
* @return mixed
53+
*/
54+
protected function getUuidConstant(string $constName): string
55+
{
56+
return constant(Uuid::class . "::" . $constName);
57+
}
58+
}

src/Business/UuidGeneratorFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
class UuidGeneratorFactory implements UuidGeneratorFactoryInterface
6+
{
7+
/**
8+
* {@inheritDoc}
9+
*/
10+
public function create(): UuidGeneratorInterface
11+
{
12+
return new UuidGenerator();
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Uuid\Business\Uuid;
4+
5+
interface UuidGeneratorFactoryInterface
6+
{
7+
/**
8+
* @return UuidGeneratorInterface
9+
*/
10+
public function create(): UuidGeneratorInterface;
11+
}

0 commit comments

Comments
 (0)