1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 28, 2016 - 3:47:35 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File AssetsAbstract.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
* @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core\Helpers; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\AssetsInterface; |
27
|
|
|
use \DOMElement; |
28
|
|
|
|
29
|
|
|
abstract class AssetsAbstract implements AssetsInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* DOM tag name of asset item |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $element_tag = 'a-asset-item'; |
38
|
|
|
|
39
|
|
|
protected $attrs = array(); |
40
|
|
|
|
41
|
|
|
protected $components = array(); |
42
|
|
|
/** |
43
|
|
|
* Asset constructor set asset ID |
44
|
|
|
* |
45
|
|
|
* @param string $id |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct(string $id) |
48
|
|
|
{ |
49
|
3 |
|
$this->id($id); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set ID attribute of the asset |
54
|
|
|
* |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @param string $id |
58
|
|
|
* @return AssetsInterface |
59
|
|
|
*/ |
60
|
6 |
|
public function id(string $id = 'untitled'): AssetsInterface |
61
|
|
|
{ |
62
|
6 |
|
$this->attrs['id'] = $id; |
63
|
6 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set Assets src attribute |
68
|
|
|
* |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
* |
71
|
|
|
* @param null|string $src |
72
|
|
|
* @return AssetsInterface |
73
|
|
|
*/ |
74
|
1 |
|
public function src(string $src = null): AssetsInterface |
75
|
|
|
{ |
76
|
1 |
|
$this->attrs['src'] = $src; |
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create and add DOM element of the asset |
82
|
|
|
* |
83
|
|
|
* @param \DOMDocument $aframe_dom |
84
|
|
|
* @return \DOMElement |
85
|
|
|
*/ |
86
|
2 |
|
public function domElement(\DOMDocument &$aframe_dom): DOMElement |
87
|
|
|
{ |
88
|
2 |
|
$a_asset = $aframe_dom->createElement($this->element_tag); |
89
|
|
|
/* Asset must have a id */ |
90
|
2 |
|
$this->appendAttributes($a_asset); |
91
|
|
|
|
92
|
2 |
|
foreach ($this->components as $component) { |
93
|
|
|
/* |
94
|
|
|
* Check does component has any attributes to add to DOM element. |
95
|
|
|
* default attributes most of cases are ommited so we might not have any attributes to add |
96
|
|
|
*/ |
97
|
1 |
|
if ($component->hasDOMAttributes()) |
98
|
1 |
|
$a_asset->setAttributeNode($component->getDOMAttr()); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $a_asset; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set Dom element name |
106
|
|
|
* |
107
|
|
|
* @param string $element_tag |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
3 |
|
public function setDomElementTag(string $element_tag) |
111
|
|
|
{ |
112
|
3 |
|
$this->element_tag = $element_tag; |
113
|
3 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Append DOM attributes no set by components |
117
|
|
|
* |
118
|
|
|
* @param \DOMElement $a_entity |
119
|
|
|
*/ |
120
|
2 |
|
private function appendAttributes(\DOMElement &$a_entity) |
121
|
|
|
{ |
122
|
2 |
|
foreach ($this->attrs as $attr => $val) { |
123
|
2 |
|
$this->setAttribute($a_entity, $attr, $val); |
124
|
|
|
} |
125
|
2 |
|
} |
126
|
|
|
|
127
|
2 |
|
private function setAttribute(&$a_entity, $attr, $val) |
128
|
|
|
{ |
129
|
2 |
|
if ($attr === 'id' && ($val === 'untitled' || is_numeric($val))) |
130
|
2 |
|
return; |
131
|
|
|
|
132
|
2 |
|
$a_entity->setAttribute($attr, $val); |
133
|
2 |
|
} |
134
|
|
|
} |
135
|
|
|
|