-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadata.php
More file actions
114 lines (94 loc) · 3.23 KB
/
Metadata.php
File metadata and controls
114 lines (94 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Contentstack\Utils\Model;
use Contentstack\Utils\Enum\EmbedItemType;
use Contentstack\Utils\Enum\StyleType;
class Metadata {
public function __construct(object $element)
{
if ($element instanceof \DOMElement) {
$this->itemType = !empty($element->getAttribute('type'))? EmbedItemType::byValue($element->getAttribute('type')) : EmbedItemType::get(EmbedItemType::ENTRY);
$this->styleType = !empty($element->getAttribute('sys-style-type')) ? StyleType::byValue($element->getAttribute('sys-style-type')) : StyleType::get(StyleType::BLOCK);
$this->itemUid = !empty($element->getAttribute('data-sys-entry-uid')) ? $element->getAttribute('data-sys-entry-uid') : $element->getAttribute('data-sys-asset-uid');
$this->contentTypeUid = $element->getAttribute('data-sys-content-type-uid');
$this->text = $element->textContent;
$this->attributes = $element->attributes;
$this->element = $element;
} else {
$this->attributes = get_object_vars($element->attrs);
$this->itemType = !empty($this->attributes->type)? EmbedItemType::byValue($this->attributes->type) : EmbedItemType::get(EmbedItemType::ENTRY);
$this->styleType = !empty($this->attributes['display-type']) ? StyleType::byValue($this->attributes['display-type']) : StyleType::get(StyleType::BLOCK);
$this->itemUid = !empty($this->attributes['entry-uid']) ? $this->attributes['entry-uid'] : $this->attributes['asset-uid'];
$this->contentTypeUid = $this->attributes['content-type-uid'];
$this->text = ($element->children && count($element->children) > 0) ? $element->children[0]->text : '';
$this->element = $element;
}
}
/* Properties */
/**
* @var EmbedItemType
*/
protected $itemType;
/**
* @var StyleType
*/
protected $styleType;
/**
* @var string
*/
protected $itemUid;
/**
* @var string
*/
protected $contentTypeUid;
/**
* @var string
*/
protected $text;
/**
* @var DOMNamedNodeMap
*/
protected $attributes;
/**
* @var string
*/
protected $element;
/* Methods */
public function getItemType(): EmbedItemType
{
return $this->itemType;
}
public function getStyleType(): StyleType
{
return $this->styleType;
}
public function getItemUid(): string
{
return $this->itemUid;
}
public function getContentTypeUid(): string
{
return $this->contentTypeUid;
}
public function getAttributes(): object
{
return $this->attributes;
}
public function getAttribute(string $name): ?string {
if ($this->element instanceof \DOMElement) {
return $this->element->getAttribute($name);
} else if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
public function getOuterHTML(): string
{
$doc = $this->element->ownerDocument;
return $doc->saveHTML($this->element);
}
public function getText(): string
{
return $this->text;
}
}