Completed
Push — master ( 169632...a17b83 )
by Marko
04:08
created

AframeDOMDocument::appendEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 27, 2016 - 9:55:09 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         AframeDOMDocument.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\DOM;
25
26
use \AframeVR\Core\Config;
27
use \DOMImplementation;
28
use \DOMDocumentType;
29
use \DOMDocument;
30
use \AframeVR\Core\Entity;
31
use \AframeVR\Interfaces\AssetsInterface;
32
33
final class AframeDOMDocument extends DOMImplementation
34
{
35
    use AframeDOMProcessor;
36
37
    /**
38
     * A-Frame DOM Document type
39
     *
40
     * @var \DOMDocumentType
41
     */
42
    protected $doctypeObj;
43
44
    /**
45
     * A-Frame DOM Document
46
     *
47
     * @var \DOMDocument
48
     */
49
    protected $docObj;
50
51
    /**
52
     * Scene meta tile
53
     *
54
     * @var string $scene_title
55
     */
56
    protected $scene_title = 'Untitled';
57
58
    /**
59
     * Scene meta description
60
     *
61
     * @var string $scene_description
62
     */
63
    protected $scene_description = '';
64
65
    /**
66
     * <head>
67
     *
68
     * @var \DOMElement
69
     */
70
    protected $head;
71
72
    /**
73
     * <body>
74
     *
75
     * @var \DOMElement
76
     */
77
    protected $body;
78
79
    /**
80
     * <a-scene>
81
     *
82
     * @var \DOMElement
83
     */
84
    protected $scene;
85
86
    /**
87
     * <a-assets>
88
     *
89
     * @var \DOMElement
90
     */
91
    protected $assets;
92
93
    /************
94
     * CONFIG
95
     ***********/
96
    
97
    /**
98
     * Nicely formats output with indentation and extra space.
99
     *
100
     * @var bool
101
     */
102
    protected $format_output = false;
103
104
    /**
105
     * CDN Of aframe.js
106
     *
107
     * @var string
108
     */
109
    protected $cdn_url;
110
    
111
    /**
112
     * Whether to use CDN
113
     *
114
     * @var bool $use_cdn
115
     */
116
    protected $use_cdn = false;
117
    
118
    /**
119
     * aframe assets URI relative to App's base URL / domain
120
     * 
121
     * @var string assets_uri
122
     */
123
    protected $assets_uri;
124
    
125
    /**
126
     * A-Frame DOM
127
     *
128
     * @param Config $config            
129
     */
130 71
    public function __construct(Config $config)
131
    {
132
        /* Config */
133 71
        $this->format_output = is_bool($config->get('format_output')) ? $config->get('format_output') : false;
134 71
        $this->cdn_url       = is_string($config->get('cdn_url')) ? $config->get('cdn_url') : null;
135 71
        $this->use_cdn       = is_bool($config->get('use_cdn')) ? $config->get('use_cdn') : false;
136 71
        $this->assets_uri    = is_string($config->get('assets_uri')) ? $config->get('assets_uri') : '/aframe';
137
        
138
        /* Create HTML5 Document type */
139 71
        $this->createDocType('html');
140
        
141
        /* Create A-Frame DOM Document */
142 71
        $this->createAframeDocument();
143
        
144
        /* Create boostrap elements */
145 71
        $this->documentBootstrap();
146 71
    }
147
148
    /**
149
     * Render scene this DOM Object is attached to
150
     *
151
     * @return string
152
     */
153 5
    public function render(): string
154
    {
155 5
        $this->docObj->formatOutput = $this->format_output;
156
        
157 5
        $html = $this->docObj->getElementsByTagName('html')->item(0);
158
        /* Make sure we do not add duplicates when render is called multiple times */
159 5
        if (! $html->hasChildNodes()) {
160 5
            $this->renderHead();
161
            
162 5
            $html->appendChild($this->head);
163
            
164 5
            $this->renderBody();
165
            
166 5
            $html->appendChild($this->body);
167
        }
168 5
        return $this->format_output ? $this->correctOutputFormat($this->docObj->saveHTML()) : $this->docObj->saveHTML();
169
    }
170
171
    /**
172
     * Set Scene meta title
173
     *
174
     * @param string $title            
175
     */
176 7
    public function setTitle(string $title)
177
    {
178 7
        $this->scene_title = $title;
179 7
    }
180
181
    /**
182
     * Set Scene meta description
183
     *
184
     * @param string $description            
185
     */
186 7
    public function setDescription(string $description)
187
    {
188 7
        $this->scene_description = $description;
189 7
    }
190
191
    /**
192
     * Append entities
193
     *
194
     * @param array $entities            
195
     * @return void
196
     */
197 7
    public function appendEntities(array $entities)
198
    {
199 7
        if (! empty($entities)) {
200 2
            foreach ($entities as $entity) {
201 2
                $this->appendEntity($entity);
202
            }
203
        }
204 7
    }
205
206
    /**
207
     * Append assets
208
     *
209
     * @param array $assets            
210
     * @return void
211
     */
212 2
    public function appendAssets(array $assets)
213
    {
214 2
        if (! empty($assets)) {
215 2
            if ($this->format_output) {
216 2
                $com = $this->docObj->createComment('');
217 2
                $this->scene->appendChild($com);
218
            }
219 2
            foreach ($assets as $asset) {
220 2
                $this->appendAsset($asset);
221
            }
222 2
            $this->scene->appendChild($this->assets);
223
        }
224 2
    }
225
226
    /**
227
     * Append asset
228
     *
229
     * Create asset DOMElement
230
     *
231
     * @param AssetsInterface $asset            
232
     */
233 2
    public function appendAsset(AssetsInterface $asset)
234
    {
235 2
        $this->appendFormatComment('assets', "\n\t");
236 2
        $this->assets->appendChild($asset->domElement($this->docObj));
237 2
    }
238
239
    /**
240
     * Create entity DOMElement
241
     *
242
     * Created entity and append it to scene
243
     *
244
     * @param Entity $entity            
245
     * @return void
246
     */
247 2
    public function appendEntity(Entity $entity)
248
    {
249 2
        $this->appendFormatComment('scene', "\n");
250 2
        $this->scene->appendChild($entity->domElement($this->docObj));
251 2
    }
252
253
    /**
254
     * Get HTML of Scene only
255
     *
256
     * @return string
257
     */
258 3
    public function renderSceneOnly()
259
    {
260 3
        $html               = new DOMDocument();
261 3
        $html->formatOutput = $this->format_output;
262
        
263 3
        $html_scene = $html->importNode($this->scene, true);
264 3
        $html->appendChild($html_scene);
265 3
        return $this->format_output ? $this->correctOutputFormat($html->saveHTML()) : $html->saveHTML();
266
    }
267
}
268