Skip to content

Commit

Permalink
Merge pull request #51 from madman-81/organistaion_multiple_contacts
Browse files Browse the repository at this point in the history
Allow an Organisation to have multiple ContactPoints
  • Loading branch information
Torann authored May 8, 2019
2 parents db39864 + 78aa711 commit 0dc6efc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
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'));
}
}

0 comments on commit 0dc6efc

Please sign in to comment.