Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 <ben@benramsey.com>
10
 * @license http://opensource.org/licenses/MIT MIT
11
 */
12
 
13
declare(strict_types=1);
14
 
15
namespace Ramsey\Uuid\Codec;
16
 
17
use Ramsey\Uuid\Builder\UuidBuilderInterface;
18
use Ramsey\Uuid\Exception\InvalidArgumentException;
19
use Ramsey\Uuid\Exception\InvalidUuidStringException;
20
use Ramsey\Uuid\Uuid;
21
use Ramsey\Uuid\UuidInterface;
22
 
23
use function bin2hex;
24
use function hex2bin;
25
use function implode;
26
use function sprintf;
27
use function str_replace;
28
use function strlen;
29
use function substr;
30
 
31
/**
32
 * StringCodec encodes and decodes RFC 4122 UUIDs
33
 *
34
 * @link http://tools.ietf.org/html/rfc4122
35
 *
36
 * @psalm-immutable
37
 */
38
class StringCodec implements CodecInterface
39
{
40
    /**
41
     * Constructs a StringCodec
42
     *
43
     * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
44
     */
45
    public function __construct(private UuidBuilderInterface $builder)
46
    {
47
    }
48
 
49
    public function encode(UuidInterface $uuid): string
50
    {
51
        $hex = bin2hex($uuid->getFields()->getBytes());
52
 
53
        /** @var non-empty-string */
54
        return sprintf(
55
            '%08s-%04s-%04s-%04s-%012s',
56
            substr($hex, 0, 8),
57
            substr($hex, 8, 4),
58
            substr($hex, 12, 4),
59
            substr($hex, 16, 4),
60
            substr($hex, 20),
61
        );
62
    }
63
 
64
    /**
65
     * @psalm-return non-empty-string
66
     * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
67
     * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
68
     */
69
    public function encodeBinary(UuidInterface $uuid): string
70
    {
71
        /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
72
        return $uuid->getFields()->getBytes();
73
    }
74
 
75
    /**
76
     * @throws InvalidUuidStringException
77
     *
78
     * @inheritDoc
79
     */
80
    public function decode(string $encodedUuid): UuidInterface
81
    {
82
        return $this->builder->build($this, $this->getBytes($encodedUuid));
83
    }
84
 
85
    public function decodeBytes(string $bytes): UuidInterface
86
    {
87
        if (strlen($bytes) !== 16) {
88
            throw new InvalidArgumentException(
89
                '$bytes string should contain 16 characters.'
90
            );
91
        }
92
 
93
        return $this->builder->build($this, $bytes);
94
    }
95
 
96
    /**
97
     * Returns the UUID builder
98
     */
99
    protected function getBuilder(): UuidBuilderInterface
100
    {
101
        return $this->builder;
102
    }
103
 
104
    /**
105
     * Returns a byte string of the UUID
106
     */
107
    protected function getBytes(string $encodedUuid): string
108
    {
109
        $parsedUuid = str_replace(
110
            ['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'],
111
            '',
112
            $encodedUuid
113
        );
114
 
115
        $components = [
116
            substr($parsedUuid, 0, 8),
117
            substr($parsedUuid, 8, 4),
118
            substr($parsedUuid, 12, 4),
119
            substr($parsedUuid, 16, 4),
120
            substr($parsedUuid, 20),
121
        ];
122
 
123
        if (!Uuid::isValid(implode('-', $components))) {
124
            throw new InvalidUuidStringException(
125
                'Invalid UUID string: ' . $encodedUuid
126
            );
127
        }
128
 
129
        return (string) hex2bin($parsedUuid);
130
    }
131
}