| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dallgoot\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dallgoot\Yaml\{Yaml as Y, Regex as R}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | final class Node2PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Returns the correct PHP datatype for the value of the current Node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param Node $n a Node object to be evaluated as PHP type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @return mixed  The value as PHP type : scalar, array or Compact, DateTime | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @throws \Exception if occurs in self::getScalar or self::getCompact | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public static function get(Node $n) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (is_null($n->value)) return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if ($n->type & (Y::REF_CALL | Y::SCALAR)) return self::getScalar($n->value); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         // if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         //     return self::getCompact(substr($n->value, 1, -1), $n->type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if ($n->type & (Y::COMPACT_MAPPING | Y::COMPACT_SEQUENCE| Y::JSON)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             return $n->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $expected = [//Y::JSON   => $n->value, //json_decode($n->value, false, 512, JSON_PARTIAL_OUTPUT_ON_ERROR), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                      Y::QUOTED => trim($n->value, "\"'"), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                      Y::RAW    => strval($n->value)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $expected[$n->type] ?? null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Returns the correct PHP type according to the string value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $v a string value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return mixed The value with appropriate PHP type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws \Exception if happens in R::isDate or R::isNumber | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private static function getScalar(string $v) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (R::isDate($v))   return date_create($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (R::isNumber($v)) return self::getNumber($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $types = ['yes'   => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'no'    => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     'true'  => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     'false' => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     'null'  => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     '.inf'  => INF, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     '-.inf' => -INF, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     '.nan'  => NAN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return array_key_exists(strtolower($v), $types) ? $types[strtolower($v)] : strval($v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Returns the correct PHP type according to the string value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $v a string value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return int|float   The scalar value with appropriate PHP type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private static function getNumber(string $v) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (preg_match("/^(0o\d+)$/i", $v))      return intval(base_convert($v, 8, 10)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (preg_match("/^(0x[\da-f]+)$/i", $v)) return intval(base_convert($v, 16, 10)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return is_bool(strpos($v, '.')) ? intval($v) : floatval($v); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Returns a Compact object representing the inline object/array provided as string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param string  $mappingOrSeqString The mapping or sequence string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param integer $type               The type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return Compact The compact object equivalent to $mappingOrString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @throws \Exception if raised by self::getScalar | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     private static function getCompact(string $objOr, int $type):object | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         if ($type === Y::COMPACT_SEQUENCE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             return new Compact(self::getArray($objOrSeqString)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         if ($type === Y::COMPACT_MAPPING) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             return new Compact(self::getObject($objOrSeqString)); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         throw new TypeError(__METHOD__.": paramater 'type' can only be YAML::COMPACT_SEQUENCE or YAML::COMPACT_MAPPING"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @todo : that's not robust enough, improve it.this should handle references present inside the string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     private function getArray($sequenceString) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $out = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $f = function ($e) { return self::getScalar(trim($e));}; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         foreach (array_map($f, explode(",", $sequenceString)) as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $out[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $out; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Gets the compact object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @todo : that's not robust enough, improve it.this should handle references present inside the string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     private static function getObject($objectString) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $out = new \StdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         foreach (explode(',', $objectString) as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             list($keyName, $keyValue) = explode(':', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $out->{trim($keyName, '\'" ')} = self::getScalar(trim($keyValue)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return $out; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  | } |