Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c0733d

Browse files
committed
Allows overriding of $escapeFormulas in context
1 parentfc743a1 commit9c0733d

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

‎src/Symfony/Component/Serializer/Encoder/CsvEncoder.php‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2727
constESCAPE_CHAR_KEY ='csv_escape_char';
2828
constKEY_SEPARATOR_KEY ='csv_key_separator';
2929
constHEADERS_KEY ='csv_headers';
30+
constESCAPE_FORMULAS_KEY ='csv_escape_formulas';
3031

3132
private$delimiter;
3233
private$enclosure;
@@ -75,11 +76,11 @@ public function encode($data, $format, array $context = array())
7576
}
7677
}
7778

78-
list($delimiter,$enclosure,$escapeChar,$keySeparator,$headers) =$this->getCsvOptions($context);
79+
list($delimiter,$enclosure,$escapeChar,$keySeparator,$headers,$escapeFormulas) =$this->getCsvOptions($context);
7980

8081
foreach ($dataas &$value) {
8182
$flattened =array();
82-
$this->flatten($value,$flattened,$keySeparator);
83+
$this->flatten($value,$flattened,$keySeparator,'',$escapeFormulas);
8384
$value =$flattened;
8485
}
8586
unset($value);
@@ -183,14 +184,15 @@ public function supportsDecoding($format)
183184
* @param array $result
184185
* @param string $keySeparator
185186
* @param string $parentKey
187+
* @param bool $escapeFormulas
186188
*/
187-
privatefunctionflatten(array$array,array &$result,$keySeparator,$parentKey ='')
189+
privatefunctionflatten(array$array,array &$result,$keySeparator,$parentKey ='',$escapeFormulas =false)
188190
{
189191
foreach ($arrayas$key =>$value) {
190192
if (is_array($value)) {
191-
$this->flatten($value,$result,$keySeparator,$parentKey.$key.$keySeparator);
193+
$this->flatten($value,$result,$keySeparator,$parentKey.$key.$keySeparator,$escapeFormulas);
192194
}else {
193-
if ($this->escapeFormulas &&\in_array(mb_substr($value,0,1),$this->formulasStartCharacters,true)) {
195+
if ($escapeFormulas &&\in_array(mb_substr($value,0,1),$this->formulasStartCharacters,true)) {
194196
$result[$parentKey.$key] ="\t".$value;
195197
}else {
196198
$result[$parentKey.$key] =$value;
@@ -206,12 +208,13 @@ private function getCsvOptions(array $context)
206208
$escapeChar =isset($context[self::ESCAPE_CHAR_KEY]) ?$context[self::ESCAPE_CHAR_KEY] :$this->escapeChar;
207209
$keySeparator =isset($context[self::KEY_SEPARATOR_KEY]) ?$context[self::KEY_SEPARATOR_KEY] :$this->keySeparator;
208210
$headers =isset($context[self::HEADERS_KEY]) ?$context[self::HEADERS_KEY] :array();
211+
$escapeFormulas =isset($context[self::ESCAPE_FORMULAS_KEY]) ?$context[self::ESCAPE_FORMULAS_KEY] :$this->escapeFormulas;
209212

210213
if (!is_array($headers)) {
211214
thrownewInvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".',self::HEADERS_KEY,gettype($headers)));
212215
}
213216

214-
returnarray($delimiter,$enclosure,$escapeChar,$keySeparator,$headers);
217+
returnarray($delimiter,$enclosure,$escapeChar,$keySeparator,$headers,$escapeFormulas);
215218
}
216219

217220
/**

‎src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function testEncodeCustomHeaders()
173173
$this->assertEquals($csv,$this->encoder->encode($value,'csv',$context));
174174
}
175175

176-
publicfunctiontestEncodeFormulaValues()
176+
publicfunctiontestEncodeFormulas()
177177
{
178178
$this->encoder =newCsvEncoder(',','"','\\','.',true);
179179

@@ -206,7 +206,7 @@ public function testEncodeFormulaValues()
206206
,$this->encoder->encode(array('@MyDataColumn'),'csv'));
207207
}
208208

209-
publicfunctiontestDoNotEncodeFormulaValues()
209+
publicfunctiontestDoNotEncodeFormulas()
210210
{
211211
$this->assertSame(<<<'CSV'
212212
0
@@ -237,6 +237,45 @@ public function testDoNotEncodeFormulaValues()
237237
,$this->encoder->encode(array('@MyDataColumn'),'csv'));
238238
}
239239

240+
publicfunctiontestEncodeFormulasWithSettingsPassedInContext()
241+
{
242+
$this->assertSame(<<<'CSV'
243+
0
244+
"=2+3"
245+
246+
CSV
247+
,$this->encoder->encode(array('=2+3'),'csv',array(
248+
CsvEncoder::ESCAPE_FORMULAS_KEY =>true,
249+
)));
250+
251+
$this->assertSame(<<<'CSV'
252+
0
253+
"-2+3"
254+
255+
CSV
256+
,$this->encoder->encode(array('-2+3'),'csv',array(
257+
CsvEncoder::ESCAPE_FORMULAS_KEY =>true,
258+
)));
259+
260+
$this->assertSame(<<<'CSV'
261+
0
262+
"+2+3"
263+
264+
CSV
265+
,$this->encoder->encode(array('+2+3'),'csv',array(
266+
CsvEncoder::ESCAPE_FORMULAS_KEY =>true,
267+
)));
268+
269+
$this->assertSame(<<<'CSV'
270+
0
271+
"@MyDataColumn"
272+
273+
CSV
274+
,$this->encoder->encode(array('@MyDataColumn'),'csv',array(
275+
CsvEncoder::ESCAPE_FORMULAS_KEY =>true,
276+
)));
277+
}
278+
240279
publicfunctiontestSupportsDecoding()
241280
{
242281
$this->assertTrue($this->encoder->supportsDecoding('csv'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp