Skip to content

Commit

Permalink
Merge pull request #165 from elgentos/master
Browse files Browse the repository at this point in the history
Added Language reference
  • Loading branch information
Raph22 committed May 21, 2019
2 parents 03aebd0 + 45fccc2 commit 85304ce
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Prismic/ApiData.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,30 @@ class ApiData
*/
private $experiments;

/**
* List of configured languages
* @var array
*/
private $languages;

/**
* A constructor to build the object when you've retrieved all the data you need.
*
* @param array $refs
* @param array $bookmarks
* @param array $types
* @param array $tags
* @param array $forms
* @param array $refs
* @param array $bookmarks
* @param array $types
* @param array $languages
* @param array $tags
* @param array $forms
* @param Experiments $experiments
* @param string $oauth_initiate
* @param string $oauth_token
* @param string $oauth_initiate
* @param string $oauth_token
*/
private function __construct(
array $refs,
array $bookmarks,
array $types,
array $languages,
array $tags,
array $forms,
Experiments $experiments,
Expand All @@ -81,6 +89,7 @@ private function __construct(
$this->refs = $refs;
$this->bookmarks = $bookmarks;
$this->types = $types;
$this->languages = $languages;
$this->tags = $tags;
$this->forms = $forms;
$this->experiments = $experiments;
Expand Down Expand Up @@ -124,6 +133,12 @@ function ($ref) {
),
(array)$json->bookmarks,
(array)$json->types,
array_map(
function ($language) {
return Language::parse($language);
},
(array)$json->languages
),
$json->tags,
(array)$json->forms,
$experiments,
Expand Down Expand Up @@ -196,4 +211,9 @@ public function getOauthToken() : string
{
return $this->oauth_token;
}

public function getLanguages() : array
{
return $this->languages;
}
}
78 changes: 78 additions & 0 deletions src/Prismic/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: jeroen
* Date: 20-3-19
* Time: 21:19
*/

namespace Prismic;

class Language
{

/**
* Language id
* @var string
*/
private $id;

/**
* Language name
* @var string
*/
private $name;

/**
* Language constructor.
*
* @param string $id
* @param string $name
*/
private function __construct(
string $id,
string $name
) {
$this->id = $id;
$this->name = $name;
}


/**
*
* @return string
*/
public function getId(): string
{
return $this->id;
}

/**
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
*
* @param \stdClass $json
* @return Language
*
* @throws Exception\InvalidArgumentException
*/
public static function parse(\stdClass $json): self
{
if (! isset($json->id, $json->name)) {
throw new Exception\InvalidArgumentException(
'The properties id, name should exist in the JSON object for a Language'
);
}
return new self(
$json->id,
$json->name
);
}
}
5 changes: 5 additions & 0 deletions tests/Prismic/ApiDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace Prismic\Test;

use Prismic\ApiData;
use Prismic\Language;
use Prismic\Ref;
use Prismic\Experiments;
use stdClass;

class ApiDataTest extends TestCase
{

/** @var ApiData */
private $data;

public function setUp()
Expand Down Expand Up @@ -52,6 +54,9 @@ public function testApiDataHasExpectedValues()
$this->assertCount(2, $this->data->getForms());
$this->assertContainsOnlyInstancesOf(stdClass::class, $this->data->getForms());

$this->assertCount(4, $this->data->getLanguages());
$this->assertContainsOnly(Language::class, $this->data->getLanguages());

$this->assertInstanceOf(Experiments::class, $this->data->getExperiments());

$this->assertSame('http://lesbonneschoses.prismic.io/auth', $this->data->getOauthInitiate());
Expand Down
41 changes: 41 additions & 0 deletions tests/Prismic/LanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: jeroen
* Date: 20-3-19
* Time: 21:24
*/

namespace Prismic\Test;

use Prismic\Exception\InvalidArgumentException;
use Prismic\Language;

class LanguageTest extends TestCase
{

/** @var Language */
private $language;
/** @var \stdClass */
private $json;

public function setUp()
{
$this->json = $json = \json_decode($this->getJsonFixture('language.json'));
$this->language = Language::parse($json);
}

public function testCorrectId()
{
$this->assertSame($this->json->id, $this->language->getId());
$this->assertSame($this->json->name, $this->language->getName());
}

/**
* @expectedException InvalidArgumentException
*/
public function testWrongObjectType()
{
Language::parse(new \stdClass);
}
}
18 changes: 18 additions & 0 deletions tests/fixtures/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@
"job-offer": "Job offer",
"product": "Product"
},
"languages": [
{
"id": "en-us",
"name": "English - United States"
},
{
"id": "nl-nl",
"name": "Dutch - Netherlands"
},
{
"id": "es-es",
"name": "Spanish - Spain (Traditional)"
},
{
"id": "fr-fr",
"name": "French - France"
}
],
"tags": ["Cupcake", "Pie", "Featured", "Macaron"],
"forms": {
"everything": {
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/language.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "en-us",
"name": "English - United States"
}

0 comments on commit 85304ce

Please sign in to comment.