From 14293eacefec868b889632f25b5f0acbf1e9e0b4 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 11:53:00 +0930 Subject: [PATCH 1/6] Implement a simpler way to do resource models with class based properties. Add authentication, addresses, attributes, and form fields to Customer, fixes #67 --- .../ResourceModels/Customer/Customer.php | 27 ++++++++++++++ .../Customer/CustomerAttributeValue.php | 4 +-- .../Customer/CustomerAuthentication.php | 13 +++++++ .../ResourceModels/ResourceModel.php | 35 +++++++++++++++++-- .../Api/Customers/CustomersApiTest.php | 9 ++++- .../responses/customers__get_all.json | 23 +++++++++++- 6 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php diff --git a/src/BigCommerce/ResourceModels/Customer/Customer.php b/src/BigCommerce/ResourceModels/Customer/Customer.php index 48a6c1a5..c847cbfc 100644 --- a/src/BigCommerce/ResourceModels/Customer/Customer.php +++ b/src/BigCommerce/ResourceModels/Customer/Customer.php @@ -3,6 +3,7 @@ namespace BigCommerce\ApiV3\ResourceModels\Customer; use BigCommerce\ApiV3\ResourceModels\ResourceModel; +use stdClass; /** * @package BigCommerce\ApiV3\ResourceModels @@ -25,4 +26,30 @@ class Customer extends ResourceModel public string $country; public string $country_iso2; public string $phone; + + /** + * @var CustomerAddress[] + */ + public array $addresses; + + /** + * @var CustomerAttributeValue[] + */ + public array $attributes; + + /** + * @var CustomerFormFieldValue[] + */ + public array $form_fields; + + public ?CustomerAuthentication $authentication; + + + protected function beforeBuildObject(): void + { + $this->buildObjectArray('addresses', CustomerAddress::class); + $this->buildObjectArray('attributes', CustomerAttributeValue::class); + $this->buildObjectArray('form_fields', CustomerFormFieldValue::class); + $this->buildPropertyObject('authentication', CustomerAuthentication::class); + } } diff --git a/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php b/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php index a6d4fdf1..448d4f26 100644 --- a/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php +++ b/src/BigCommerce/ResourceModels/Customer/CustomerAttributeValue.php @@ -8,6 +8,6 @@ class CustomerAttributeValue extends ResourceModel { public int $attribute_id; public string $value; - public int $customer_id; - public int $id; + public ?int $customer_id; + public ?int $id; } diff --git a/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php new file mode 100644 index 00000000..d53ad47a --- /dev/null +++ b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php @@ -0,0 +1,13 @@ + $value) { + $this->optionObject = $optionObject; + + $this->beforeBuildObject(); + + if (!is_null($this->optionObject)) { + foreach ($this->optionObject as $key => $value) { $this->$key = $value; } } @@ -27,4 +33,29 @@ public function jsonSerialize(): array return $data; } + + protected function buildObjectArray(string $property, string $className): void + { + if (!is_null($this->optionObject) && isset($this->optionObject->$property)) { + $this->$property = array_map(function ($m) use ($className) { + return new $className($m); + }, $this->optionObject->$property); + + unset($this->optionObject->$property); + } + } + + protected function buildPropertyObject(string $property, string $className): void + { + if (!is_null($this->optionObject) && isset($this->optionObject->$property)) { + $this->$property = new $className($this->optionObject->$property); + + unset($this->optionObject->$property); + } + } + + /** + * Override this function to implement custom build functionality + */ + protected function beforeBuildObject(): void {} } diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index c22ebb04..ef5375f0 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -3,6 +3,8 @@ namespace BigCommerce\Tests\Api\Customers; use BigCommerce\ApiV3\ResourceModels\Customer\Customer; +use BigCommerce\ApiV3\ResourceModels\Customer\CustomerAddress; +use BigCommerce\ApiV3\ResourceModels\Customer\CustomerAttributeValue; use BigCommerce\Tests\BigCommerceApiTest; class CustomersApiTest extends BigCommerceApiTest @@ -11,8 +13,13 @@ public function testCanGetCustomers() { $this->setReturnData('customers__get_all.json'); $customersResponse = $this->getApi()->customers()->getAll(); + $customers = $customersResponse->getCustomers(); $this->assertEquals(1, $customersResponse->getPagination()->total); - $this->assertEquals('John', $customersResponse->getCustomers()[0]->first_name); + $this->assertEquals('John', $customers[0]->first_name); + $this->assertInstanceOf(CustomerAddress::class, $customers[0]->addresses[0]); + $this->assertInstanceOf(CustomerAttributeValue::class, $customers[0]->attributes[0]); + $this->assertEquals('California', $customers[0]->addresses[0]->state_or_province); + } public function testCanGetCustomerByEmail() diff --git a/tests/BigCommerce/responses/customers__get_all.json b/tests/BigCommerce/responses/customers__get_all.json index 286d6661..d0629ac7 100644 --- a/tests/BigCommerce/responses/customers__get_all.json +++ b/tests/BigCommerce/responses/customers__get_all.json @@ -17,7 +17,28 @@ "date_created": "2020-08-27T06:18:07Z", "date_modified": "2020-08-28T06:03:38Z", "accepts_product_review_abandoned_cart_emails": true, - "channel_ids": null + "channel_ids": null, + "addresses": [ + { + "address1": "Addr 1", + "address2": "", + "address_type": "residential", + "city": "San Francisco", + "company": "History", + "country_code": "US", + "first_name": "Ronald", + "last_name": "Swimmer", + "phone": "707070707", + "postal_code": "33333", + "state_or_province": "California" + } + ], + "attributes": [ + { + "attribute_id": 0, + "value": "string" + } + ] } ], "meta": { From 72d8949c3789add432e39ec7a73904a68c7f5c9c Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 11:54:05 +0930 Subject: [PATCH 2/6] Update release notes --- RELEASE_NOTES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 1a211d58..b8c453bc 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,2 @@ ### New Features - -### Fixes + - Add the ability to specifu attributes, addresses, form_fields, and authentication when creating customers (#67) \ No newline at end of file From 293e479fd1887a34efd262d4d540e37bf18fcf45 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 11:56:26 +0930 Subject: [PATCH 3/6] Fix code style errors --- .../ResourceModels/Customer/CustomerAuthentication.php | 2 -- src/BigCommerce/ResourceModels/ResourceModel.php | 4 +++- tests/BigCommerce/Api/Customers/CustomersApiTest.php | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php index d53ad47a..beb48961 100644 --- a/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php +++ b/src/BigCommerce/ResourceModels/Customer/CustomerAuthentication.php @@ -1,9 +1,7 @@ assertInstanceOf(CustomerAddress::class, $customers[0]->addresses[0]); $this->assertInstanceOf(CustomerAttributeValue::class, $customers[0]->attributes[0]); $this->assertEquals('California', $customers[0]->addresses[0]->state_or_province); - } public function testCanGetCustomerByEmail() From 2c975aca2565529e8d8f155a77c0ab14ccca5e2e Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 12:54:46 +0930 Subject: [PATCH 4/6] Add a difference class for customer attribute values, because the customers endpoint uses different property names to customers/attributes --- .../ResourceModels/Customer/AttributeValue.php | 14 ++++++++++++++ .../ResourceModels/Customer/Customer.php | 5 ++--- .../BigCommerce/Api/Customers/CustomersApiTest.php | 4 ++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/BigCommerce/ResourceModels/Customer/AttributeValue.php diff --git a/src/BigCommerce/ResourceModels/Customer/AttributeValue.php b/src/BigCommerce/ResourceModels/Customer/AttributeValue.php new file mode 100644 index 00000000..bcb5d863 --- /dev/null +++ b/src/BigCommerce/ResourceModels/Customer/AttributeValue.php @@ -0,0 +1,14 @@ +buildObjectArray('addresses', CustomerAddress::class); - $this->buildObjectArray('attributes', CustomerAttributeValue::class); + $this->buildObjectArray('attributes', AttributeValue::class); $this->buildObjectArray('form_fields', CustomerFormFieldValue::class); $this->buildPropertyObject('authentication', CustomerAuthentication::class); } diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index ced8c604..d9adb79a 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -2,9 +2,9 @@ namespace BigCommerce\Tests\Api\Customers; +use BigCommerce\ApiV3\ResourceModels\Customer\AttributeValue; use BigCommerce\ApiV3\ResourceModels\Customer\Customer; use BigCommerce\ApiV3\ResourceModels\Customer\CustomerAddress; -use BigCommerce\ApiV3\ResourceModels\Customer\CustomerAttributeValue; use BigCommerce\Tests\BigCommerceApiTest; class CustomersApiTest extends BigCommerceApiTest @@ -17,7 +17,7 @@ public function testCanGetCustomers() $this->assertEquals(1, $customersResponse->getPagination()->total); $this->assertEquals('John', $customers[0]->first_name); $this->assertInstanceOf(CustomerAddress::class, $customers[0]->addresses[0]); - $this->assertInstanceOf(CustomerAttributeValue::class, $customers[0]->attributes[0]); + $this->assertInstanceOf(AttributeValue::class, $customers[0]->attributes[0]); $this->assertEquals('California', $customers[0]->addresses[0]->state_or_province); } From 03a08ab9ac1f20c7959605ddaf39a85a4e6234f7 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 13:17:05 +0930 Subject: [PATCH 5/6] Add missing field customer_group_id to Customer --- src/BigCommerce/ResourceModels/Customer/Customer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BigCommerce/ResourceModels/Customer/Customer.php b/src/BigCommerce/ResourceModels/Customer/Customer.php index e0f5cdae..85843687 100644 --- a/src/BigCommerce/ResourceModels/Customer/Customer.php +++ b/src/BigCommerce/ResourceModels/Customer/Customer.php @@ -25,6 +25,7 @@ class Customer extends ResourceModel public string $country; public string $country_iso2; public string $phone; + public int $customer_group_id; /** * @var CustomerAddress[] From 4e84fd8967a358030131f19d73cc306944eeb0ea Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 10 May 2021 13:34:08 +0930 Subject: [PATCH 6/6] Add many missing fields to Customer --- RELEASE_NOTES.md | 9 ++++++++- src/BigCommerce/ResourceModels/Customer/Customer.php | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b8c453bc..373e04f5 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,2 +1,9 @@ + ### New Features - - Add the ability to specifu attributes, addresses, form_fields, and authentication when creating customers (#67) \ No newline at end of file + + - Add the ability to specify attributes, addresses, form_fields, and authentication when creating customers (#67) + +### Fixes + + - Add missing Customer properties (`notes`, `tax_exempt_category`, `accepts_product_review_abandoned_cart_emails`, + `store_credit_amounts`, `origin_channel_id`, and `channel_ids`) \ No newline at end of file diff --git a/src/BigCommerce/ResourceModels/Customer/Customer.php b/src/BigCommerce/ResourceModels/Customer/Customer.php index 85843687..5c5d1a84 100644 --- a/src/BigCommerce/ResourceModels/Customer/Customer.php +++ b/src/BigCommerce/ResourceModels/Customer/Customer.php @@ -26,6 +26,12 @@ class Customer extends ResourceModel public string $country_iso2; public string $phone; public int $customer_group_id; + public string $notes; + public string $tax_exempt_category; + public bool $accepts_product_review_abandoned_cart_emails; + public ?array $store_credit_amounts; + public ?int $origin_channel_id; + public ?array $channel_ids; /** * @var CustomerAddress[]