-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Serializer] Add context builders #43973
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
Merged
+2,371
−7
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Serializer/Context/ContextBuilderTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Context; | ||
|
||
/** | ||
* @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
*/ | ||
trait ContextBuilderTrait | ||
{ | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
protected array $context = []; | ||
|
||
protected function with(string $key, mixed $value): static | ||
{ | ||
$instance = new static(); | ||
$instance->context = array_merge($this->context, [$key => $value]); | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $context | ||
*/ | ||
public function withContext(array $context): static | ||
{ | ||
$instance = new static(); | ||
$instance->context = array_merge($this->context, $context); | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->context; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
src/Symfony/Component/Serializer/Context/Encoder/CsvEncoderContextBuilder.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\Context\ContextBuilderTrait; | ||
use Symfony\Component\Serializer\Encoder\CsvEncoder; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* A helper providing autocompletion for available CsvEncoder options. | ||
* | ||
* @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
*/ | ||
final class CsvEncoderContextBuilder | ||
{ | ||
use ContextBuilderTrait; | ||
|
||
/** | ||
* Configures the column delimiter character. | ||
* | ||
* Must be a single character. | ||
* | ||
* @param non-empty-string|null $delimiter | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function withDelimiter(?string $delimiter): static | ||
chalasr marked this conversation as resolved.
Show resolved
Hide resolved
mtarld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (null !== $delimiter && 1 !== \strlen($delimiter)) { | ||
throw new InvalidArgumentException(sprintf('The "%s" delimiter must be a single character.', $delimiter)); | ||
} | ||
|
||
return $this->with(CsvEncoder::DELIMITER_KEY, $delimiter); | ||
} | ||
|
||
/** | ||
* Configures the field enclosure character. | ||
* | ||
* Must be a single character. | ||
* | ||
* @param non-empty-string|null $enclosure | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function withEnclosure(?string $enclosure): static | ||
{ | ||
if (null !== $enclosure && 1 !== \strlen($enclosure)) { | ||
throw new InvalidArgumentException(sprintf('The "%s" enclosure must be a single character.', $enclosure)); | ||
} | ||
|
||
return $this->with(CsvEncoder::ENCLOSURE_KEY, $enclosure); | ||
} | ||
|
||
/** | ||
* Configures the escape character. | ||
* | ||
* Must be empty or a single character. | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function withEscapeChar(?string $escapeChar): static | ||
{ | ||
if (null !== $escapeChar && \strlen($escapeChar) > 1) { | ||
throw new InvalidArgumentException(sprintf('The "%s" escape character must be empty or a single character.', $escapeChar)); | ||
} | ||
|
||
return $this->with(CsvEncoder::ESCAPE_CHAR_KEY, $escapeChar); | ||
} | ||
|
||
/** | ||
* Configures the key separator when (un)flattening arrays. | ||
*/ | ||
public function withKeySeparator(?string $keySeparator): static | ||
{ | ||
return $this->with(CsvEncoder::KEY_SEPARATOR_KEY, $keySeparator); | ||
} | ||
|
||
/** | ||
* Configures the headers. | ||
* | ||
* @param list<mixed>|null $headers | ||
*/ | ||
public function withHeaders(?array $headers): static | ||
{ | ||
return $this->with(CsvEncoder::HEADERS_KEY, $headers); | ||
} | ||
|
||
/** | ||
* Configures whether formulas should be escaped. | ||
*/ | ||
public function withEscapedFormulas(?bool $escapedFormulas): static | ||
{ | ||
return $this->with(CsvEncoder::ESCAPE_FORMULAS_KEY, $escapedFormulas); | ||
} | ||
|
||
/** | ||
* Configures whether the decoded result should be considered as a collection | ||
* or as a single element. | ||
*/ | ||
public function withAsCollection(?bool $asCollection): static | ||
{ | ||
return $this->with(CsvEncoder::AS_COLLECTION_KEY, $asCollection); | ||
} | ||
|
||
/** | ||
* Configures whether the input (or output) is containing (or will contain) headers. | ||
*/ | ||
public function withNoHeaders(?bool $noHeaders): static | ||
{ | ||
return $this->with(CsvEncoder::NO_HEADERS_KEY, $noHeaders); | ||
} | ||
|
||
/** | ||
* Configures the end of line characters. | ||
*/ | ||
public function withEndOfLine(?string $endOfLine): static | ||
{ | ||
return $this->with(CsvEncoder::END_OF_LINE, $endOfLine); | ||
} | ||
|
||
/** | ||
* Configures whether to add the UTF-8 Byte Order Mark (BOM) | ||
* at the beginning of the encoded result or not. | ||
*/ | ||
public function withOutputUtf8Bom(?bool $outputUtf8Bom): static | ||
{ | ||
return $this->with(CsvEncoder::OUTPUT_UTF8_BOM_KEY, $outputUtf8Bom); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Serializer/Context/Encoder/JsonEncoderContextBuilder.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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\Context\ContextBuilderTrait; | ||
use Symfony\Component\Serializer\Encoder\JsonDecode; | ||
use Symfony\Component\Serializer\Encoder\JsonEncode; | ||
|
||
/** | ||
* A helper providing autocompletion for available JsonEncoder options. | ||
* | ||
* @author Mathias Arlaud <mathias.arlaud@gmail.com> | ||
*/ | ||
final class JsonEncoderContextBuilder | ||
{ | ||
use ContextBuilderTrait; | ||
|
||
/** | ||
* Configures the json_encode flags bitmask. | ||
* | ||
* @see https://www.php.net/manual/en/json.constants.php | ||
* | ||
* @param positive-int|null $options | ||
*/ | ||
public function withEncodeOptions(?int $options): static | ||
{ | ||
return $this->with(JsonEncode::OPTIONS, $options); | ||
} | ||
|
||
/** | ||
* Configures the json_decode flags bitmask. | ||
* | ||
* @see https://www.php.net/manual/en/json.constants.php | ||
* | ||
* @param positive-int|null $options | ||
*/ | ||
public function withDecodeOptions(?int $options): static | ||
{ | ||
return $this->with(JsonDecode::OPTIONS, $options); | ||
} | ||
|
||
/** | ||
* Configures whether decoded objects will be given as | ||
* associative arrays or as nested stdClass. | ||
*/ | ||
public function withAssociative(?bool $associative): static | ||
{ | ||
return $this->with(JsonDecode::ASSOCIATIVE, $associative); | ||
} | ||
|
||
/** | ||
* Configures the maximum recursion depth. | ||
* | ||
* Must be strictly positive. | ||
* | ||
* @param positive-int|null $recursionDepth | ||
*/ | ||
public function withRecursionDepth(?int $recursionDepth): static | ||
{ | ||
return $this->with(JsonDecode::RECURSION_DEPTH, $recursionDepth); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.