Laravel-通过对象将数据发送到API

时间:2018-11-21 13:32:32

标签: php laravel api

我在一个项目中使用Laravel,在这个项目中,我必须使用特定的键名将数据发送到JSON编码数组中的API端点。

随着用户填写表单,我将数据持久化到名为Onboard的模型中,该模型存储了通过我使用的API加载客户端所需的数据,该表如下所示:

Schema::create('onboardings', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('user_id');
            $table->text('email_type')->nullable();
            $table->text('date_of_birth')->nullable();
            $table->text('country_of_birth')->nullable();
            $table->text('national_insurance_number')->nullable();
            $table->text('country_of_residency')->nullable();
            $table->text('tax_identification_number')->nullable();
            $table->boolean('politically_exposed_person')->nullable();
            $table->text('address_line_1')->nullable();
            $table->text('address_line_2')->nullable();
            $table->text('city')->nullable();
            $table->text('county')->nullable();
            $table->text('postcode')->nullable();
            $table->text('country')->nullable();
            $table->text('telephone_type')->nullable();
            $table->text('dialing_code')->nullable();
            $table->text('telephone_number')->nullable();
            $table->text('account_name')->nullable();
            $table->text('account_number')->nullable();
            $table->text('sort_code')->nullable();
            $table->text('investment_type')->nullable();
            $table->text('investment_amount')->nullable();
            $table->boolean('independent_financial_advisor')->nullable();
            $table->boolean('understand_objective')->nullable();
            $table->boolean('confirm_objective')->nullable();
            $table->boolean('understand_term_held')->nullable();
            $table->boolean('tax_relief')->nullable();
        });

因此,用户填写一个表格,并且此表中填充了数据。

API的提供者给了我一个示例类,用于封装所有必要的数据,如下所示。

<?php
class OnboardingClientDetails {
    //[Required]
    public $Title = "Ms.";
    //[Required]
    public $Forenames = "Beatrice";
    //[Required]
    public $Surname = "Volk";
    //[Required] (Between 1 and 244)
    public $CountryOfBirth = 1;
    //[Required]
    public $EmailAddress = "BeatriceJVolk@dayrep.com";
    //[Required]
    public $EmailType = "Work";
    //[Required]
    public $BirthDate;
    public $Suffix = "";
    public $NationalInsuranceNumber = "NITESTValue";
    //[Required]
    public $PrimaryAddress;
    public $AdditionalAddresses;
    //[Required]
    public $PrimaryTelephone;
    public $AdditionalTelephone;
    //[Required]
    public $BankAccount;
    //[Required]
    public $PrimaryCitizenship;
    public $AdditionalCitizenship;
    //[Required]
    public $ExternalCustomerId = "12345";
    //[Required]
    public $ExternalPlanId = "P4541";
    public $PlanType = 10;
    public $MiddleNames = "W";
    public $PlaceOfBirth = "London";
    public $OnlineValuation = true;
    public $PrincipalNationality;
    public $AdditionalNationality;
    function __construct()
    {
        $this->BirthDate = "/Date(". strtotime("10 September 1977") . ")/";
        $this->PrimaryAddress = new Address();
        $this->AdditionalAddresses = array(new Address(),new Address());
        $this->PrimaryTelephone = new OnboardingTelephoneNumber();
        $this->AdditionalTelephone = array(new OnboardingTelephoneNumber(),new OnboardingTelephoneNumber());
        $this->BankAccount = new OnboardingBankAccount();
        $this->PrimaryCitizenship = new OnboardingCitizenship();
        $this->AdditionalCitizenship = array (new OnboardingCitizenship(),new OnboardingCitizenship());
        $this->PrincipalNationality = new OnboardingPrincipalNationality();
        $this->AdditionalNationality = new OnboardingPrincipalNationality();
    }
}
class Address {
    //[Required]
    public $Address1 = "17 Moorgate";
    public $Address2 = "";
    //[Required]
    public $City = "London";
    public $County  = "";
    //[Required]
    public $Postcode = "ec2r 6ar";
    //[Required]
    //Between 1 and  244
    public $Country = 4;
    //[Required]
    public $AddressType = 1;
}
class OnboardingCitizenship
{
    public $CountryOfResidency = 1;
    public $TaxIdentificationNumber ="445010101";
}
class OnboardingTelephoneNumber {
    //[Required]
    public $Number = "5048491752";
    //[Required]
    public $DialingCode = "44";
    //[Required]
    public $TelephoneType = "1";
}
class OnboardingBankAccount {
    //[Required]
    public $AccountName = "Beatrice Volk";
    //[Required]
    public $AccountNumber = "7659708042";
    //[Required]
    public $SortCode = "010101";
}
class OnboardingPrincipalNationality {
    public $CountryId = 1;
    public $PrincipalNCI = "P434523";
    public $PrincipalNationalityType = "Primary";
}
?>

为了匹配此格式,我创建了一个数组,其中包含用户和入职表中存储的内容的组合:

$userData = auth()->user()->first();

// dd($userData);

$submissionData = array(
"Title" => $userData->title,
"Forenames" => $userData->first_name,
"Surname" => $userData->last_name,
"CountryOfBirth" => $userData->onboarding->country_of_birth,
"EmailAddress" => $userData->email,
"EmailType" => $userData->onboarding->email_type,
"BirthDate" => "/Date(". strtotime($userData->onboarding->date_of_birth) . ")/",
"Suffix" => null,
"PrimaryAddress" => array(
"Address1" => $userData->onboarding->address_line_1,
"Address2" => $userData->onboarding->address_line_2,
"City" => $userData->onboarding->city,
"County" => $userData->onboarding->county,
"Postcode" => $userData->onboarding->postcode,
"Country" => $userData->onboarding->country
),
"AdditionalAddresses" => array(
array(
"Address1" => null,
"Address2" => null,
"City" => null,
"County" => null,
"Postcode" => null,
"Country" => 0,
"AddressType" => 0
)
),
"PrimaryTelephone" => array(
"Number" => $userData->onboarding->telephone_number,
"DialingCode" => $userData->onboarding->dialing_code,
"TelephoneType" => $userData->onboarding->telephone_type
),
"AdditionalTelephone" => array(
array(
"Number" => "123456789",
"DialingCode" => 1,
"TelephoneType" => 1
)
),
"BankAccount" => array(
"AccountName" => $userData->onboarding->account_name,
"AccountNumber" => $userData->onboarding->account_number,
"SortCode" => $userData->onboarding->sort_code
),
"PrimaryCitizenship" => array(
"CountryOfResidency" => $userData->onboarding->country_of_residency,
"TaxIdentificationNumber" => $userData->onboarding->tax_identification_number
),
"AdditionalCitizenship" => array(
array(
"CountryOfResidency" => 0,
"TaxIdentificationNumber" => null
)
),
"ExternalCustomerId" => rand(100, 10000),
"ExternalPlanId" => "151",
"PlanType" => 10

但是,这感觉像是一种非常不干净的处理方式,那么,处理仅在Laravel中保存数据的PHP类的最佳方法是什么?

0 个答案:

没有答案