Skip to content

Commit e415465

Browse files
committed
Added possibility to parse yaml for value when encoding is not utf-8
1 parent 58e21c6 commit e415465

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

lib/addon/sfData.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getDeleteCurrentData()
5353
protected function doLoadDataFromFile($file)
5454
{
5555
// import new datas
56-
$data = sfYaml::load($file);
56+
$data = sfYaml::load($file, sfConfig::get('sf_charset', 'UTF-8'));
5757

5858
$this->loadDataFromArray($data);
5959
}

lib/config/sfYamlConfigHandler.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static public function parseYaml($configFile)
6969
}
7070

7171
// parse our config
72-
$config = sfYaml::load($configFile);
72+
$config = sfYaml::load($configFile, sfConfig::get('sf_charset', 'UTF-8'));
7373

7474
if ($config === false)
7575
{

lib/i18n/extract/sfI18nYamlGeneratorExtractor.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function extract($content)
2929
{
3030
$this->strings = array();
3131

32-
$config = sfYaml::load($content);
32+
$config = sfYaml::load($content, sfConfig::get('sf_charset', 'UTF-8'));
3333

3434
if (!isset($config['generator']['param']['config']))
3535
{
@@ -82,7 +82,7 @@ public function extract($content)
8282
}
8383
}
8484
}
85-
85+
8686
return $this->strings;
8787
}
8888

lib/i18n/extract/sfI18nYamlValidateExtractor.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function extract($content)
2727
{
2828
$strings = array();
2929

30-
$config = sfYaml::load($content);
30+
$config = sfYaml::load($content, sfConfig::get('sf_charset', 'UTF-8'));
3131

3232
// New validate.yml format
3333

lib/yaml/sfYaml.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
/**

lib/yaml/sfYamlParser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function parse($value)
5757
$this->currentLine = '';
5858
$this->lines = explode("\n", $this->cleanup($value));
5959

60+
if (function_exists('mb_detect_encoding') && false === mb_detect_encoding($value, 'UTF-8', true))
61+
{
62+
throw new InvalidArgumentException('The YAML value does not appear to be valid UTF-8.');
63+
}
64+
6065
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2)
6166
{
6267
$mbEncoding = mb_internal_encoding();

0 commit comments

Comments
 (0)