opsway / zohobooks-api

ZohoBooks PHP library
MIT License
6 stars 10 forks source link

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

Open phansys opened 7 years ago

phansys commented 7 years ago

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 commented 7 years ago

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();