| 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 |  |  |     protected $attrs = array(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $components = array(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $assets_uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Asset constructor set asset ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |     public function __construct(string $id, string $assets_uri) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         $this->id($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |         $this->assets_uri = $assets_uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |         $this->init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 3 |  |     public function init() | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Set ID attribute of the asset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return AssetsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |     public function id(string $id = 'untitled'): AssetsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 6 |  |         $this->attrs['id'] = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Set Assets src attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param null|string $src | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return AssetsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |     public function src(string $src = null): AssetsInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         $this->attrs['src'] = $this->assets_uri ? $this->assets_uri . $src : $src; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Create and add DOM element of the asset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param \DOMDocument $aframe_dom | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return \DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     public function domElement(\DOMDocument &$aframe_dom): DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         $a_asset = $aframe_dom->createElement($this->element_tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         /* Asset must have a id */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |         $this->appendAttributes($a_asset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         foreach ($this->components as $component) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |              * Check does component has any attributes to add to DOM element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |              * default attributes most of cases are ommited so we might not have any attributes to add | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             if ($component->hasDOMAttributes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |                 $a_asset->setAttributeNode($component->getDOMAttr()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         return $a_asset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Set Dom element name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param string $element_tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |     public function setDomElementTag(string $element_tag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |         $this->element_tag = $element_tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * Append DOM attributes no set by components | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param \DOMElement $a_entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 2 |  |     private function appendAttributes(\DOMElement &$a_entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |         foreach ($this->attrs as $attr => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 2 |  |             $this->setAttribute($a_entity, $attr, $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 2 |  |     private function setAttribute(&$a_entity, $attr, $val) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  |         if ($attr === 'id' && ($val === 'untitled' || is_numeric($val))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 2 |  |         $a_entity->setAttribute($attr, $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |  | 
            
                        
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.