| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\HtmlElement; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | class HtmlElement | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |     protected $element; | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** @var \Spatie\HtmlElement\Attributes */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     protected $attributes; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     /** @var string */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     protected $contents; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @link https://github.com/spatie/html-element#examples | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * el('p');                                 <p></p> | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * el('p', 'Hello!')                        <p>Hello!</p> | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * el('p#intro', 'Hello!')                  <p id="intro">Hello!</p> | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * el('p', ['id' => 'intro'], 'Hello!')     <p id="intro">Hello!</p> | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @param string       $tag        The html element tag. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @param array|string $attributes When only two arguments are passed, the second parameter | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      *                                 represents the content instead of the attribute. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param array|string $contents   Contents can be passed in as a string or an array which | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      *                                 will be concatenated as siblings. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public static function render(string $tag, $attributes = null, $contents = null) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return (new static($tag, $attributes, $contents))->renderTag(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected function __construct(...$arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         list($abbreviation, $attributes, $contents) = $this->parseArguments($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->attributes = new Attributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->parseContents($contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->parseAbbreviation($abbreviation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->parseAttributes($attributes); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected function parseArguments($arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $attributes = isset($arguments[2]) ? $arguments[1] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $contents = $arguments[2] ?? $arguments[1] ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $tags = preg_split('/ \s* > \s* /x', $arguments[0], 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (isset($tags[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $contents = static::render($tags[1], [], $contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return [$tags[0], $attributes, $contents]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     protected function parseContents($contents) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->contents = is_array($contents) ? implode('', $contents) : $contents; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function parseAbbreviation(string $abbreviation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $parsed = AbbreviationParser::parse($abbreviation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->element = $parsed['element'] ?: 'div'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->attributes->addClass($parsed['classes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($parsed['attributes'] as $attribute => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->attributes->setAttribute($attribute, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     protected function parseAttributes(array $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->attributes->setAttributes($attributes); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected function renderTag() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return TagRenderer::render($this->element, $this->attributes, $this->contents); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |