From ce249407944e291e37c636fb9c65e5d685302a7f Mon Sep 17 00:00:00 2001 From: bHosted Date: Sat, 26 Feb 2022 01:36:04 +0100 Subject: [PATCH] Added toObject method --- src/Requests/CompanyRequest.php | 2 +- src/Responses/AbstractResponse.php | 13 +++++++++++++ src/Responses/Company.php | 9 +++++++++ src/Responses/Contact.php | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Requests/CompanyRequest.php b/src/Requests/CompanyRequest.php index 71eced3..c792184 100644 --- a/src/Requests/CompanyRequest.php +++ b/src/Requests/CompanyRequest.php @@ -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() ) ); diff --git a/src/Responses/AbstractResponse.php b/src/Responses/AbstractResponse.php index a67b4bf..a138e8b 100644 --- a/src/Responses/AbstractResponse.php +++ b/src/Responses/AbstractResponse.php @@ -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 diff --git a/src/Responses/Company.php b/src/Responses/Company.php index 2916f6e..fd4cb67 100644 --- a/src/Responses/Company.php +++ b/src/Responses/Company.php @@ -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 ); diff --git a/src/Responses/Contact.php b/src/Responses/Contact.php index 2dec67e..f55ea55 100644 --- a/src/Responses/Contact.php +++ b/src/Responses/Contact.php @@ -39,6 +39,11 @@ public function createContact() ]; } + public function toObject(): \stdClass + { + return (object)$this->createContact(); + } + /** * @param mixed $firstName * @return Contact