Skip to content

WIP: Serializer context #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Symfony/Component/Serializer/Context/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context;

/**
* Holds contexts needed by serialalization indexed by their FQCN.
*
* @author Mathias Arlaud <[email protected]>
*/
final class Context
{
/**
* @template T of object
*
* @var array<class-string<T>, T> $optionsMap
*/
private array $optionsMap = [];

public function __construct(...$options)
{
$this->addOptions(...$options);
}

public function addOptions(object ...$optionsList): self
{
foreach ($optionsList as $options) {
$this->optionsMap[get_class($options)] = $options;
}

return $this;
}

/**
* @param class-string $optionsClass
*/
public function removeOptions(string ...$optionsClassList): self
{
foreach ($optionsClassList as $optionsClass) {
unset($this->optionsMap[$optionsClass]);
}

return $this;
}

/**
* @param class-string $optionsClass
*/
public function hasOptions(string $optionsClass): bool
{
return isset($this->optionsMap[$optionsClass]);
}

/**
* @template T of object
*
* @param class-string<T>
*
* @return T|null
*/
public function getOptions(string $optionsClass): ?object
{
return $this->optionsMap[$optionsClass] ?? null;
}
}
247 changes: 247 additions & 0 deletions src/Symfony/Component/Serializer/Context/Encoder/CsvEncoderOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Context\Encoder;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;

final class CsvEncoderOptions
{
/**
* Column delimiter character.
* Must be one character only.
*/
private ?string $delimiter = null;

/**
* Field enclosure character.
* This must be one character only.
*/
private ?string $enclosure = null;

/**
* Escape character.
* Must be one character only.
*/
private ?string $escapeChar = null;

/**
* Whether formulas should be escaped.
*/
private ?string $endOfLine = null;

/**
* Key separator when (un)flattening arrays.
*/
private ?string $keySeparator = null;

/**
* CSV table headers.
*
* @var list<string>|null $headers
*/
private ?array $headers = null;

/**
* Whether the decoded result should be considered as a collection
* or as a single element.
*/
private ?bool $asCollection = null;

/**
* Whether formulas should be escaped.
*/
private ?bool $escapeFormulas = null;

/**
* Whether the input (or output) isn't containing (or won't contain) headers.
*/
private ?bool $withoutHeaders = null;

/**
* End of line characters.
*/
private ?bool $outputUtf8Bom = null;

public function getDelimiter(): string
{
return $this->delimiter ?? ',';
}

public function setDelimiter(?string $delimiter): self
{
if (null !== $delimiter && \strlen($delimiter) > 1) {
throw new InvalidArgumentException(sprintf('The "%s" delimiter is not valid. It must be one character only.', $delimiter));
}

$this->delimiter = $delimiter;

return $this;
}

public function getEnclosure(): string
{
return $this->enclosure ?? '"';
}

public function setEnclosure(?string $enclosure): self
{
if (null !== $enclosure && \strlen($enclosure) > 1) {
throw new InvalidArgumentException(sprintf('The "%s" enclosure is not valid. It must be one character only.', $enclosure));
}

$this->enclosure = $enclosure;

return $this;
}

public function getEscapeChar(): string
{
return $this->escapeChar ?? '';
}

public function setEscapeChar(?string $escapeChar): self
{
if (null !== $escapeChar && \strlen($escapeChar) > 1) {
throw new InvalidArgumentException(sprintf('The "%s" escape character is not valid. It must be one character only.', $escapeChar));
}

$this->escapeChar = $escapeChar;

return $this;
}

public function getEndOfLine(): string
{
return $this->endOfLine ?? "\n";
}

public function setEndOfLine(?string $endOfLine): self
{
$this->endOfLine = $endOfLine;

return $this;
}

public function getKeySeparator(): string
{
return $this->keySeparator ?? '.';
}

public function setKeySeparator(?string $keySeparator): self
{
$this->keySeparator = $keySeparator;

return $this;
}

/**
* @return list<string>
*/
public function getHeaders(): array
{
return $this->headers ?? [];
}

/**
* @param list<string>|null $headers
*/
public function setHeaders(?array $headers): self
{
$this->headers = $headers;

return $this;
}

public function isAsCollection(): bool
{
return $this->asCollection ?? true;
}

public function setAsCollection(?bool $asCollection): self
{
$this->asCollection = $asCollection;

return $this;
}

public function isEscapeFormulas(): bool
{
return $this->escapeFormulas ?? false;
}

public function setEscapeFormulas(?bool $escapeFormulas): self
{
$this->escapeFormulas = $escapeFormulas;

return $this;
}

public function isWithoutHeaders(): bool
{
return $this->withoutHeaders ?? false;
}

public function setWithoutHeaders(?bool $withoutHeaders): self
{
$this->withoutHeaders = $withoutHeaders;

return $this;
}

public function isOutputUtf8Bom(): bool
{
return $this->outputUtf8Bom ?? false;
}

public function setOutputUtf8Bom(?bool $outputUtf8Bom): self
{
$this->outputUtf8Bom = $outputUtf8Bom;

return $this;
}

public function merge(self $other): self
{
$this->delimiter ??= $other->delimiter;
$this->enclosure ??= $other->enclosure;
$this->escapeChar ??= $other->escapeChar;
$this->endOfLine ??= $other->endOfLine;
$this->keySeparator ??= $other->keySeparator;
$this->headers ??= $other->headers;
$this->escapeFormulas ??= $other->escapeFormulas;
$this->withoutHeaders ??= $other->withoutHeaders;
$this->outputUtf8Bom ??= $other->outputUtf8Bom;

return $this;
}

/**
* @internal
*
* @return array<string, mixed>
*/
public function toLegacyContext(): array
{
return [
'csv_delimiter' => $this->getDelimiter(),
'csv_enclosure' => $this->getEnclosure(),
'csv_escape_char' => $this->getEscapeChar(),
'csv_key_separator' => $this->getKeySeparator(),
'csv_headers' => $this->getHeaders(),
'csv_escape_formulas' => $this->isEscapeFormulas(),
'as_collection' => $this->isAsCollection(),
'no_headers' => $this->isWithoutHeaders(),
'csv_end_of_line' => $this->getEndOfLine(),
'output_utf8_bom' => $this->isOutputUtf8Bom(),
];
}
}
Loading