diff --git a/src/ContextTypes/Organization.php b/src/ContextTypes/Organization.php index 6738b01..b23152e 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..fd3d2a9 --- /dev/null +++ b/tests/ContextTypes/Organization2ContactsTest.php @@ -0,0 +1,51 @@ + '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..09d4e07 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')); + } }