From c2b16762a26358006eab6f4247cf9c9cf2d7895c Mon Sep 17 00:00:00 2001 From: Aarjan Langereis Date: Sat, 27 Apr 2019 12:45:32 +0200 Subject: [PATCH 1/4] Allow an Organisation to have multiple ContactPoints --- src/ContextTypes/Organization.php | 22 ++++++++ .../Organization2ContactsTest.php | 52 +++++++++++++++++++ tests/ContextTypes/OrganizationTest.php | 15 ++++++ 3 files changed, 89 insertions(+) create mode 100644 tests/ContextTypes/Organization2ContactsTest.php diff --git a/src/ContextTypes/Organization.php b/src/ContextTypes/Organization.php index 6738b01..4f7e459 100644 --- a/src/ContextTypes/Organization.php +++ b/src/ContextTypes/Organization.php @@ -26,4 +26,26 @@ public function __construct(array $attributes, array $extendedStructure = []) parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)); } + /** + * Set the contactPoints + * + * @param array $items + * @return array + */ + protected function setContactPointAttribute($items) + { + if (is_array($items) === false) { + return $items; + } + + //Check if it is an array with one dimension + if (is_array(reset($items)) === false) { + return $this->getNestedContext(ContactPoint::class, $items); + } + + //Process multi dimensional array + return array_map(function ($item) { + return $this->getNestedContext(ContactPoint::class, $item); + }, $items); + } } diff --git a/tests/ContextTypes/Organization2ContactsTest.php b/tests/ContextTypes/Organization2ContactsTest.php new file mode 100644 index 0000000..d9bcae4 --- /dev/null +++ b/tests/ContextTypes/Organization2ContactsTest.php @@ -0,0 +1,52 @@ + 'Said Organization', + 'url' => 'https://google.com/organization/22', + 'address' => [ + 'streetAddress' => '112 Apple St.', + 'addressLocality' => 'Hamden', + 'addressRegion' => 'CT', + 'postalCode' => '06514', + ], + 'logo' => 'https://google.com/thumbnail1.jpg', + 'contactPoint' => [ + ['@type' => 'contactPoint', + 'telephone' => '18008888888', + 'contactType' => 'customer service', + ], + ['@type' => 'contactPoint', + 'telephone' => '18009999999', + 'contactType' => 'sales', + ], + ], + ]; + + /** + * @test + */ + public function shouldHave2ContactsArray() + { + $context = $this->make(); + + $this->assertEquals([ + '@type' => 'ContactPoint', + 'telephone' => '18008888888', + 'contactType' => 'customer service', + ], $context->getProperty('contactPoint')[0]); + $this->assertEquals([ + '@type' => 'ContactPoint', + 'telephone' => '18009999999', + 'contactType' => 'sales', + ], $context->getProperty('contactPoint')[1]); + } + +} diff --git a/tests/ContextTypes/OrganizationTest.php b/tests/ContextTypes/OrganizationTest.php index 2539e89..3ba2716 100644 --- a/tests/ContextTypes/OrganizationTest.php +++ b/tests/ContextTypes/OrganizationTest.php @@ -38,4 +38,19 @@ public function shouldHaveContactPointObject() ], $context->getProperty('contactPoint')); } + /** + * @test + */ + public function shouldHaveAddressArray() + { + $context = $this->make(); + + $this->assertEquals([ + '@type' => 'PostalAddress', + 'streetAddress' => '112 Apple St.', + 'addressLocality' => 'Hamden', + 'addressRegion' => 'CT', + 'postalCode' => '06514', + ], $context->getProperty('address')); + } } From b5afc05898285babc6a4a4a6aacdfc2ece223840 Mon Sep 17 00:00:00 2001 From: Daniel Stainback Date: Wed, 8 May 2019 11:47:09 -0400 Subject: [PATCH 2/4] Fix indenting --- src/ContextTypes/Organization.php | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/ContextTypes/Organization.php b/src/ContextTypes/Organization.php index 4f7e459..b23152e 100644 --- a/src/ContextTypes/Organization.php +++ b/src/ContextTypes/Organization.php @@ -26,26 +26,26 @@ public function __construct(array $attributes, array $extendedStructure = []) parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure)); } - /** - * Set the contactPoints - * - * @param array $items - * @return array - */ - protected function setContactPointAttribute($items) - { - if (is_array($items) === false) { - return $items; - } + /** + * Set the contactPoints + * + * @param array $items + * @return array + */ + protected function setContactPointAttribute($items) + { + if (is_array($items) === false) { + return $items; + } - //Check if it is an array with one dimension - if (is_array(reset($items)) === false) { - return $this->getNestedContext(ContactPoint::class, $items); - } + //Check if it is an array with one dimension + if (is_array(reset($items)) === false) { + return $this->getNestedContext(ContactPoint::class, $items); + } - //Process multi dimensional array - return array_map(function ($item) { - return $this->getNestedContext(ContactPoint::class, $item); - }, $items); - } + //Process multi dimensional array + return array_map(function ($item) { + return $this->getNestedContext(ContactPoint::class, $item); + }, $items); + } } From 381fde5d3e1da77897f9e10eab483a72ac6f8e89 Mon Sep 17 00:00:00 2001 From: Daniel Stainback Date: Wed, 8 May 2019 11:47:39 -0400 Subject: [PATCH 3/4] Update Organization2ContactsTest.php --- .../Organization2ContactsTest.php | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/ContextTypes/Organization2ContactsTest.php b/tests/ContextTypes/Organization2ContactsTest.php index d9bcae4..fd3d2a9 100644 --- a/tests/ContextTypes/Organization2ContactsTest.php +++ b/tests/ContextTypes/Organization2ContactsTest.php @@ -19,14 +19,14 @@ class Organization2ContactsTest extends TestCase ], 'logo' => 'https://google.com/thumbnail1.jpg', 'contactPoint' => [ - ['@type' => 'contactPoint', + ['@type' => 'contactPoint', 'telephone' => '18008888888', 'contactType' => 'customer service', - ], - ['@type' => 'contactPoint', + ], + ['@type' => 'contactPoint', 'telephone' => '18009999999', 'contactType' => 'sales', - ], + ], ], ]; @@ -37,16 +37,15 @@ public function shouldHave2ContactsArray() { $context = $this->make(); - $this->assertEquals([ - '@type' => 'ContactPoint', - 'telephone' => '18008888888', - 'contactType' => 'customer service', - ], $context->getProperty('contactPoint')[0]); - $this->assertEquals([ - '@type' => 'ContactPoint', - 'telephone' => '18009999999', - 'contactType' => 'sales', - ], $context->getProperty('contactPoint')[1]); + $this->assertEquals([ + '@type' => 'ContactPoint', + 'telephone' => '18008888888', + 'contactType' => 'customer service', + ], $context->getProperty('contactPoint')[0]); + $this->assertEquals([ + '@type' => 'ContactPoint', + 'telephone' => '18009999999', + 'contactType' => 'sales', + ], $context->getProperty('contactPoint')[1]); } - } From 78aa71101667ca256e0eedb4dcd8d8ab5849f355 Mon Sep 17 00:00:00 2001 From: Daniel Stainback Date: Wed, 8 May 2019 11:48:03 -0400 Subject: [PATCH 4/4] Fix indenting --- tests/ContextTypes/OrganizationTest.php | 28 ++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/ContextTypes/OrganizationTest.php b/tests/ContextTypes/OrganizationTest.php index 3ba2716..09d4e07 100644 --- a/tests/ContextTypes/OrganizationTest.php +++ b/tests/ContextTypes/OrganizationTest.php @@ -38,19 +38,19 @@ public function shouldHaveContactPointObject() ], $context->getProperty('contactPoint')); } - /** - * @test - */ - public function shouldHaveAddressArray() - { - $context = $this->make(); - - $this->assertEquals([ - '@type' => 'PostalAddress', - 'streetAddress' => '112 Apple St.', - 'addressLocality' => 'Hamden', - 'addressRegion' => 'CT', - 'postalCode' => '06514', - ], $context->getProperty('address')); + /** + * @test + */ + public function shouldHaveAddressArray() + { + $context = $this->make(); + + $this->assertEquals([ + '@type' => 'PostalAddress', + 'streetAddress' => '112 Apple St.', + 'addressLocality' => 'Hamden', + 'addressRegion' => 'CT', + 'postalCode' => '06514', + ], $context->getProperty('address')); } }