Skip to content

[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
merged 1 commit into from
Feb 2, 2022

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Nov 9, 2021

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fixes partially #30818
License MIT
Doc PR TODO

This PR introduces ContextBuilders as discussed in #30818.

The main idea here is to introduce an abstract context builder that could be extended to create concrete context builders.
These context builders will allow serialization context construction using withers (maybe setters are better?) while providing validation, documentation, and IDE autocompletion.
Once construction is ready, toArray (maybe build is better?) can be called to generate the actual serialization context.

For example:

use Symfony\Component\Serializer\Context\Encoder\CsvEncoderContextBuilder;
use Symfony\Component\Serializer\Context\Normalizer\DateTimeNormalizerContextBuilder;

$initialContext = ['custom_key' => 'custom_value']);

$contextBuilder = (new DateTimeNormalizerContextBuilder()
  ->withContext($initialContext)
  ->withFormat('Y_m_d')
  ->withTimezone('GMT');

$contextBuilder = (new CsvEncoderContextBuilder())
  ->withContext($contextBuilder->toArray())
  ->withDelimiter('-')
  ->withHeaders(['foo', 'bar']);

$this->serializer->serialize($data, 'csv', $contextBuilder->toArray());
// Serialization context will be:
// [
//   'custom_key' => 'custom_value',
//   'datetime_format' => 'Y_m_d',
//   'datetime_timezone' => DateTimeZone instance,
//   'csv_delimiter' => '-',
//   'csv_headers' => ['foo', 'bar'],
// ]

@mtarld mtarld requested a review from dunglas as a code owner November 9, 2021 13:09
@carsonbot carsonbot added this to the 6.0 milestone Nov 9, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 1a6e2e3 to 0478eb0 Compare November 9, 2021 13:11
@mtarld mtarld changed the title Add context builers [Serializer] Add context builers Nov 9, 2021
@mtarld mtarld changed the title [Serializer] Add context builers [Serializer] Add context builders Nov 9, 2021
@carsonbot carsonbot changed the title [Serializer] Add context builders Add context builders Nov 9, 2021
@ogizanagi ogizanagi modified the milestones: 6.0, 6.1 Nov 9, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 0478eb0 to c86c320 Compare November 10, 2021 14:20
@mtarld mtarld requested a review from dunglas November 10, 2021 14:22
@mtarld mtarld force-pushed the feature/serializer-context-builder branch 2 times, most recently from 44e5b18 to 86b2834 Compare November 12, 2021 14:51
@mtarld mtarld changed the base branch from 6.0 to 6.1 December 24, 2021 10:08
@mtarld mtarld changed the title Add context builders [Serializer] Add context builders Dec 24, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 86b2834 to abacfdf Compare December 24, 2021 10:09
@mtarld mtarld requested a review from dunglas December 24, 2021 10:30
@carsonbot carsonbot changed the title [Serializer] Add context builders Add context builders Dec 25, 2021
Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good DX improvement! This also needs a rebase.

@mtarld mtarld force-pushed the feature/serializer-context-builder branch 2 times, most recently from c2739a4 to be6b0aa Compare January 10, 2022 11:26
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from be6b0aa to 65af0d9 Compare January 10, 2022 17:55
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtarld Can you rebase?

@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 48f7b82 to f1b078c Compare January 26, 2022 08:12
@chalasr
Copy link
Member

chalasr commented Feb 2, 2022

Thank you @mtarld.

@chalasr chalasr merged commit fac3060 into symfony:6.1 Feb 2, 2022
@mtarld mtarld deleted the feature/serializer-context-builder branch February 2, 2022 18:13
@fabpot fabpot mentioned this pull request Apr 15, 2022
javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this pull request Apr 26, 2022
…mtarld)

This PR was merged into the 6.1 branch.

Discussion
----------

[Serializer] Add context builders documentation

Related to symfony/symfony#43973
Closes symfony#16470

Commits
-------

7be3c93 [Serializer] Add context builders documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants