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

[RFC] Use symfony/options-resolver in order to handle request requirements and parameters #15

Open
phansys opened this issue Jan 10, 2017 · 1 comment

Comments

@phansys
Copy link
Contributor

phansys commented Jan 10, 2017

Use symfony/options-resolver in order to handle request requirements and parameters.

Example with invoice creation payload:

$resolver = new \Symfony\Component\OptionsResolver\OptionsResolver();
$resolver
    ->setDefined([
        'adjustment',
        'adjustment_description',
        'allow_partial_payments',
        'avatax_tax_code',
        'avatax_use_code',
        'avatax_exempt_no',
        'contact_persons',
        'customer_id',
        'custom_body',
        'custom_subject',
        'custom_fields',
        'date',
        'discount',
        'discount_type',
        'due_date',
        'exchange_rate',
        'invoiced_estimate_id',
        'is_discount_before_tax',
        'is_inclusive_tax',
        'line_items',
        'number',
        'payment_options',
        'payment_terms',
        'payment_terms_label',
        'recurring_id',
        'salesperson_name',
        'shipping_charge',
        'tax_authority_id',
        'tax_exemption_id',
        'tax_exemption_id',
        'template_id',
        'vat_treatment',
    ])
    ->setAllowedTypes('adjustment', 'float')
    ->setAllowedTypes('adjustment_description', 'string')
    ->setAllowedTypes('allow_partial_payments', 'boolean')
    ->setAllowedTypes('avatax_tax_code', 'string')
    ->setAllowedTypes('avatax_use_code', 'string')
    ->setAllowedTypes('avatax_exempt_no', 'string')
    ->setAllowedTypes('contact_persons', 'array')
    ->setAllowedTypes('customer_id', 'string')
    ->setAllowedTypes('custom_fields', 'array')
    ->setAllowedTypes('custom_body', 'string')
    ->setAllowedTypes('custom_subject', 'string')
    ->setAllowedTypes('date', 'string')
    ->setAllowedTypes('discount', 'string')
    ->setAllowedTypes('discount_type', 'string')
    ->setAllowedTypes('due_date', 'string')
    ->setAllowedTypes('exchange_rate', 'float')
    ->setAllowedTypes('invoiced_estimate_id', 'string')
    ->setAllowedTypes('is_discount_before_tax', 'boolean')
    ->setAllowedTypes('is_inclusive_tax', 'boolean')
    ->setAllowedTypes('line_items', 'array')
    ->setAllowedTypes('number', 'string')
    ->setAllowedTypes('payment_options', 'array')
    ->setAllowedTypes('payment_terms', 'int')
    ->setAllowedTypes('payment_terms_label', 'string')
    ->setAllowedTypes('recurring_id', 'string')
    ->setAllowedTypes('salesperson_name', 'string')
    ->setAllowedTypes('shipping_charge', 'float')
    ->setAllowedTypes('tax_authority_id', 'string')
    ->setAllowedTypes('tax_exemption_id', 'string')
    ->setAllowedTypes('template_id', 'string')
    ->setAllowedTypes('vat_treatment', 'string')
    ->setRequired([
        'customer_id',
    ])
;

$invoicePayload = $optionResolver->resolve([
    'some_parameter' => 'some_wrong_type';
]);
@mrVrAlex
Copy link
Collaborator

mrVrAlex commented Jan 18, 2017

Great idea. And in future rules for payload options-resolver can encapsulate in DTO classes.
E.g.

$entity = Invoice::create(['customer_id' => 1]);
$result = $api->save($entity);
$result->getCustomerId(); $result->getDueDate(); .... $result->toArray();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants