[Serializer] Remove CsvEncoder "as_collection" deprecation & change default value
This commit is contained in:
parent
c6a077cdde
commit
22dd071b03
@ -382,6 +382,7 @@ SecurityBundle
|
|||||||
Serializer
|
Serializer
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
* The default value of the `CsvEncoder` "as_collection" option was changed to `true`.
|
||||||
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.
|
* The `AbstractNormalizer::handleCircularReference()` method has two new `$format` and `$context` arguments.
|
||||||
|
|
||||||
Translation
|
Translation
|
||||||
|
@ -6,6 +6,7 @@ CHANGELOG
|
|||||||
|
|
||||||
* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
|
* throw an exception when creating a `Serializer` with normalizers which neither implement `NormalizerInterface` nor `DenormalizerInterface`
|
||||||
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
|
* throw an exception when creating a `Serializer` with encoders which neither implement `EncoderInterface` nor `DecoderInterface`
|
||||||
|
* changed the default value of the `CsvEncoder` "as_collection" option to `true`
|
||||||
|
|
||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
@ -33,6 +33,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
|
|
||||||
private $formulasStartCharacters = ['=', '-', '+', '@'];
|
private $formulasStartCharacters = ['=', '-', '+', '@'];
|
||||||
private $defaultContext = [
|
private $defaultContext = [
|
||||||
|
self::AS_COLLECTION_KEY => true,
|
||||||
self::DELIMITER_KEY => ',',
|
self::DELIMITER_KEY => ',',
|
||||||
self::ENCLOSURE_KEY => '"',
|
self::ENCLOSURE_KEY => '"',
|
||||||
self::ESCAPE_CHAR_KEY => '\\',
|
self::ESCAPE_CHAR_KEY => '\\',
|
||||||
@ -135,7 +136,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
$headerCount = [];
|
$headerCount = [];
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
|
list($delimiter, $enclosure, $escapeChar, $keySeparator, , , $asCollection) = $this->getCsvOptions($context);
|
||||||
|
|
||||||
while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
|
while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
|
||||||
$nbCols = \count($cols);
|
$nbCols = \count($cols);
|
||||||
@ -183,7 +184,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
}
|
}
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|
||||||
if ($context[self::AS_COLLECTION_KEY] ?? false) {
|
if ($asCollection) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,10 +192,6 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($context['as_collection'])) {
|
|
||||||
@trigger_error('Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.', E_USER_DEPRECATED);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is only one data line in the document, return it (the line), the result is not considered as a collection
|
// If there is only one data line in the document, return it (the line), the result is not considered as a collection
|
||||||
return $result[0];
|
return $result[0];
|
||||||
}
|
}
|
||||||
@ -233,12 +230,13 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
|
|||||||
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
|
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
|
||||||
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
|
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
|
||||||
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
|
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
|
||||||
|
$asCollection = $context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY];
|
||||||
|
|
||||||
if (!\is_array($headers)) {
|
if (!\is_array($headers)) {
|
||||||
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));
|
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas];
|
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $asCollection];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -327,21 +327,6 @@ CSV
|
|||||||
$this->assertFalse($this->encoder->supportsDecoding('foo'));
|
$this->assertFalse($this->encoder->supportsDecoding('foo'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @group legacy
|
|
||||||
* @expectedDeprecation Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.
|
|
||||||
*/
|
|
||||||
public function testDecodeLegacy()
|
|
||||||
{
|
|
||||||
$expected = ['foo' => 'a', 'bar' => 'b'];
|
|
||||||
|
|
||||||
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
|
|
||||||
foo,bar
|
|
||||||
a,b
|
|
||||||
CSV
|
|
||||||
, 'csv'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testDecodeAsSingle()
|
public function testDecodeAsSingle()
|
||||||
{
|
{
|
||||||
$expected = ['foo' => 'a', 'bar' => 'b'];
|
$expected = ['foo' => 'a', 'bar' => 'b'];
|
||||||
@ -382,9 +367,7 @@ foo
|
|||||||
a
|
a
|
||||||
|
|
||||||
CSV
|
CSV
|
||||||
, 'csv', [
|
, 'csv'));
|
||||||
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDecodeToManyRelation()
|
public function testDecodeToManyRelation()
|
||||||
@ -449,9 +432,7 @@ CSV
|
|||||||
a;bar-baz
|
a;bar-baz
|
||||||
'hell''o';b;c
|
'hell''o';b;c
|
||||||
CSV
|
CSV
|
||||||
, 'csv', [
|
, 'csv'));
|
||||||
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDecodeCustomSettingsPassedInContext()
|
public function testDecodeCustomSettingsPassedInContext()
|
||||||
@ -466,7 +447,6 @@ CSV
|
|||||||
CsvEncoder::ENCLOSURE_KEY => "'",
|
CsvEncoder::ENCLOSURE_KEY => "'",
|
||||||
CsvEncoder::ESCAPE_CHAR_KEY => '|',
|
CsvEncoder::ESCAPE_CHAR_KEY => '|',
|
||||||
CsvEncoder::KEY_SEPARATOR_KEY => '-',
|
CsvEncoder::KEY_SEPARATOR_KEY => '-',
|
||||||
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
|
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user