1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DH\Adf\Node\Block; |
6
|
|
|
|
7
|
|
|
use DH\Adf\Builder\TextBuilder; |
8
|
|
|
use DH\Adf\Node\BlockNode; |
9
|
|
|
use DH\Adf\Node\Inline\Text; |
10
|
|
|
use DH\Adf\Node\Node; |
11
|
|
|
use JsonSerializable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/codeBlock |
15
|
|
|
*/ |
16
|
|
|
class CodeBlock extends BlockNode implements JsonSerializable |
17
|
|
|
{ |
18
|
|
|
use TextBuilder; |
19
|
|
|
|
20
|
|
|
protected string $type = 'codeBlock'; |
21
|
|
|
protected array $allowedContentTypes = [ |
22
|
|
|
Text::class, |
23
|
|
|
]; |
24
|
|
|
private ?string $language; |
25
|
|
|
|
26
|
|
|
public function __construct(?string $language = null, ?BlockNode $parent = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($parent); |
29
|
|
|
$this->language = $language; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function load(array $data, ?BlockNode $parent = null): self |
33
|
|
|
{ |
34
|
|
|
self::checkNodeData(static::class, $data, ['attrs']); |
35
|
|
|
self::checkRequiredKeys(['language'], $data['attrs']); |
36
|
|
|
|
37
|
|
|
$node = new self($data['attrs']['language'], $parent); |
38
|
|
|
|
39
|
|
|
// set content if defined |
40
|
|
|
if (\array_key_exists('content', $data)) { |
41
|
|
|
foreach ($data['content'] as $nodeData) { |
42
|
|
|
$class = Node::NODE_MAPPING[$nodeData['type']]; |
43
|
|
|
$child = $class::load($nodeData, $node); |
44
|
|
|
|
45
|
|
|
$node->append($child); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $node; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function attrs(): array |
53
|
|
|
{ |
54
|
|
|
$attrs = parent::attrs(); |
55
|
|
|
|
56
|
|
|
if (null !== $this->language) { |
57
|
|
|
$attrs['language'] = $this->language; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $attrs; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|