Skip to content

Commit 0dc6efc

Browse files
authored
Merge pull request #51 from madman-81/organistaion_multiple_contacts
Allow an Organisation to have multiple ContactPoints
2 parents db39864 + 78aa711 commit 0dc6efc

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/ContextTypes/Organization.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,26 @@ public function __construct(array $attributes, array $extendedStructure = [])
2626
parent::__construct($attributes, array_merge($this->structure, $this->extendedStructure, $extendedStructure));
2727
}
2828

29+
/**
30+
* Set the contactPoints
31+
*
32+
* @param array $items
33+
* @return array
34+
*/
35+
protected function setContactPointAttribute($items)
36+
{
37+
if (is_array($items) === false) {
38+
return $items;
39+
}
40+
41+
//Check if it is an array with one dimension
42+
if (is_array(reset($items)) === false) {
43+
return $this->getNestedContext(ContactPoint::class, $items);
44+
}
45+
46+
//Process multi dimensional array
47+
return array_map(function ($item) {
48+
return $this->getNestedContext(ContactPoint::class, $item);
49+
}, $items);
50+
}
2951
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace JsonLd\Test\ContextTypes;
4+
5+
use JsonLd\Test\TestCase;
6+
7+
class Organization2ContactsTest extends TestCase
8+
{
9+
protected $class = \JsonLd\ContextTypes\Organization::class;
10+
11+
protected $attributes = [
12+
'name' => 'Said Organization',
13+
'url' => 'https://google.com/organization/22',
14+
'address' => [
15+
'streetAddress' => '112 Apple St.',
16+
'addressLocality' => 'Hamden',
17+
'addressRegion' => 'CT',
18+
'postalCode' => '06514',
19+
],
20+
'logo' => 'https://google.com/thumbnail1.jpg',
21+
'contactPoint' => [
22+
['@type' => 'contactPoint',
23+
'telephone' => '18008888888',
24+
'contactType' => 'customer service',
25+
],
26+
['@type' => 'contactPoint',
27+
'telephone' => '18009999999',
28+
'contactType' => 'sales',
29+
],
30+
],
31+
];
32+
33+
/**
34+
* @test
35+
*/
36+
public function shouldHave2ContactsArray()
37+
{
38+
$context = $this->make();
39+
40+
$this->assertEquals([
41+
'@type' => 'ContactPoint',
42+
'telephone' => '18008888888',
43+
'contactType' => 'customer service',
44+
], $context->getProperty('contactPoint')[0]);
45+
$this->assertEquals([
46+
'@type' => 'ContactPoint',
47+
'telephone' => '18009999999',
48+
'contactType' => 'sales',
49+
], $context->getProperty('contactPoint')[1]);
50+
}
51+
}

tests/ContextTypes/OrganizationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,19 @@ public function shouldHaveContactPointObject()
3838
], $context->getProperty('contactPoint'));
3939
}
4040

41+
/**
42+
* @test
43+
*/
44+
public function shouldHaveAddressArray()
45+
{
46+
$context = $this->make();
47+
48+
$this->assertEquals([
49+
'@type' => 'PostalAddress',
50+
'streetAddress' => '112 Apple St.',
51+
'addressLocality' => 'Hamden',
52+
'addressRegion' => 'CT',
53+
'postalCode' => '06514',
54+
], $context->getProperty('address'));
55+
}
4156
}

0 commit comments

Comments
 (0)