Skip to content

Allow an Organisation to have multiple ContactPoints #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/ContextTypes/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
51 changes: 51 additions & 0 deletions tests/ContextTypes/Organization2ContactsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace JsonLd\Test\ContextTypes;

use JsonLd\Test\TestCase;

class Organization2ContactsTest extends TestCase
{
protected $class = \JsonLd\ContextTypes\Organization::class;

protected $attributes = [
'name' => '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]);
}
}
15 changes: 15 additions & 0 deletions tests/ContextTypes/OrganizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}