Skip to content
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

[Serializer] Add context builders #43973

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 Nov 9, 2021
@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 Nov 9, 2021
@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 removed this from the 6.0 milestone Nov 9, 2021
@ogizanagi ogizanagi added this to the 6.1 milestone Nov 9, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 0478eb0 to c86c320 Nov 10, 2021
@mtarld mtarld requested a review from dunglas Nov 10, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch 2 times, most recently from 44e5b18 to 86b2834 Nov 12, 2021
@mtarld mtarld changed the base branch from 6.0 to 6.1 Dec 24, 2021
@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 Dec 24, 2021
@mtarld mtarld requested a review from dunglas Dec 24, 2021
@carsonbot carsonbot changed the title [Serializer] Add context builders Add context builders Dec 25, 2021
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from abacfdf to a2bf307 Jan 7, 2022
@mtarld mtarld requested a review from dunglas Jan 7, 2022
Copy link
Member

@dunglas dunglas left a comment

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 Jan 10, 2022
@mtarld mtarld force-pushed the feature/serializer-context-builder branch from be6b0aa to 65af0d9 Jan 10, 2022
Copy link
Member

@chalasr chalasr left a comment

@mtarld Can you rebase?

@mtarld mtarld force-pushed the feature/serializer-context-builder branch from 48f7b82 to f1b078c Jan 26, 2022
dunglas
dunglas approved these changes Feb 2, 2022
@chalasr
Copy link
Member

@chalasr chalasr commented Feb 2, 2022

Thank you @mtarld.

@chalasr chalasr merged commit fac3060 into symfony:6.1 Feb 2, 2022
8 of 9 checks passed
@mtarld mtarld deleted the feature/serializer-context-builder branch Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants