├── .mailmap ├── .gitignore ├── .github ├── stale.yml ├── dependabot.yml └── workflows │ ├── fix-coding-standards-violations.yml │ └── ci.yml ├── .docheader ├── .php-cs-fixer.php ├── examples ├── bootstrap.php └── generate.php ├── src └── Broadway │ └── UuidGenerator │ ├── UuidGeneratorInterface.php │ ├── Converter │ ├── BinaryUuidConverterInterface.php │ └── BinaryUuidConverter.php │ ├── Rfc4122 │ └── Version4Generator.php │ └── Testing │ ├── MockUuidGenerator.php │ └── MockUuidSequenceGenerator.php ├── README.md ├── phpunit.xml.dist ├── composer.json ├── LICENSE ├── test └── Broadway │ └── UuidGenerator │ ├── Rfc4122 │ └── Version4GeneratorTest.php │ ├── Testing │ ├── MockUuidGeneratorTest.php │ └── MockUuidSequenceGeneratorTest.php │ └── Converter │ └── BinaryUuidConverterTest.php ├── Makefile └── CHANGELOG.md /.mailmap: -------------------------------------------------------------------------------- 1 | othillo 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | .php-cs-fixer.cache 4 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # see https://probot.github.io/apps/stale/ 2 | 3 | # inherit settings from https://github.com/broadway/coding-standard/blob/master/.github/stale.yml 4 | _extends: coding-standard 5 | -------------------------------------------------------------------------------- /.docheader: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the broadway/uuid-generator package. 3 | * 4 | * (c) 2020 Broadway project 5 | * 6 | * For the full copyright and license information, please view the LICENSE 7 | * file that was distributed with this source code. 8 | */ 9 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | - package-ecosystem: github-actions 9 | directory: "/" 10 | schedule: 11 | interval: weekly 12 | -------------------------------------------------------------------------------- /.php-cs-fixer.php: -------------------------------------------------------------------------------- 1 | setFinder( 6 | \PhpCsFixer\Finder::create() 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/test', 10 | __DIR__ . '/examples', 11 | ]) 12 | ); 13 | 14 | return $config; 15 | -------------------------------------------------------------------------------- /.github/workflows/fix-coding-standards-violations.yml: -------------------------------------------------------------------------------- 1 | name: "Fix coding standards violations" 2 | 3 | on: 4 | push: 5 | branches: 6 | - "master" 7 | schedule: 8 | - cron: "37 13 * * 2" 9 | workflow_dispatch: 10 | 11 | jobs: 12 | check-coding-standards: 13 | uses: "broadway/github-actions/.github/workflows/fix-coding-standards-violations.yml@main" 14 | -------------------------------------------------------------------------------- /examples/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | ./test/ 17 | 18 | 19 | 20 | 21 | ./src/ 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Broadway/UuidGenerator/Rfc4122/Version4Generator.php: -------------------------------------------------------------------------------- 1 | toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Broadway/UuidGenerator/Converter/BinaryUuidConverter.php: -------------------------------------------------------------------------------- 1 | getBytes(); 23 | } 24 | 25 | public static function fromBytes(string $bytes): string 26 | { 27 | return Uuid::fromBytes($bytes)->toString(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "broadway/uuid-generator", 3 | "description": "UUID generator for broadway/broadway.", 4 | "license": "MIT", 5 | "require": { 6 | "php": ">=7.2" 7 | }, 8 | "require-dev": { 9 | "ramsey/uuid": "^4.0", 10 | "phpunit/phpunit": "^9.5", 11 | "broadway/coding-standard": "^1.2", 12 | "phpstan/phpstan": "^1.0" 13 | }, 14 | "suggest": { 15 | "ramsey/uuid": "Allows creating UUIDs" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "Broadway\\UuidGenerator\\": "src/Broadway/UuidGenerator" 20 | } 21 | }, 22 | "autoload-dev": { 23 | "psr-4": { 24 | "Broadway\\UuidGenerator\\": "test/Broadway/UuidGenerator" 25 | } 26 | }, 27 | "extra": { 28 | "branch-alias": { 29 | "dev-master": "2.0.x-dev" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Broadway/UuidGenerator/Testing/MockUuidGenerator.php: -------------------------------------------------------------------------------- 1 | uuid = $uuid; 31 | } 32 | 33 | public function generate(): string 34 | { 35 | return $this->uuid; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Broadway project - https://github.com/broadway 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /src/Broadway/UuidGenerator/Testing/MockUuidSequenceGenerator.php: -------------------------------------------------------------------------------- 1 | uuids = (array) $uuids; 34 | } 35 | 36 | public function generate(): string 37 | { 38 | if (0 === count($this->uuids)) { 39 | throw new \RuntimeException('No more uuids in sequence'); 40 | } 41 | 42 | return array_shift($this->uuids); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/Broadway/UuidGenerator/Rfc4122/Version4GeneratorTest.php: -------------------------------------------------------------------------------- 1 | generate(); 28 | 29 | $this->assertIsString($uuid); 30 | } 31 | 32 | /** 33 | * @test 34 | */ 35 | public function it_should_generate_a_version_4_uuid(): void 36 | { 37 | $generator = new Version4Generator(); 38 | $uuid = $generator->generate(); 39 | 40 | $uuidObject = Uuid::fromString($uuid); 41 | 42 | $this->assertEquals(4, $uuidObject->getVersion()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .DEFAULT_GOAL:=help 2 | 3 | .PHONY: dependencies 4 | dependencies: 5 | composer install --no-interaction --no-suggest --no-scripts --ansi 6 | 7 | .PHONY: test 8 | test: 9 | vendor/bin/phpunit --testdox --exclude-group=none --colors=always 10 | 11 | .PHONY: qa 12 | qa: php-cs-fixer-ci phpstan 13 | 14 | .PHONY: php-cs-fixer 15 | php-cs-fixer: 16 | vendor/bin/php-cs-fixer fix --no-interaction --allow-risky=yes --diff --verbose 17 | 18 | .PHONY: php-cs-fixer-ci 19 | php-cs-fixer-ci: 20 | vendor/bin/php-cs-fixer fix --dry-run --no-interaction --allow-risky=yes --diff --verbose 21 | 22 | .PHONY: phpstan 23 | phpstan: 24 | vendor/bin/phpstan analyse -l max src 25 | 26 | .PHONY: changelog 27 | changelog: 28 | git log $$(git describe --abbrev=0 --tags)...HEAD --no-merges --pretty=format:"* [%h](http://github.com/${TRAVIS_REPO_SLUG}/commit/%H) %s (%cN)" 29 | 30 | .PHONY: license 31 | license: 32 | vendor/bin/docheader check --no-interaction --ansi -vvv {src,test,examples} 33 | 34 | # Based on https://suva.sh/posts/well-documented-makefiles/ 35 | help: ## Display this help 36 | @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) 37 | -------------------------------------------------------------------------------- /examples/generate.php: -------------------------------------------------------------------------------- 1 | generate()); 20 | } 21 | } 22 | 23 | echo "A random generated uuid:\n"; 24 | $randomGenerator = new Broadway\UuidGenerator\Rfc4122\Version4Generator(); 25 | generateAndOutput5($randomGenerator); 26 | 27 | echo "\n"; 28 | 29 | echo "A generator that will always return the same uuid (for testing):\n"; 30 | $mockUuidGenerator = new Broadway\UuidGenerator\Testing\MockUuidGenerator(42); 31 | generateAndOutput5($mockUuidGenerator); 32 | 33 | echo "\n"; 34 | 35 | echo "A generator that will always return the same sequence of uuids and throw an exception if depleted (for testing):\n"; 36 | $mockUuidSequenceGenerator = new Broadway\UuidGenerator\Testing\MockUuidSequenceGenerator([1, 1, 2, 3, 5, 8, 13, 21]); 37 | generateAndOutput5($mockUuidSequenceGenerator); 38 | -------------------------------------------------------------------------------- /test/Broadway/UuidGenerator/Testing/MockUuidGeneratorTest.php: -------------------------------------------------------------------------------- 1 | createMockUuidGenerator(); 26 | $uuid = $generator->generate(); 27 | 28 | $this->assertIsString($uuid); 29 | } 30 | 31 | /** 32 | * @test 33 | */ 34 | public function it_generates_the_same_string(): void 35 | { 36 | $generator = $this->createMockUuidGenerator(); 37 | 38 | for ($i = 0; $i < 5; ++$i) { 39 | $this->assertEquals('e2d0c739-53ac-434c-8d7a-03e29b400566', $generator->generate()); 40 | } 41 | } 42 | 43 | private function createMockUuidGenerator(): MockUuidGenerator 44 | { 45 | return new MockUuidGenerator('e2d0c739-53ac-434c-8d7a-03e29b400566'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: "CI" 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - "master" 7 | push: 8 | branches: 9 | - "master" 10 | schedule: 11 | - cron: "37 13 * * 1" 12 | 13 | jobs: 14 | tests: 15 | name: "Run tests" 16 | runs-on: "ubuntu-20.04" 17 | strategy: 18 | matrix: 19 | php-version: 20 | - "7.4" 21 | - "8.0" 22 | - "8.1" 23 | - "8.2" 24 | steps: 25 | - name: "Checkout" 26 | uses: "actions/checkout@v4" 27 | - name: "Install PHP" 28 | uses: "shivammathur/setup-php@v2" 29 | with: 30 | php-version: "${{ matrix.php-version }}" 31 | coverage: "none" 32 | env: 33 | fail-fast: true 34 | - name: "Validate composer.json and composer.lock" 35 | run: "composer validate --strict --no-interaction --ansi" 36 | - name: "Install dependencies with Composer" 37 | uses: "ramsey/composer-install@v3" 38 | - name: "Run tests" 39 | run: "make test" 40 | 41 | check-coding-standards: 42 | name: "Check coding standards" 43 | uses: "broadway/github-actions/.github/workflows/check-coding-standards.yml@main" 44 | 45 | static-analysis: 46 | name: "Static analysis" 47 | runs-on: "ubuntu-20.04" 48 | steps: 49 | - name: "Checkout" 50 | uses: "actions/checkout@v4" 51 | - name: "Install PHP" 52 | uses: "shivammathur/setup-php@v2" 53 | with: 54 | php-version: "8.0" 55 | coverage: "none" 56 | - name: "Install dependencies with Composer" 57 | uses: "ramsey/composer-install@v3" 58 | - name: "Run PHPStan" 59 | run: "make phpstan" 60 | -------------------------------------------------------------------------------- /test/Broadway/UuidGenerator/Converter/BinaryUuidConverterTest.php: -------------------------------------------------------------------------------- 1 | assertEquals('0x260b70eba5b74fde916a4cc021745c13', '0x'.bin2hex($binary)); 28 | } 29 | 30 | /** 31 | * @test 32 | */ 33 | public function it_throws_when_converting_invalid_string_uuid_to_binary_uuid(): void 34 | { 35 | $this->expectException('InvalidArgumentException'); 36 | $this->expectExceptionMessage('Invalid UUID string: yolo'); 37 | BinaryUuidConverter::fromString('YOLO'); 38 | } 39 | 40 | /** 41 | * @test 42 | */ 43 | public function it_converts_binary_uuid_to_string_uuid(): void 44 | { 45 | $this->assertEquals( 46 | '260b70eb-a5b7-4fde-916a-4cc021745c13', 47 | BinaryUuidConverter::fromBytes(hex2bin('260b70eba5b74fde916a4cc021745c13')) 48 | ); 49 | } 50 | 51 | /** 52 | * @test 53 | */ 54 | public function it_throws_when_converting_invalid_binary_uuid_to_string_uuid(): void 55 | { 56 | $this->expectException('InvalidArgumentException'); 57 | $this->expectExceptionMessage('$bytes string should contain 16 characters.'); 58 | BinaryUuidConverter::fromBytes('YOLO'); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /test/Broadway/UuidGenerator/Testing/MockUuidSequenceGeneratorTest.php: -------------------------------------------------------------------------------- 1 | createMockUuidGenerator(); 36 | $uuid = $generator->generate(); 37 | 38 | $this->assertIsString($uuid); 39 | } 40 | 41 | /** 42 | * @test 43 | */ 44 | public function it_generates_the_same_string(): void 45 | { 46 | $generator = $this->createMockUuidGenerator(); 47 | 48 | foreach ($this->uuids as $uuid) { 49 | $this->assertEquals($uuid, $generator->generate()); 50 | } 51 | } 52 | 53 | /** 54 | * @test 55 | */ 56 | public function it_throws_an_exception_when_pool_is_empty(): void 57 | { 58 | $this->expectException('RuntimeException'); 59 | $generator = $this->createMockUuidGenerator(); 60 | 61 | for ($i = 0; $i < 5; ++$i) { 62 | $generator->generate(); 63 | } 64 | } 65 | 66 | private function createMockUuidGenerator(): MockUuidSequenceGenerator 67 | { 68 | return new MockUuidSequenceGenerator($this->uuids); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.0 4 | 5 | ## 0.5.0 6 | 7 | * [e8bb0ca](http://github.com/broadway/broadway-uuid-generator/commit/e8bb0caeaf1a0e67ea1a9c216fab68cb90ffc51f) strict types (othillo) 8 | * [f14dbee](http://github.com/broadway/broadway-uuid-generator/commit/f14dbee20eebb0e2928151bc3184f9bf4af4c6b2) updated license header in examples (othillo) 9 | * [ad6c2f6](http://github.com/broadway/broadway-uuid-generator/commit/ad6c2f61cf08698b6a97ba10bd742ec3d554e097) Makefile (othillo) 10 | * [b20cbd5](http://github.com/broadway/broadway-uuid-generator/commit/b20cbd56edc99cbdfd286812b9b3342aeac2d749) added license (othillo) 11 | * [b99496b](http://github.com/broadway/broadway-uuid-generator/commit/b99496bbe3e21f058f3f35222cf69641ebe1faed) PHPStan level max (othillo) 12 | * [0c7cdc4](http://github.com/broadway/broadway-uuid-generator/commit/0c7cdc4a1b10e8681d04a17c0ef07e510f6d0c52) added Travis CI build status badge (othillo) 13 | * [44656ab](http://github.com/broadway/broadway-uuid-generator/commit/44656ab596f7789eca2765e82fb1669db2c878b7) applied coding standard (othillo) 14 | * [eb98610](http://github.com/broadway/broadway-uuid-generator/commit/eb98610f69f8bf8a767f2d84efc03d6dc320668a) PHPUnit 8 (othillo) 15 | * [a9f4067](http://github.com/broadway/broadway-uuid-generator/commit/a9f40677f77babc136f739d9fe36d4253abce351) require php >= 7.2 (othillo) 16 | * [1466ab4](http://github.com/broadway/broadway-uuid-generator/commit/1466ab4d6ba3c236bae0f0b7fef6810cef939325) only test actively supported PHP versions (othillo) 17 | * [0a2a0e9](http://github.com/broadway/broadway-uuid-generator/commit/0a2a0e94369f2414bdfd861353db1f165d59e057) run tests in CI (othillo) 18 | 19 | ## 0.4.0 20 | 21 | - **BC Break** We dropped support for ramsey/uuid ^2.0 22 | 23 | ## 0.3.0 24 | 25 | - added a binary uuid converter 26 | - added PHPUnit as a development dependency 27 | 28 | ## 0.2.0 29 | 30 | - **BC Break** Required PHP version is now 5.5.9 or higher 31 | - We merged support for ramsey/uuid ^3.0 32 | --------------------------------------------------------------------------------