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

[PHP-70] Support for setting the user's address and date of birth #46

Merged
merged 11 commits into from
Feb 9, 2024
5 changes: 3 additions & 2 deletions config/bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
declare(strict_types=1);

use TrueLayer\Entities;
use TrueLayer\Entities\User;
use TrueLayer\Interfaces;

return [
Interfaces\UserInterface::class => User::class,
Interfaces\HppInterface::class => 'makeHpp',

Interfaces\AddressInterface::class => Entities\Address::class,
Interfaces\UserInterface::class => Entities\User::class,

Interfaces\Beneficiary\BeneficiaryBuilderInterface::class => Entities\Beneficiary\BeneficiaryBuilder::class,
Interfaces\Beneficiary\MerchantBeneficiaryInterface::class => Entities\Beneficiary\MerchantBeneficiary::class,
Interfaces\Beneficiary\ExternalAccountBeneficiaryInterface::class => Entities\Beneficiary\ExternalAccountBeneficiary::class,
Expand Down
184 changes: 184 additions & 0 deletions src/Entities/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

namespace TrueLayer\Entities;

use TrueLayer\Interfaces\AddressInterface;

class Address extends Entity implements AddressInterface
{
/**
* @var string
*/
protected string $addressLine1;

/**
* @var string
*/
protected string $addressLine2;

/**
* @var string
*/
protected string $city;

/**
* @var string
*/
protected string $state;

/**
* @var string
*/
protected string $zip;

/**
* @var string
*/
protected string $countryCode;

/**
* @var string[]
*/
protected array $arrayFields = [
'address_line1',
'address_line2',
'city',
'state',
'zip',
'country_code',
];

/**
* @var string[]
*/
protected array $rules = [
'address_line1' => 'string|required',
'address_line2' => 'string|nullable',
'city' => 'string|required',
'state' => 'string|nullable',
'zip' => 'string|required',
'country_code' => 'string|required',
];

/**
* @return string|null
*/
public function getAddressLine1(): ?string
{
return $this->addressLine1 ?? null;
}

/**
* @param string $addressLine1
*
* @return AddressInterface
*/
public function addressLine1(string $addressLine1): AddressInterface
{
$this->addressLine1 = $addressLine1;

return $this;
}

/**
* @return string|null
*/
public function getAddressLine2(): ?string
{
return $this->addressLine2 ?? null;
}

/**
* @param string $addressLine2
*
* @return AddressInterface
*/
public function addressLine2(string $addressLine2): AddressInterface
{
$this->addressLine2 = $addressLine2;

return $this;
}

/**
* @return string|null
*/
public function getCity(): ?string
{
return $this->city ?? null;
}

/**
* @param string $city
*
* @return AddressInterface
*/
public function city(string $city): AddressInterface
{
$this->city = $city;

return $this;
}

/**
* @return string|null
*/
public function getState(): ?string
{
return $this->state ?? null;
}

/**
* @param string $state
*
* @return AddressInterface
*/
public function state(string $state): AddressInterface
{
$this->state = $state;

return $this;
}

/**
* @return string|null
*/
public function getZip(): ?string
{
return $this->zip ?? null;
}

/**
* @param string $zip
*
* @return AddressInterface
*/
public function zip(string $zip): AddressInterface
{
$this->zip = $zip;

return $this;
}

/**
* @return string|null
*/
public function getCountryCode(): ?string
{
return $this->countryCode ?? null;
}

/**
* @param string $countryCode
*
* @return AddressInterface
*/
public function countryCode(string $countryCode): AddressInterface
{
$this->countryCode = $countryCode;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class Entity implements ArrayableInterface, HasAttributesInterface
/**
* @var EntityFactoryInterface
*/
private EntityFactoryInterface $entityFactory;
protected EntityFactoryInterface $entityFactory;

/**
* @param ValidatorFactory $validatorFactory
Expand Down
81 changes: 76 additions & 5 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace TrueLayer\Entities;

use TrueLayer\Exceptions\InvalidArgumentException;
use TrueLayer\Exceptions\ValidationException;
use TrueLayer\Interfaces\AddressInterface;
use TrueLayer\Interfaces\UserInterface;
use TrueLayer\Validation\ValidType;

final class User extends Entity implements UserInterface
{
Expand All @@ -28,6 +32,16 @@ final class User extends Entity implements UserInterface
*/
protected string $phone;

/**
* @var AddressInterface
*/
protected AddressInterface $address;

/**
* @var string
*/
protected string $dateOfBirth;

/**
* @var string[]
*/
Expand All @@ -36,18 +50,32 @@ final class User extends Entity implements UserInterface
'name',
'email',
'phone',
'address',
'date_of_birth',
];

/**
* @var string[]
*/
protected array $rules = [
'id' => 'string|nullable',
'name' => 'string|nullable|required_without:id',
'email' => 'string|nullable|email|required_without_all:phone,id',
'phone' => 'string|nullable|required_without_all:email,id',
protected array $casts = [
'address' => AddressInterface::class,
];

/**
* @return mixed[]
*/
protected function rules(): array
{
return [
'id' => 'string|nullable',
'name' => 'string|nullable|required_without:id',
'email' => 'string|nullable|email|required_without_all:phone,id',
'phone' => 'string|nullable|required_without_all:email,id',
'address' => ['nullable', ValidType::of(AddressInterface::class)],
'date_of_birth' => 'string|nullable|date',
];
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -127,4 +155,47 @@ public function phone(string $phone): UserInterface

return $this;
}

/**
* @return AddressInterface|null
*/
public function getAddress(): ?AddressInterface
{
return $this->address ?? null;
}

/**
* @param AddressInterface|null $address
*
* @throws ValidationException
* @throws InvalidArgumentException
*
* @return AddressInterface
*/
public function address(?AddressInterface $address = null): AddressInterface
{
$this->address = $address ?: $this->entityFactory->make(AddressInterface::class);

return $this->address;
}

/**
* @return string|null
*/
public function getDateOfBirth(): ?string
{
return $this->dateOfBirth ?? null;
}

/**
* @param string $dateOfBirth
*
* @return UserInterface
*/
public function dateOfBirth(string $dateOfBirth): UserInterface
{
$this->dateOfBirth = $dateOfBirth;

return $this;
}
}
Loading
Loading