| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\HtmlElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Spatie\HtmlElement\Helpers\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class AbbreviationParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $element = 'div'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $classes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public static function parse(string $tag) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $parsed = (new static($tag)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'element' => $parsed->element, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'classes' => $parsed->classes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'attributes' => $parsed->attributes, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected function __construct(string $tag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->parseTag($tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function parseTag(string $tag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         foreach ($this->explodeTag($tag) as $part) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             switch ($part[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 case '.': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     $this->parseClass($part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 case '#': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     $this->parseId($part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 case '[': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     $this->parseAttribute($part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     $this->parseElement($part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function parseClass(string $class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->classes[] = ltrim($class, '.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     protected function parseId(string $id) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->attributes['id'] = ltrim($id, '#'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function parseAttribute(string $attribute) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $keyValueSet = explode('=', trim($attribute, '[]'), 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $key = $keyValueSet[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $value = $keyValueSet[1] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->attributes[$key] = trim($value, '\'"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected function parseElement(string $element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->element = $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     protected function explodeTag(string $tag) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         // First split out the attributes set with `[...=...]` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $parts = preg_split('/(?=( \[[^]]+] ))/x', $tag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Afterwards we can extract the rest of the attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return Arr::flatMap($parts, function ($part) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             if (strpos($part, '[') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 list($attributeValue, $rest) = explode(']', $part, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 return [$attributeValue] + $this->explodeTag($rest); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return preg_split('/(?=( (\.) | (\#) ))/x', $part); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  |