media-cloud-sync
/
includes
/
sdk
/
google
/
ramsey
/
uuid
/
src
/
Generator
/
PeclUuidNameGenerator.php
PeclUuidNameGenerator.php in Media Cloud Sync 1.2.5, at includes/sdk/google/ramsey/uuid/src/Generator/PeclUuidNameGenerator.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the ramsey/uuid library |
| 5 | * |
| 6 | * For the full copyright and license information, please view the LICENSE |
| 7 | * file that was distributed with this source code. |
| 8 | * |
| 9 | * @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | declare (strict_types=1); |
| 13 | namespace Dudlewebs\WPMCS\Ramsey\Uuid\Generator; |
| 14 | |
| 15 | use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\NameException; |
| 16 | use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; |
| 17 | use function sprintf; |
| 18 | use function uuid_generate_md5; |
| 19 | use function uuid_generate_sha1; |
| 20 | use function uuid_parse; |
| 21 | /** |
| 22 | * PeclUuidNameGenerator generates strings of binary data from a namespace and a |
| 23 | * name, using ext-uuid |
| 24 | * |
| 25 | * @link https://pecl.php.net/package/uuid ext-uuid |
| 26 | */ |
| 27 | class PeclUuidNameGenerator implements NameGeneratorInterface |
| 28 | { |
| 29 | /** @psalm-pure */ |
| 30 | public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string |
| 31 | { |
| 32 | switch ($hashAlgorithm) { |
| 33 | case 'md5': |
| 34 | $uuid = uuid_generate_md5($ns->toString(), $name); |
| 35 | break; |
| 36 | case 'sha1': |
| 37 | $uuid = uuid_generate_sha1($ns->toString(), $name); |
| 38 | break; |
| 39 | default: |
| 40 | throw new NameException(sprintf('Unable to hash namespace and name with algorithm \'%s\'', $hashAlgorithm)); |
| 41 | } |
| 42 | return uuid_parse($uuid); |
| 43 | } |
| 44 | } |
| 45 |