| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dallgoot\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dallgoot\Yaml\Yaml as Y; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author  Stéphane Rebai <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license Apache 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link    TODO : url to specific online doc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class API | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** @var null|bool */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $hasDocStart; // null = no docstart, true = docstart before document comments, false = docstart after document comments | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     private $_references = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $_comments   = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     // private $_documents  = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     private $_tags = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public $type = Y::MAPPING; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     public $value = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     const UNKNOWN_REFERENCE = self::class.": no reference named '%s'"; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     const UNAMED_REFERENCE  = self::class.": reference MUST have a name"; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Adds a reference. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param string $name  The reference name | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param mixed  $value The reference value | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws \UnexpectedValueException  (description) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function addReference(string $name, $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if (empty($name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             throw new \UnexpectedValueException(self::UNAMED_REFERENCE, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $this->_references[(string) $name] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * Return the reference saved by $name | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $name Name of the reference | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @return mixed Value of the reference | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @throws \UnexpectedValueException    if there's no reference by that $name | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public function &getReference($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if (array_key_exists($name, $this->_references)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             return $this->_references[$name]; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         throw new \UnexpectedValueException(sprintf(self::UNKNOWN_REFERENCE, $name), 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     public function getAllReferences():array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return $this->_references; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * Adds a comment. | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param int    $lineNumber The line number at which thecomment should appear | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $value      The comment | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function addComment(int $lineNumber, $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $this->_comments[$lineNumber] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Gets the comment at $lineNumber | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param int|null $lineNumber The line number | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @return string|array The comment at $lineNumber OR ALL comments. | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function getComment(int $lineNumber = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (array_key_exists((int) $lineNumber, $this->_comments)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return $this->_comments[$lineNumber]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |         return $this->_comments; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * Sets the text when the content is *only* a litteral | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $value The value | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     public function setText(string $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $this->value .= $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * TODO:  what to do with these tags ??? | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * Adds a tag. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $value The value | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     public function addTag(string $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $this->_tags[] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * Determines if it has YAML document start string => '---'. | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * @return     boolean  True if has document start, False otherwise. | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function hasDocStart() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |         return is_bool($this->hasDocStart); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * Sets the document start. | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * @param      null|bool  $value  The value : null = no docstart, true = docstart before document comments, false = docstart after document comments | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     public function setDocStart($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |         $this->hasDocStart = $value; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 134 |  |  |  |