Skip to content

Commit fbcdb41

Browse files
authored
Merge pull request #45 from boly38/feature/reviewsAuthors
multiple reviews and authors
2 parents a85d7f6 + 433d9d6 commit fbcdb41

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

src/ContextTypes/CreativeWork.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ protected function setTextAttribute($txt)
5656
return $this->truncate($txt, 260);
5757
}
5858

59+
/**
60+
* Set the authors
61+
*
62+
* @param array $items
63+
* @return array
64+
*/
65+
protected function setAuthorAttribute($items)
66+
{
67+
if (is_array($items) === false) {
68+
return $items;
69+
}
70+
71+
return array_map(function ($item) {
72+
return $this->getNestedContext(Person::class, $item);
73+
}, $items);
74+
}
75+
5976
/**
6077
* Set the comments
6178
*
@@ -72,4 +89,21 @@ protected function setCommentAttribute($items)
7289
return $this->getNestedContext(Comment::class, $item);
7390
}, $items);
7491
}
92+
93+
/**
94+
* Set the reviews
95+
*
96+
* @param array $items
97+
* @return array
98+
*/
99+
protected function setReviewAttribute($items)
100+
{
101+
if (is_array($items) === false) {
102+
return $items;
103+
}
104+
105+
return array_map(function ($item) {
106+
return $this->getNestedContext(Review::class, $item);
107+
}, $items);
108+
}
75109
}

tests/ContextTypes/ArticleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ArticleTest extends TestCase
2525
'thumbnailUrl' => 'https://google.com/thumbnail1.jpg',
2626
'text' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.',
2727
'review' => [
28+
'@type' => 'Review',
2829
'reviewRating' => 5,
2930
],
3031
'publisher' => [
@@ -53,6 +54,7 @@ class ArticleTest extends TestCase
5354
'dateModified' => '2013-10-04T00:00',
5455
'datePublished' => '2013-10-04T00:00',
5556
'author' => [
57+
'@type' => 'Person',
5658
'name' => 'Joe Joe',
5759
],
5860
'mainEntityOfPage' => [

tests/ContextTypes/CreativeWorkTest.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class CreativeWorkTest extends TestCase
1717
'ratingCount' => 4,
1818
],
1919
'author' => [
20-
'name' => 'Joe Joe',
20+
['@type' => 'Person',
21+
'name' => 'Joe Joe'],
22+
['@type' => 'Person',
23+
'name' => 'Jammy Joe'],
2124
],
2225
'creator' => [
2326
'name' => 'Joe Joe',
@@ -37,7 +40,8 @@ class CreativeWorkTest extends TestCase
3740
]
3841
],
3942
'review' => [
40-
'reviewRating' => 5,
43+
['@type' => 'Review', 'name' => 'first review', 'reviewRating' => 3],
44+
['@type' => 'Review', 'name' => 'second review', 'reviewRating' => 5],
4145
],
4246
'video' => [
4347
"url" => 'https://google.com/thumbnail1.mov',
@@ -66,16 +70,21 @@ public function shouldHaveAggregateRatingObject()
6670
/**
6771
* @test
6872
*/
69-
public function shouldHaveAuthorObject()
73+
public function shouldHave2AuthorsArray()
7074
{
7175
$context = $this->make();
7276

7377
$this->assertEquals([
7478
'@type' => 'Person',
7579
'name' => 'Joe Joe',
76-
], $context->getProperty('author'));
80+
], $context->getProperty('author')[0]);
81+
$this->assertEquals([
82+
'@type' => 'Person',
83+
'name' => 'Jammy Joe',
84+
], $context->getProperty('author')[1]);
7785
}
7886

87+
7988
/**
8089
* @test
8190
*/
@@ -126,14 +135,20 @@ public function shouldHavePublisherObject()
126135
/**
127136
* @test
128137
*/
129-
public function shouldHaveReviewObject()
138+
public function shouldHave2ReviewsArray()
130139
{
131140
$context = $this->make();
132141

133142
$this->assertEquals([
134143
'@type' => 'Review',
135-
'reviewRating' => 5,
136-
], $context->getProperty('review'));
144+
'name' => 'first review',
145+
'reviewRating' => 3
146+
], $context->getProperty('review')[0]);
147+
$this->assertEquals([
148+
'@type' => 'Review',
149+
'name' => 'second review',
150+
'reviewRating' => 5
151+
], $context->getProperty('review')[1]);
137152
}
138153

139154
/**

0 commit comments

Comments
 (0)