Passed
Push — main ( e16d46...16e3d2 )
by Damien
07:21
created

Mention::__construct()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 25
c 0
b 0
f 0
rs 9.3554
cc 5
nc 3
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Inline;
6
7
use DH\Adf\Node\BlockNode;
8
use DH\Adf\Node\InlineNode;
9
use InvalidArgumentException;
10
11
/**
12
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/mention
13
 */
14
class Mention extends InlineNode
15
{
16
    public const ACCESS_LEVEL_NONE = 'NONE';
17
    public const ACCESS_LEVEL_SITE = 'SITE';
18
    public const ACCESS_LEVEL_APPLICATION = 'APPLICATION';
19
    public const ACCESS_LEVEL_CONTAINER = 'CONTAINER';
20
21
    public const USER_TYPE_DEFAULT = 'DEFAULT';
22
    public const USER_TYPE_SPECIAL = 'SPECIAL';
23
    public const USER_TYPE_APP = 'APP';
24
25
    protected string $type = 'mention';
26
    private string $id;
27
    private string $text;
28
    private ?string $accessLevel;
29
    private ?string $userType;
30
31
    public function __construct(string $id, string $text, ?string $accessLevel = null, ?string $userType = null)
32
    {
33
        if (null !== $accessLevel && !\in_array($accessLevel, [
34
            self::ACCESS_LEVEL_NONE,
35
            self::ACCESS_LEVEL_SITE,
36
            self::ACCESS_LEVEL_APPLICATION,
37
            self::ACCESS_LEVEL_CONTAINER,
38
            '',
39
        ], true)) {
40
            throw new InvalidArgumentException('Invalid access level');
41
        }
42
43
        if (null !== $userType && !\in_array($userType, [
44
            self::USER_TYPE_DEFAULT,
45
            self::USER_TYPE_SPECIAL,
46
            self::USER_TYPE_APP,
47
            //            '',
48
        ], true)) {
49
            throw new InvalidArgumentException('Invalid user type');
50
        }
51
52
        $this->id = $id;
53
        $this->text = $text;
54
        $this->accessLevel = $accessLevel;
55
        $this->userType = $userType;
56
    }
57
58
    public static function load(array $data, ?BlockNode $parent = null): self
59
    {
60
        self::checkNodeData(static::class, $data);
61
        self::checkRequiredKeys(['id', 'text'], $data['attrs']);
62
63
        return new self(
64
            $data['attrs']['id'],
65
            $data['attrs']['text'],
66
            $data['attrs']['accessLevel'] ?? null,
67
            $data['attrs']['userType'] ?? null
68
        );
69
    }
70
71
    protected function attrs(): array
72
    {
73
        $attrs = parent::attrs();
74
        $attrs['id'] = $this->id;
75
        $attrs['text'] = $this->text;
76
77
        if (null !== $this->accessLevel) {
78
            $attrs['accessLevel'] = $this->accessLevel;
79
        }
80
81
        if (null !== $this->userType) {
82
            $attrs['userType'] = $this->userType;
83
        }
84
85
        return $attrs;
86
    }
87
}
88