Skip to content

Commit 85304ce

Browse files
authored
Merge pull request #165 from elgentos/master
Added Language reference
2 parents 03aebd0 + 45fccc2 commit 85304ce

File tree

6 files changed

+173
-7
lines changed

6 files changed

+173
-7
lines changed

src/Prismic/ApiData.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,30 @@ class ApiData
5656
*/
5757
private $experiments;
5858

59+
/**
60+
* List of configured languages
61+
* @var array
62+
*/
63+
private $languages;
64+
5965
/**
6066
* A constructor to build the object when you've retrieved all the data you need.
6167
*
62-
* @param array $refs
63-
* @param array $bookmarks
64-
* @param array $types
65-
* @param array $tags
66-
* @param array $forms
68+
* @param array $refs
69+
* @param array $bookmarks
70+
* @param array $types
71+
* @param array $languages
72+
* @param array $tags
73+
* @param array $forms
6774
* @param Experiments $experiments
68-
* @param string $oauth_initiate
69-
* @param string $oauth_token
75+
* @param string $oauth_initiate
76+
* @param string $oauth_token
7077
*/
7178
private function __construct(
7279
array $refs,
7380
array $bookmarks,
7481
array $types,
82+
array $languages,
7583
array $tags,
7684
array $forms,
7785
Experiments $experiments,
@@ -81,6 +89,7 @@ private function __construct(
8189
$this->refs = $refs;
8290
$this->bookmarks = $bookmarks;
8391
$this->types = $types;
92+
$this->languages = $languages;
8493
$this->tags = $tags;
8594
$this->forms = $forms;
8695
$this->experiments = $experiments;
@@ -124,6 +133,12 @@ function ($ref) {
124133
),
125134
(array)$json->bookmarks,
126135
(array)$json->types,
136+
array_map(
137+
function ($language) {
138+
return Language::parse($language);
139+
},
140+
(array)$json->languages
141+
),
127142
$json->tags,
128143
(array)$json->forms,
129144
$experiments,
@@ -196,4 +211,9 @@ public function getOauthToken() : string
196211
{
197212
return $this->oauth_token;
198213
}
214+
215+
public function getLanguages() : array
216+
{
217+
return $this->languages;
218+
}
199219
}

src/Prismic/Language.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: jeroen
5+
* Date: 20-3-19
6+
* Time: 21:19
7+
*/
8+
9+
namespace Prismic;
10+
11+
class Language
12+
{
13+
14+
/**
15+
* Language id
16+
* @var string
17+
*/
18+
private $id;
19+
20+
/**
21+
* Language name
22+
* @var string
23+
*/
24+
private $name;
25+
26+
/**
27+
* Language constructor.
28+
*
29+
* @param string $id
30+
* @param string $name
31+
*/
32+
private function __construct(
33+
string $id,
34+
string $name
35+
) {
36+
$this->id = $id;
37+
$this->name = $name;
38+
}
39+
40+
41+
/**
42+
*
43+
* @return string
44+
*/
45+
public function getId(): string
46+
{
47+
return $this->id;
48+
}
49+
50+
/**
51+
*
52+
* @return string
53+
*/
54+
public function getName(): string
55+
{
56+
return $this->name;
57+
}
58+
59+
/**
60+
*
61+
* @param \stdClass $json
62+
* @return Language
63+
*
64+
* @throws Exception\InvalidArgumentException
65+
*/
66+
public static function parse(\stdClass $json): self
67+
{
68+
if (! isset($json->id, $json->name)) {
69+
throw new Exception\InvalidArgumentException(
70+
'The properties id, name should exist in the JSON object for a Language'
71+
);
72+
}
73+
return new self(
74+
$json->id,
75+
$json->name
76+
);
77+
}
78+
}

tests/Prismic/ApiDataTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
namespace Prismic\Test;
55

66
use Prismic\ApiData;
7+
use Prismic\Language;
78
use Prismic\Ref;
89
use Prismic\Experiments;
910
use stdClass;
1011

1112
class ApiDataTest extends TestCase
1213
{
1314

15+
/** @var ApiData */
1416
private $data;
1517

1618
public function setUp()
@@ -52,6 +54,9 @@ public function testApiDataHasExpectedValues()
5254
$this->assertCount(2, $this->data->getForms());
5355
$this->assertContainsOnlyInstancesOf(stdClass::class, $this->data->getForms());
5456

57+
$this->assertCount(4, $this->data->getLanguages());
58+
$this->assertContainsOnly(Language::class, $this->data->getLanguages());
59+
5560
$this->assertInstanceOf(Experiments::class, $this->data->getExperiments());
5661

5762
$this->assertSame('http://lesbonneschoses.prismic.io/auth', $this->data->getOauthInitiate());

tests/Prismic/LanguageTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: jeroen
5+
* Date: 20-3-19
6+
* Time: 21:24
7+
*/
8+
9+
namespace Prismic\Test;
10+
11+
use Prismic\Exception\InvalidArgumentException;
12+
use Prismic\Language;
13+
14+
class LanguageTest extends TestCase
15+
{
16+
17+
/** @var Language */
18+
private $language;
19+
/** @var \stdClass */
20+
private $json;
21+
22+
public function setUp()
23+
{
24+
$this->json = $json = \json_decode($this->getJsonFixture('language.json'));
25+
$this->language = Language::parse($json);
26+
}
27+
28+
public function testCorrectId()
29+
{
30+
$this->assertSame($this->json->id, $this->language->getId());
31+
$this->assertSame($this->json->name, $this->language->getName());
32+
}
33+
34+
/**
35+
* @expectedException InvalidArgumentException
36+
*/
37+
public function testWrongObjectType()
38+
{
39+
Language::parse(new \stdClass);
40+
}
41+
}

tests/fixtures/data.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030
"job-offer": "Job offer",
3131
"product": "Product"
3232
},
33+
"languages": [
34+
{
35+
"id": "en-us",
36+
"name": "English - United States"
37+
},
38+
{
39+
"id": "nl-nl",
40+
"name": "Dutch - Netherlands"
41+
},
42+
{
43+
"id": "es-es",
44+
"name": "Spanish - Spain (Traditional)"
45+
},
46+
{
47+
"id": "fr-fr",
48+
"name": "French - France"
49+
}
50+
],
3351
"tags": ["Cupcake", "Pie", "Featured", "Macaron"],
3452
"forms": {
3553
"everything": {

tests/fixtures/language.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "en-us",
3+
"name": "English - United States"
4+
}

0 commit comments

Comments
 (0)