| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Dallgoot\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Dallgoot\Yaml\{Yaml as Y}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author  Stéphane Rebai <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @license Apache 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @link    TODO : url to specific online doc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $tagName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** @var Node|null|string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var null|Node|NodeList */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $raw; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const LEGACY_TAGS = ['!str', '!binary', '!set', '!omap', 'php/object', '!inline', '!long']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Tag constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $tagName the name of the tag like '!!str' (WITHOUT the first "!") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param mixed $value  any PHP variable type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @throws \Exception if $tagName is an invalid string or absent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct(string $tagName, $raw) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (empty($tagName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             throw new \Exception(self::class.": a tag MUST have a name", 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->tagName = $tagName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->raw = $raw; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Return the tagged value according to Tag type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param      scalar|Node|NodeList  $value  The value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return     mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @todo implement others legacy types if needed  + Symfony type 'php/object' (unserialize ???) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function buildValue(object &$parent = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $value = $this->raw; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         if ($value instanceof Node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             $value = new NodeList($this->raw); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $value->forceType(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // $parent = Builder::$_root; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         switch ($this->tagName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             case '!set': $value->type = Y::SET;break; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             case '!omap': $value->type = Y::SEQUENCE;break; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             // assumed to be !str,!binary | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             default: $parent = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                      $value->type = Y::RAW; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->value = Builder::buildNodeList($value, $parent); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         return $this->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function isKnown():bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return in_array($this->tagName, self::LEGACY_TAGS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Should verify if the tag is correct | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param      string  $providedName  The provided name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @todo is this required ??? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     // private function checkNameValidity(string $providedName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     // { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         /* TODO  implement and throw Exception if invalid (setName method ???) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          *The suffix must not contain any “!” character. This would cause the tag shorthand to be interpreted as having a named tag handle. In addition, the suffix must not contain the “[”, “]”, “{”, “}” and “,” characters. These characters would cause ambiguity with flow collection structures. If the suffix needs to specify any of the above restricted characters, they must be escaped using the “%” character. This behavior is consistent with the URI character escaping rules (specifically, section 2.3 of RFC2396). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     // } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |