Skip to content

Commit

Permalink
Clear address object if all properties are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-puslecki committed Aug 17, 2016
1 parent 9d25110 commit 3d66f57
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
17 changes: 15 additions & 2 deletions MangoPay/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* Class represents an address.
*/
class Address extends Libraries\Dto
{

{
/**
* Address line 1.
* @var string
Expand Down Expand Up @@ -43,4 +42,18 @@ class Address extends Libraries\Dto
* @var string
*/
public $Country;

public function CanBeNull()
{
return $this->IsEmpty($this->AddressLine1)
&& $this->IsEmpty($this->AddressLine2)
&& $this->IsEmpty($this->City)
&& $this->IsEmpty($this->Region)
&& $this->IsEmpty($this->PostalCode)
&& $this->IsEmpty($this->Country);
}

private function IsEmpty($value){
return is_null($value) || empty($value);
}
}
17 changes: 16 additions & 1 deletion MangoPay/ApiUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,23 @@ public function Update($user)
$className = get_class($user);
if ($className == 'MangoPay\UserNatural') {
$methodKey = 'users_savenaturals';
if (!is_null($user->Address)
&& is_a($user->Address, "MangoPay\Address")
&& $user->Address->CanBeNull()) {
$user->Address = null;
}
} elseif ($className == 'MangoPay\UserLegal') {
$methodKey = 'users_savelegals';
if (!is_null($user->HeadquartersAddress)
&& is_a($user->HeadquartersAddress, "MangoPay\Address")
&& $user->HeadquartersAddress->CanBeNull()) {
$user->HeadquartersAddress = null;
}
if (!is_null($user->LegalRepresentativeAddress)
&& is_a($user->LegalRepresentativeAddress, "MangoPay\Address")
&& $user->LegalRepresentativeAddress->CanBeNull()) {
$user->LegalRepresentativeAddress = null;
}
} else {
throw new Libraries\Exception('Wrong entity class for user');
}
Expand Down Expand Up @@ -148,7 +163,7 @@ public function GetBankAccount($userId, $bankAccountId)
* @return \MangoPay\BankAccount Entity of bank account object
*/
public function UpdateBankAccount($userId, $bankAccount)
{
{
return $this->SaveObject('bankaccounts_save', $bankAccount, '\MangoPay\BankAccount', $userId);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/cases/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ function test_Users_Save_Natural() {
$this->assertIdenticalInputProps($userSaved, $john);
$this->assertIdenticalInputProps($userFetched, $john);
}

function test_Users_Save_NaturalAndClearAddresIfNeeded() {
$user = new \MangoPay\UserNatural();
$user->FirstName = "John";
$user->LastName = "Doe";
$user->Email = "[email protected]";
$user->Birthday = mktime(0, 0, 0, 12, 21, 1975);
$user->Nationality = "FR";
$user->CountryOfResidence = "FR";
$newUser = $this->_api->Users->Create($user);

$userSaved = $this->_api->Users->Update($newUser);

$this->assertTrue($userSaved->Id > 0);
}

function test_Users_Save_Legal() {
$matrix = $this->getMatrix();
Expand All @@ -121,6 +136,23 @@ function test_Users_Save_Legal() {
$this->assertIdenticalInputProps($userFetched, $matrix);
}

function test_Users_Save_LegalAndClearAddresIfNeeded() {
$user = new \MangoPay\UserLegal();
$user->Name = "MartixSampleOrg";
$user->Email = "[email protected]";
$user->LegalPersonType = \MangoPay\LegalPersonType::Business;
$user->LegalRepresentativeFirstName = "FirstName";
$user->LegalRepresentativeLastName = "LastName";
$user->LegalRepresentativeBirthday = mktime(0, 0, 0, 12, 21, 1975);
$user->LegalRepresentativeNationality = "FR";
$user->LegalRepresentativeCountryOfResidence = "FR";
$newUser = $this->_api->Users->Create($user);

$userSaved = $this->_api->Users->Update($newUser);

$this->assertTrue($userSaved->Id > 0);
}

function test_Users_CreateBankAccount_IBAN() {
$john = $this->getJohn();
$account = $this->getJohnsAccount();
Expand Down

0 comments on commit 3d66f57

Please sign in to comment.