UlidIdentity::fromString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 19
rs 9.9332
cc 3
nc 3
nop 1
1
<?php
2
3
/*
4
 * identity-extra (https://github.com/phpgears/identity-extra).
5
 * Identity objects for PHP.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/identity-extra
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Identity\Extra;
15
16
use Gears\Identity\AbstractIdentity;
17
use Gears\Identity\Exception\InvalidIdentityException;
18
use Ulid\Ulid;
19
20
/**
21
 * Universally Unique Lexicographically Sortable ID identity.
22
 */
23
class UlidIdentity extends AbstractIdentity
24
{
25
    private const ULID_LENGTH = 26;
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws InvalidIdentityException
31
     */
32
    final public static function fromString(string $value)
33
    {
34
        if (\strlen($value) !== static::ULID_LENGTH) {
35
            throw new InvalidIdentityException(
36
                \sprintf('Provided identity value "%s" is not a valid ULID.', $value)
37
            );
38
        }
39
40
        try {
41
            Ulid::fromString($value);
42
        } catch (\Exception $exception) {
43
            throw new InvalidIdentityException(
44
                \sprintf('Provided identity value "%s" is not a valid ULID.', $value),
45
                0,
46
                $exception
47
            );
48
        }
49
50
        return new static($value);
51
    }
52
}
53