@@ -64,7 +64,7 @@ static public function getSpecVersion()
6464 *
6565 * @throws InvalidArgumentException If the YAML is not valid
6666 */
67- public static function load ($ input )
67+ public static function load ($ input, $ encoding = ' UTF-8 ' )
6868 {
6969 $ file = '' ;
7070
@@ -87,6 +87,14 @@ public static function load($input)
8787 return $ input ;
8888 }
8989
90+ $ mbConvertEncoding = false ;
91+ $ encoding = strtoupper ($ encoding );
92+ if ('UTF-8 ' != $ encoding && function_exists ('mb_convert_encoding ' ))
93+ {
94+ $ input = mb_convert_encoding ($ input , 'UTF-8 ' , $ encoding );
95+ $ mbConvertEncoding = true ;
96+ }
97+
9098 require_once dirname (__FILE__ ).'/sfYamlParser.php ' ;
9199
92100 $ yaml = new sfYamlParser ();
@@ -100,6 +108,11 @@ public static function load($input)
100108 throw new InvalidArgumentException (sprintf ('Unable to parse %s: %s ' , $ file ? sprintf ('file "%s" ' , $ file ) : 'string ' , $ e ->getMessage ()));
101109 }
102110
111+ if ($ ret && $ mbConvertEncoding )
112+ {
113+ $ ret = self ::arrayConvertEncoding ($ ret , $ encoding );
114+ }
115+
103116 return $ ret ;
104117 }
105118
@@ -122,6 +135,25 @@ public static function dump($array, $inline = 2)
122135
123136 return $ yaml ->dump ($ array , $ inline );
124137 }
138+
139+ /**
140+ * Converts all kayes and values from UTF-8 to given encoding
141+ *
142+ * @param array $result Original result
143+ * @param string $encoding The expected encoding
144+ * @param array $convertedResult Converted result
145+ * @return array
146+ */
147+ protected static function arrayConvertEncoding (array $ result , $ encoding , &$ convertedResult = array ())
148+ {
149+ foreach ($ result as $ key => $ value )
150+ {
151+ $ key = mb_convert_encoding ($ key , 'UTF-8 ' , $ encoding );
152+ $ convertedResult [$ key ] = is_array ($ value ) ? self ::arrayConvertEncoding ($ value , $ encoding , $ convertedArray ) : mb_convert_encoding ($ value , $ encoding , 'UTF-8 ' );
153+ }
154+
155+ return $ convertedResult ;
156+ }
125157}
126158
127159/**
0 commit comments