Skip to content

Commit

Permalink
Added toObject method
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdgeijn committed Feb 26, 2022
1 parent f264db7 commit ce24940
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Requests/CompanyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function get(string $companyId ): ?Company
*/
public function create(Company $company): ?Company
{
$response = $this->getRequest('/v1/companies', $company->createCompany() );
$response = $this->getRequest('/v1/companies', $company->toObject() );

if ($response->getStatusCode() == 200)
return Company::parse(json_decode( $response->getBody() ) );
Expand Down
13 changes: 13 additions & 0 deletions src/Responses/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ public static function createFromBody(string $body ): PaginatedCollection
return $collection;
}

/**
* @return \stdClass
*/
public function toObject(): \stdClass
{
$object = new \stdClass;

foreach( $this as $key => $value )
$object->{$key} = $value;

return $object;
}

/**
* @param object $item
* @return AbstractResponse
Expand Down
9 changes: 9 additions & 0 deletions src/Responses/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function createCompany( ): array
];
}

public function toObject(): \stdClass
{
$array = $this->createCompany();

$array['address'] = (object)$array['address'];

return (object)$array;
}

public function setAddress( $address )
{
$this->setStreet( $address->street );
Expand Down
5 changes: 5 additions & 0 deletions src/Responses/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function createContact()
];
}

public function toObject(): \stdClass
{
return (object)$this->createContact();
}

/**
* @param mixed $firstName
* @return Contact
Expand Down

0 comments on commit ce24940

Please sign in to comment.